turkerdev / fastify-type-provider-zod

MIT License
337 stars 21 forks source link

swagger: response description is ignored with jsonSchemaTransform #47

Open ricogmacedo opened 1 year ago

ricogmacedo commented 1 year ago

The description of a response is given in the schema definition and this description is ignored when the json is transformed by jsonSchemaTransform.

Schema definition

const healthcheckSchema: FastifySchema = {
  response: {
    200: {
      description: "Healthy",
      properties: z.object({
        status: z.boolean(),
      }),
    },
  },
};

swagger config with jsonSchemaTransform

fastify.register(fastifySwagger, {
  openapi: {
    info: {
      title: packageJsom.name,
      description: packageJsom.description,
      version: packageJsom.version,
    },
    servers: [
      {
        url: "http://localhost:3000",
        description: "Local",
      },
    ]
  }
  transform: jsonSchemaTransform
})

Description Healthy is missing when SwaggerUI is rendering based on transformed JSON: image

Transformed JSON:

{
   "openapi":"3.0.3",
   "info":{
      "title":"some-title",
      "description":"some-description",
      "version":"1.0.0"
   },
   "components":{
      "schemas":{

      }
   },
   "paths":{
      "/":{
         "get":{
            "responses":{
               "200":{
                  "description":"Default Response",
                  "content":{
                     "application/json":{
                        "schema":{
                           "type":"object",
                           "properties":{
                              "status":{
                                 "type":"boolean"
                              }
                           },
                           "required":[
                              "status"
                           ],
                           "additionalProperties":false
                        }
                     }
                  }
               }
            }
         }
      }
   },
   "servers":[
      {
         "url":"http://localhost:3000",
         "description":"Local"
      }
   ]
}
sergiopastan commented 1 year ago

you can use the .describe() function in your zod schema. for example:

const healthcheckSchema: FastifySchema = {
  response: {
    200: z.object({
      status: z.boolean()
    }).describe('Healthy'),
  },
};