Skip to content

Unable to generate openapi schema from params or querystring #244

@mvuckovic

Description

@mvuckovic

Hello!

We're trying to upgrade from V4 to V6 and encountered an issue when trying to generate open-api schemas from params or queryParams.
On V4 this was working fine but on V6 it fails with "Cannot read properties of undefined (reading 'type')" when trying to generate the schema.

Is there any particular reason why it's now not possible to get whole schema from params or queryParams?

Here's the code taken from README file but slightly modified to showcase failure scenario

PS: If I comment out the z.globalRegistry.add(USER_QUERY, { id: 'UserQuery }); then schema will be generated properly but query params will be embedded in path.

import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import fastify from 'fastify';
import { z } from 'zod/v4';
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
import {
  jsonSchemaTransformObject,
  jsonSchemaTransform,
  serializerCompiler,
  validatorCompiler,
} from 'fastify-type-provider-zod';

const USER_SCHEMA = z.object({
  id: z.number().int().positive(),
  name: z.string().describe('The name of the user'),
});

const USER_QUERY = z.object({
    name: z.string()
})

z.globalRegistry.add(USER_SCHEMA, { id: 'User' });
z.globalRegistry.add(USER_QUERY, { id: 'UserQuery });

const app = fastify();
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);

app.register(fastifySwagger, {
  openapi: {
    info: {
      title: 'SampleApi',
      description: 'Sample backend service',
      version: '1.0.0',
    },
    servers: [],
  },
  transform: jsonSchemaTransform,
  transformObject: jsonSchemaTransformObject
})

app.register(fastifySwaggerUI, {
  routePrefix: '/documentation',
});

app.after(() => {
  app.withTypeProvider<ZodTypeProvider>().route({
    method: 'GET',
    url: '/users',
    schema: {
      querystring: USER_QUERY,
      response: {
        200: USER_SCHEMA.array(),
      },
    },
    handler: (req, res) => {
      res.send([]);
    },
  });
});

async function run() {
  await app.ready();

  await app.listen({
    port: 4949,
  });

console.log(`Documentation running at http://localhost:4949/documentation`);
}

run();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions