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

feature: deduplicate schemas in generated OpenAPI #366

Open
llllvvuu opened this issue Aug 20, 2023 · 2 comments
Open

feature: deduplicate schemas in generated OpenAPI #366

llllvvuu opened this issue Aug 20, 2023 · 2 comments
Labels

Comments

@llllvvuu
Copy link
Contributor

llllvvuu commented Aug 20, 2023

Let's say you have a Post entity that's returned from a bunch of endpoints. Repeating that entity in the OpenAPI schema at every endpoint will create a large .JSON file. It probably also makes it harder for downstream tooling to organize types (would end up with Post1 Post2 etc) - this is one advantage of schema-first codegen driven development right now, there is clean code available when you e.g. jump to definition which takes you to a nicely-named interface

Check out the OpenAPI specification's components: https://swagger.io/specification/#:~:text=Specification%20Extensions.-,Components%20Object,-Holds%20a%20set

Perhaps referential equality could be used to output refs. OR, change the library's API to allow specifying shared schemas.

EDIT: Referential equality probably wouldn't work; even if the Zod schema is referentially equal (which it may not be if the user defines it twice for some reason), @anatine/zod-openapi probably doesn't create referentially equal objects. The objects would have to be deduped by content (perhaps JSON.stringify if it can be trusted that the objects are stringifiable and deterministically ordered). This kind of magic seems like it'd be brittle.

@dmvinson
Copy link

dmvinson commented Sep 5, 2023

zod-to-openapi seems to handle this by providing extensions to the Zod interface to let users define OpenAPI names for different Zod schemas. @michaelangeloio @oliverbutler Would you be open to a zod-to-openapi plugin implementation as a solution for this? At the moment this makes using the generated openapi schema pretty difficult if you're not writing a client by hand.

@dmvinson
Copy link

On further look, it seems like @anatine/zod-openapi offers a extendZodWithOpenApi method. There could be a second step in processing that goes through all the title fields if user adds those and creates a components field in the OpenAPI schema from the titles defined by the user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants