swagger-api / swagger-js

Javascript library to connect to swagger-enabled APIs via browser or nodejs
http://swagger.io
Apache License 2.0
2.62k stars 758 forks source link

Path not resolving correctly if the URL contains space #3516

Closed DevyaJha closed 4 months ago

DevyaJha commented 4 months ago

Q&A (please complete the following information)

Content & configuration

Describe the bug you're encountering

If the URL has spaces, then the path parameters are not resolved correctly.

To reproduce...

Steps to reproduce the behavior:

const Swagger = require('swagger-client')
const spec = {
    host: 'swagger.io',
    basePath: '/v1',
    paths: {
      '/accounts/{accountName}/resources/Task lists/operations/getTasks/{id}/orders/{order}/{itemId}': {
        get: {
          operationId: 'getMe',
          parameters: [
            {
                "name": "accountName",
                "in": "path",
                "description": "Account name",
                "required": true,
                "schema": {
                    "type": "string"
                }
            },
            {
              in: 'path',
              name: 'id',
              type: 'number',
              required: true,
            },
            {
              in: 'path',
              name: 'order',
              type: 'number',
              required: true,
            },
            {
              in: 'path',
              name: 'itemId',
              type: 'number',
              required: true,
            },
          ],
        },
      },
    },
  };

  const req = Swagger.buildRequest({
    spec,
    operationId: 'getMe',
    parameters: { accountName: 'Account 1', id: '123', order: '456', itemId: '789' },
  });

  console.log('req ---- ', req)

Current behavior

req ---- { url: 'http://swagger.io/v1/accounts/{accountName}/resources/Task lists/operations/getTasks/{id}/orders/{order}/{itemId}', credentials: 'same-origin', headers: {}, method: 'GET' }

Expected behavior

Resolved req should be -

req ---- { url: 'http://swagger.io/v1/accounts/Account%201/resources/Task lists/operations/getTasks/123/orders/456/789', credentials: 'same-origin', headers: {}, method: 'GET' }

Screenshots

Additional context or thoughts

orionchikby commented 4 months ago

in my case I use bower-asset/swagger-ui , and in "v5.17.7" was bug just update to v5.17.9

char0n commented 4 months ago

Hi everybody,

In latest versions of swagger-client we've utilized the spec compliant OpenAPI Path template parsing.

Here is what OpenAPI stipulates about path templating:

Path templating refers to the usage of template expressions, delimited by curly braces ({}), to mark a section of a URL path as replaceable using path parameters. link

This specifies that path template forms a relative URL reference. This is what OpenAPI stipulates about relative URL reference:

Unless specified otherwise, all properties that are URLs MAY be relative references as defined by RFC3986 link

This means that parsing of path template MUST be RFC3986 compliant and thus cannot contain unencoded empty string values. As the path template with empty space doesn't qualify as valid path template, it's not being resolved.


Having said that, we didn't introduce a regression, we fixed a bug that allowed to define path templates as invalid RFC3986. Now we don't allow it anymore.

To remedy specific situation as described in the description of this issue

/accounts/{accountName}/resources/Task lists/operations/getTasks/{id}/orders/{order}/{itemId}

needs to be replaced by

/accounts/{accountName}/resources/Task%20lists/operations/getTasks/{id}/orders/{order}/{itemId}