turkerdev / fastify-type-provider-zod

MIT License
303 stars 19 forks source link

204 response without content #76

Open EricSmekens opened 4 months ago

EricSmekens commented 4 months ago

Currently, fastify-swagger allows the specification of a 204 without any content:

https://github.com/fastify/fastify-swagger?tab=readme-ov-file#empty-body-responses

{
  response: {
    204: {
      type: 'null',
      description: 'No Content'
    },
    503: {
      type: 'null',
      description: 'Service Unavailable'
    }
  }
}

However, when you do this together with this library, that results in a error:

"err": {
    "type": "Error",
    "message": "Invalid schema passed: {\"type\":\"null\",\"description\":\"No Content\"}",
    "stack": "Error: Invalid schema passed: {\"type\":\"null\",\"description\":\"No Content\"}
   at resolveSchema (--\\node_modules\\fastify-type-provider-zod\\dist\\index.js:116:11)
   at Object.transform (--\\node_modules\\fastify-type-provider-zod\\dist\\index.js:72:32)
   at Object.swagger (--\\node_modules\\@fastify\\swagger\\lib\\spec\\openapi\\index.js:36:21)
   at fastify.route.handler (--\\node_modules\\@fastify\\swagger-ui\\lib\\routes.js:175:28)
   at preHandlerCallback (--\\node_modules\\fastify\\lib\\handleRequest.js:137:37)
   at validationCompleted (--\\node_modules\\fastify\\lib\\handleRequest.js:121:5)
   at preValidationCallback (--\\node_modules\\fastify\\lib\\handleRequest.js:98:5)
   at handler (--\\node_modules\\fastify\\lib\\handleRequest.js:75:7)
   at handleRequest (--\\node_modules\\fastify\\lib\\handleRequest.js:24:5)
   at runPreParsing (--\\node_modules\\fastify\\lib\\route.js:593:5)"
}

How to achieve this with zod? I've tried z.undefined(), but that is not resulting in an empty content. Or is this just a bug at this moment?

anthonyvii27 commented 2 months ago

Yes, I'm here with the same issue. Commenting to receive news about this functionality.

StrangeBytesDev commented 1 month ago

You can do

response: {
  200: z.null().describe('No Content'),
}