fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
910 stars 200 forks source link

Endpoint-specific schema transform #749

Closed thorhj closed 1 year ago

thorhj commented 1 year ago

Prerequisites

πŸš€ Feature Proposal

fastify-swagger allows choose a transform function in case you have a different schema type than the built-in one.

You can have different schema types for different endpoints, but to my knowledge fastify-swagger does not support this (I couldn't find anything for this at least). I would like to have an endpoint-specific transform function that differs from the "global" transform function. Something like this is what I imagine:

fastify.get("/my-endpoint", {
  schema: { ... },
  config: {
    swaggerTransform: ( ... ) => { ... }
  }
},
async (request, reply) => { ... });

This swaggerTransform property should be able to indicate the "default" (no) transform or any other transform function.

Motivation

In some of my projects I am fastify-type-provider-zod which has its own transform function for this use-case.

However, I have found a need for having a specific endpoint NOT using zod schemas to define the schema. In my case, we would like to use a plugin that adds a new route-shorthand to fastify to easily create AWS SNS event webhooks correctly, and in this plugin the schema is implemented using the default schema declaration. When mixing schema types in the project, fastify-swagger will fail because it expects the routes to have zod schemas, but this one endpoint does not.

My current workaround is to hide the endpoint in question from swagger, but I would like to have it included, and to do that I think I need an endpoint-specific transform function for the endpoints in question.

Example

No response

Uzlopak commented 1 year ago

Does swagger really expect zod schemas?

@kibertoad

kibertoad commented 1 year ago

@Uzlopak not swagger per se, but if type-provider-zod compiler and validator are registered, that will be the format of schemas that they will expect

kibertoad commented 1 year ago

see https://github.com/turkerdev/fastify-type-provider-zod/blob/85675fad44c7679622d80fb1925f5901cc2f57f8/src/index.ts#L32

thorhj commented 1 year ago

By the way, if you think this is a good idea and if we make a design for the interface (there are probably opinions on my suggestion), I could work on this feature myself.

MatanYadaev commented 1 year ago

@mcollina Suggested this API: https://github.com/fastify/fastify-swagger/issues/727#issuecomment-1618258696

fredrikj31 commented 1 year ago

I really need this feature as well, for a project using zod type provider, as a alternative schema parser.

Is this being shipped in the next version, or what's the status of it? πŸ€”

Uzlopak commented 1 year ago

@fredrikj31 If you could test #752 and check if it solves your issue, I could easier approve. Personally I dont have a clue because I dont use zod etc..

fredrikj31 commented 1 year ago

I have tried pulling it down, and using it on a fastify server with zod type provider, and it seems to be working, so it looks like it solves my problem. Can this be released so I can use it on my apis?

Uzlopak commented 1 year ago

I reviewed it now. Waiting for feedback by @thorhj

fredrikj31 commented 1 year ago

Okay, super. Thanks for the status update. Will be watching the PR πŸ™

fredrikj31 commented 1 year ago

Is there any update to this? I can see that the PR has been merged in.

mcollina commented 1 year ago

shipped v8.10.0