Forum Discussion

more-urgen-jest's avatar
more-urgen-jest
Occasional Contributor
3 years ago

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
    • more-urgen-jest's avatar
      more-urgen-jest
      Occasional Contributor

      I tried changing it by adding explode: true:

          "/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,
                  "explode": true
                }
              ],
              "tags": [
                "Here"
              ]
            }
          }

      but when I rebuild the project the output is the same:

              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("&");
      • more-urgen-jest's avatar
        more-urgen-jest
        Occasional Contributor

        updating the packages to the latest versions:

         

            <PackageReference Include="NSwag.ApiDescription.Client" Version="13.17.0">

         


        resulted in this output:

         

                public virtual 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?");
                    foreach (var item_ in Request.AdditionalProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key) + "=").Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }
                    urlBuilder_.Length--;

         

         

        so does that mean that if I want the query parameters to be populated just setting the properties of the object won't do it, I also have to populate AdditionalProperties???

        also, it's Request.AdditionalProperties instead of request.AdditionalProperties, which will result in a runtime error?