Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code generated by NSwag.ApiDescription.Client generates incorrect URLs #5096

Open
ebarnard opened this issue Jan 27, 2025 · 0 comments
Open

Comments

@ebarnard
Copy link

ebarnard commented Jan 27, 2025

Describe the bug

When updating to NSwag v14 from v13, I've found that segmented URLs where the last segment is optional are generated incorrectly.

var urlBuilder_ = new System.Text.StringBuilder();

// Operation Path: "views/searchinstruments/{type}/{query}"
urlBuilder_.Append("views/searchinstruments/");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(type, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Append('/');
if (query != null)
{
    urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(query, System.Globalization.CultureInfo.InvariantCulture)));
}
else
    if (urlBuilder_.Length > 0) urlBuilder_.Length--;
urlBuilder_.Append("{query}");

PrepareRequest(client_, request_, urlBuilder_);

Version used

v14.0.0 and v14.2.0

To Reproduce

No small reproducer yet, but this is a snippet of the OpenAPI V3 JSON from which the code above is generated:

"/views/searchinstruments/{type}/{query}": {
  "get": {
    "tags": [
      "Searches"
    ],
    "operationId": "GetSearchInstrumentsEndpoint_Get",
    "parameters": [
      {
        "name": "x-actor-id",
        "in": "header",
        "required": true,
        "schema": {
          "type": "string"
        },
        "examples": {
          "No Netwealth data": {
            "value": "5df23838-73eb-41e8-893f-b6cfd019ade3"
          }
        }
      },
      {
        "name": "type",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        },
        "examples": {
          "All": {
            "value": "All"
          },
          "Cash": {
            "value": "Cash"
          },
          "Common Stock": {
            "value": "Common Stock"
          },
          "ETF": {
            "value": "ETF"
          },
          "Fund": {
            "value": "Fund"
          },
          "Investment Trust": {
            "value": "Investment Trust"
          }
        }
      },
      {
        "name": "query",
        "in": "path",
        "schema": {
          "type": "string"
        }
      }
    ],
    "responses": {
      "200": {
        "description": "A success response",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/getSearchInstrumentsEndpointResponse"
            },
            "examples": {
              "Single Instrument": {
                "value": "{\"instruments\":[{\"name\":\"Apple\",\"code\":\"AAPL\",\"currency\":\"USD\",\"isGBX\":false,\"isin\":\"US12345\",\"assetClassType\":\"Common Stock\",\"exchange\":\"US\",\"ticker\":\"AAPL\",\"isStatic\":false}],\"totalFoundInstruments\":1}"
              }
            }
          }
        }
      },
      "400": {
        "description": "A bad request response"
      }
    },
    "security": [
      {
        "function_key": [ ]
      }
    ]
  }
},

Expected behavior

The URL does not contain the string "{query}" at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant