more-urgen-jest
3 years agoOccasional Contributor
c# openapi parameter schema object
is it possible to define a parameter using a ref to a schema? e.g.:
"/path/to/here": {
"get": {
"responses": {
"200": {
"$ref": "#/components/responses/Response"
}
},
"operationId": "get-to-here",
"description": "Get to here",
"parameters": [
{
"schema": {
"$ref": "#/components/schemas/Request"
},
"in": "query",
"name": "Request",
"description": "Request for getting here",
"required": true
}
],
"tags": [
"Here"
]
}
}
this generates a C# abstract controller as expected via the openapi generator:
public abstract Task<IActionResult> GetToHere([FromQuery (Name = "Request")][Required()]Request request);
but the NSwag generator, via a service reference, generates:
public async Task<Response> GetToHereAsync(Request request, CancellationToken cancellationToken)
{
if (request == null)
throw new System.ArgumentNullException("request");
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/path/to/here?");
urlBuilder_.Append(System.Uri.EscapeDataString("Request") + "=").Append(System.Uri.EscapeDataString(ConvertToString(request, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
urlBuilder_.Length--;
rather than adding the primitives in the request object as separate query parameters.
the request is defined as:
"Request": {
"title": "Request",
"x-stoplight": {
"id": "blah"
},
"type": "object",
"x-examples": {
"example": {
"$ref": "#/components/examples/RequestExample"
},
"example Names": {
"$ref": "#/components/examples/RentalSuburbRangeRequestNames"
}
},
"properties": {
"Id": {
"type": "string"
},
"Name": {
"type": "string"
},
"OtherName": {
"type": "string"
}
}
}
any hints much appreciated
Yes, you can use schemas in parameters.
You might be looking for Parameter serialization, specifically the `explode` keyword... https://swagger.io/docs/specification/serialization/
That will allow each field in an object to be its own query name/value pair.