fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
889 stars 199 forks source link

Bad request for querystring when using explode is false #782

Closed mstimvol closed 5 months ago

mstimvol commented 5 months ago

Prerequisites

Fastify version

4.25.2

Plugin version

8.14.0

Node.js version

18.12.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

Windows 11

Description

I've a get request that sends a comma separated list of ids to fastify. But when I send more than one id, I'm getting the following error:

{
    "statusCode": 400,
    "code": "FST_ERR_VALIDATION",
    "error": "Bad Request",
    "message": "querystring/ids/0 must be number"
}

That's the request that causes the error: GET http://localhost:8888/test?ids=201,202

Steps to Reproduce

  const fastify = Fastify({
    logger: true,

    ajv: {
      plugins: [
        function (ajv) {
          ajv.addKeyword({ keyword: 'style' })
          ajv.addKeyword({ keyword: 'explode' })
        }
      ]
    }
  })

  await fastify.register(FastifySwagger, {
    openapi: {
      info: {
        title: 'MyAPI',
        version: '0.1.0'
      },
      host: 'https://host.tld',
      schemes: ['http', 'https'],
      consumes: ['application/json'],
      produces: ['application/json']
    }
  })

  fastify.get('/test', {
    schema: {
      querystring: {
        type: 'object',
        additionalProperties: false,
        properties: {
          ids: {
            type: 'array',
            description: 'List of ids to return',
            uniqueItems: true,
            items: {
              type: 'number',
              format: 'int64',
              exclusiveMinimum: 200
            }
          },
          explode: false
        }
      }
    }
  }, (req) => {
    // do something
    console.log(req.query)
    return ''
  })

  await fastify.listen({
    host: 'localhost',
    port: 8888
  })

Expected Behavior

My expected behavior is that req.query.ids is an array that contains the ids, e.g. [201, 202, 203].

Uzlopak commented 5 months ago

Without deeper checking, i think you also need to set an appropriate querystring parser which can handle that format. The native querystring parser is afaik not able to handle that exploded format.