turkerdev / fastify-type-provider-zod

MIT License
337 stars 21 forks source link

fastify-swagger support #6

Closed kibertoad closed 2 years ago

kibertoad commented 2 years ago

Using fastify-type-provider-zod together with https://github.com/fastify/fastify-swagger results in route documentation being generated without request schemas.

Configuration used:

  app.register(fastifySwagger, {
    exposeRoute: true,
    openapi: {
      info: {
        title: 'SampleApi',
        description: 'Sample backend service',
        version: '1.0.0',
      },
      servers: [
        {
          url: `http://${
            appConfig.bindAddress === '0.0.0.0' ? 'localhost' : appConfig.bindAddress
          }:${appConfig.port}`,
        },
      ],
      components: {
        securitySchemes: {
          bearerAuth: {
            type: 'http',
            scheme: 'bearer',
            bearerFormat: 'JWT',
          },
        },
      },
    },
  })

...

export const LOGIN_SCHEMA = z.object({
  username: z.string().max(32).describe('someDescription'),
  password: z.string().max(32),
})

const route =       {
        method: 'POST',
        url: '/login',
        handler: someController,
        schema: { body: LOGIN_SCHEMA, describe: 'test route' },
      },
kibertoad commented 2 years ago

See https://github.com/fastify/help/issues/728#issuecomment-1205741881 I'm happy to work on support for this, but would need some pointers. @turkerdev

Gomah commented 2 years ago

Hey @kibertoad, @turkerdev, thank you for creating and maintaining this project!

You could potentially use the same approach as fastify-zod & leverage zod-to-json-schema to generate an openApi3 schema ?

kibertoad commented 2 years ago

@Gomah Sounds like a plan! any suggestions where to plug that in?

turkerdev commented 2 years ago

@kibertoad I did manage to create a working demo (CodeSandbox), do not forget to run "dev" script.

kibertoad commented 2 years ago

@turkerdev Thank you, will check it out! Do you think this should be included as a part of fastify-type-provider-zod, e. g. as zodFastifySwaggerTransformer? Or you see this as an absolutely separate thing?

turkerdev commented 2 years ago

I think we can include it in this package.

kibertoad commented 2 years ago

@turkerdev Awesome, I'll create a PR, and include test as well then. Can you take a look at https://github.com/turkerdev/fastify-type-provider-zod/pull/5 prior to that?