fastify-swagger icon indicating copy to clipboard operation
fastify-swagger copied to clipboard

Type.Record() in querystring or headers schema causes crash

Open marcoreni opened this issue 2 years ago • 1 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.2.0

Plugin version

7.4.1

Node.js version

16.15.1

Operating system

macOS

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

12.4

Description

Adding a Type.Record() as querystring schema causes Swagger to crash. The same goes for "headers", while body seems to be OK.

/Volumes/Workspace/test/node_modules/@fastify/swagger/lib/util/common.js:190
  const localRef = jsonSchema.$ref.split('/')[2]
                                   ^

TypeError: Cannot read properties of undefined (reading 'split')
    at resolveLocalRef (/Volumes/Workspace/test/node_modules/@fastify/swagger/lib/util/common.js:190:36)
    at plainJsonObjectToOpenapi3 (/Volumes/Workspace/test/node_modules/@fastify/swagger/lib/spec/openapi/utils.js:133:41)
    at resolveCommonParams (/Volumes/Workspace/test/node_modules/@fastify/swagger/lib/spec/openapi/utils.js:273:15)
    at prepareOpenapiMethod (/Volumes/Workspace/test/node_modules/@fastify/swagger/lib/spec/openapi/utils.js:375:29)
    at Object.swagger (/Volumes/Workspace/test/node_modules/@fastify/swagger/lib/spec/openapi/index.js:46:29)
    at file:///Volumes/Workspace/test/fastify.mjs:22:9
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Steps to Reproduce

import Fastify from 'fastify';
import { Type } from "@sinclair/typebox";
import swagger from "@fastify/swagger";
const fastify = Fastify().withTypeProvider();


await fastify.register(swagger, {
  exposeRoute: true,
  routePrefix: '/documentation',
  openapi: {
    info: { title: 'OpenAPI', version: '1.0.0' }
  }
})

fastify.get('/foo', {
  schema: {
    querystring: Type.Record(Type.String(), Type.String()),
  }
}, (req, reply) => ({ result: 'OK' }))

await fastify.ready()
fastify.swagger()

Expected Behavior

I think it should work. Querystring passes schema validation and is correctly inferred by fastify.

According to the internet free-form querystring parameters are supported by OAI 3. I tried adding explode: true, style: "form" to the schema but nothing changed, therefore I assume this is a @fastify/swagger issue.

marcoreni avatar Jul 12 '22 20:07 marcoreni

This seems to be a duplicate of https://github.com/fastify/fastify-swagger/issues/633.

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Jul 12 '22 20:07 mcollina