samchungy/fastify-zod-openapi

Operation description and summary

Closed this issue · 4 comments

First of all, thank you for a wonderful library. Solves the exact issue I was having gluing together a bunch of disparate solutions.

Is there a way to add a description and summary to an endpoint?

Hey there, thanks for the kind words!

You can define a description and summary here on the same level as content, but it looks like fastify-swagger swallows up the summary field. You'd need to raise a PR somewhere around here

Thanks. But this is a description of the response, not of the operation itself, yeah? For example, it wouldn't show up here (like "Uploads an image") in Swagger UI

image

Ah I see, my mistake, the operation.... 🙈

You should be able to declare them under the schema section.

eg.

  app.withTypeProvider<FastifyZodOpenApiTypeProvider>().route({
    method: 'POST',
    url: '/:jobId',
    schema: {
      description: 'hello', // <- HERE
      summary: 'world', // <- AND HERE
      params: z.object({
        foo: z.string().openapi({
          description: 'path parameter example',
          example: 'bar',
        }),
      }),
      response: {
        200: z.object({
          jobId: JobIdSchema,
        }),
        201: {
          content: {
            'application/json': {
              example: { jobId: '123' },
              schema: z.object({
                jobId: z.string().openapi({
                  description: 'Job ID',
                  example: '60002023',
                }),
              }),
            },
          },
        },
      },
    } satisfies FastifyZodOpenApiSchema,
    handler: async (_req, res) =>
      res.send({
        jobId: '60002023',
      }),
  });

There should hopefully be some intellisense types in there for you to use

Wonderful, thank you! Sending a coffee your way