swagger-api / swagger-js

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

OpenAPI 2.x: undefined body and path parameters are included in requests #3438

Closed glowcloud closed 4 months ago

glowcloud commented 4 months ago

Content & configuration

Swagger/OpenAPI definition:

{
  host: 'swagger.io',
  basePath: '/v1',
  paths: {
    '/pets/find/{pathParam}': {
      get: {
        operationId: 'getMe',
        parameters: [
          {
            in: 'path',
            name: 'pathParam',
            type: 'string',
            required: false,
          },
          {
            in: 'body',
            name: 'bodyParam',
            type: 'string',
            required: false,
          },
        ],
        responses: {
          200: {
            description: 'ok',
          },
        },
      },
    },
  },
}

Swagger-Client usage:

SwaggerClient.buildRequest({
  spec,
  operationId: 'getMe',
  parameters: { bodyParam: undefined, pathParam: undefined },
})

Describe the bug you're encountering

Swagger Client adds undefined values to path and body parameters.

{
  url: 'http://swagger.io/v1/pets/find/undefined',
  credentials: 'same-origin',
  headers: {},
  method: 'GET',
  body: undefined
}

Expected behavior

Swagger Client should not add anything to the body when provided with undefined values and should not replace the parameter with undefined string in the path.

{
  url: 'http://swagger.io/v1/pets/find/{pathParam}',
  credentials: 'same-origin',
  headers: {},
  method: 'GET'
}
char0n commented 4 months ago

Addressed in https://github.com/swagger-api/swagger-js/pull/3439