Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint-specific schema transform #749

Closed
2 tasks done
thorhj opened this issue Aug 19, 2023 · 12 comments
Closed
2 tasks done

Endpoint-specific schema transform #749

thorhj opened this issue Aug 19, 2023 · 12 comments

Comments

@thorhj
Copy link
Contributor

thorhj commented Aug 19, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 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
Copy link
Contributor

Uzlopak commented Aug 19, 2023

Does swagger really expect zod schemas?

@kibertoad

@kibertoad
Copy link
Member

@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

@thorhj
Copy link
Contributor Author

thorhj commented Aug 20, 2023

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
Copy link

@mcollina Suggested this API: #727 (comment)

@fredrikj31
Copy link

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
Copy link
Contributor

Uzlopak commented Aug 30, 2023

@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
Copy link

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
Copy link
Contributor

Uzlopak commented Aug 30, 2023

I reviewed it now. Waiting for feedback by @thorhj

@fredrikj31
Copy link

Okay, super. Thanks for the status update. Will be watching the PR 🙏

@fredrikj31
Copy link

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

@mcollina
Copy link
Member

mcollina commented Sep 5, 2023

shipped v8.10.0

@mcollina mcollina closed this as completed Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants