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: execute builder for formData parameter should not skip falsy values #3434

Closed glowcloud closed 4 months ago

glowcloud commented 4 months ago

Content & configuration

Swagger/OpenAPI definition:

{
  host: 'swagger.io',
  basePath: '/v1',
  paths: {
    '/pets/findByStatus': {
      get: {
        operationId: 'getMe',
        parameters: [
          {
            in: 'formData',
            name: 'status',
            type: 'boolean',
            required: false,
          },
        ],
        responses: {
          200: {
            description: 'ok',
          },
        },
      },
    },
  },
}

Swagger-Client usage:

SwaggerClient.buildRequest({
  spec,
  operationId: 'getMe',
  parameters: { status: false },
});

Describe the bug you're encountering

Swagger Client does not add the status parameter with false value to the request.

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

Expected behavior

Swagger Client should add status=false to the request body

{
  url: 'http://swagger.io/v1/pets/findByStatus',
  credentials: 'same-origin',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  method: 'GET',
  body: 'status=false'
}

Additional context or thoughts

This behaviour is correct for query parameters as we convert falsy values to string there https://github.com/swagger-api/swagger-js/blob/0883d2ddeb1feaf60f8e9480c6a801db0c72a8d9/src/execute/swagger2/parameter-builders.js#L46-L52 The same logic should be applied to form data.

char0n commented 4 months ago

Addressed in https://github.com/swagger-api/swagger-js/commit/7e1cf7b4af0545eb296f4bde3bddabfd6280352e