-
Notifications
You must be signed in to change notification settings - Fork 67
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
Inherit zod schema description by default #104
Comments
Hey @j-murata would you be able to provide an example. I.e what zod schema you would want to use and what the returned OpenAPI should be? I think I got the overall idea but it would be great if I can see something that showcases the problem directly 🙏 . |
For example, suppose the following schema is defined.
|
@j-murata I get it now. Great catch. I'll check where the issue is as this is definitely not the expected behavior. |
@j-murata hmmm I just wrote some tests and they were all passing. That lead me to take a closer look at your code: Your schema So for example if you just inline your schema in the Note: This is not only a problem for Please check if this answers your issues and if you can resolve it with this information and get back to us 🙏 |
@AGalabov |
I made the following changes to
|
Overriding zod's flowchart LR
S([Zod schema])
A([API spec])
I([Input validator])
T([*.d.ts files])
S --> A
S --> I
S --> T
Therefore, I would be happy if you could consider a different solution to this in the future, i.e. not overriding |
@j-murata I get your point. And that is what we are doing in our project as well. Basically we have multiple schemas spread across files that we use for documentation (through the registry), validation and type coverage. And I do get your point about the description specifically, however with your current approach you wouldn't be able to use I can share some insight on how we have setup our project:
All that being said if it is really the description that you want, then that shouldn't be a problem. It would be a slightly less elegant solution but it can work. Just let me know if the explanation above changes your perspective on the matter. If not I'll see how the implementation should change to account for it. |
@AGalabov Thanks for your reply.
I may not fully understand your explanation, but my perspective on this probably remains the same. For example, suppose there is a repository for schema definitions that is shared by several teams.
There is another repository, separate from the above, dedicated to the API server team.
flowchart LR
S[(repo for\nCommon Schema)]
C([repo for\nClient App])
A([repo for\nAPI Server])
B([repo for\nDB Server])
C -.-> S
A -.-> S
B -.-> S
C -.-> A
|
Hey @AGalabov, thanks for detailing how you approach this in your own projects. I feel like these patterns are valuable information to share as part of the |
@j-murata I see your point now, thanks for the explanation. With that information provided I can agree that logic that depends on inner zod workings (length/description/regex, ...) that are already present per schema should in fact be retrieved from the schemas themselves as opposed to relying on the overrides and the call of I'll make the necessary changes asap. |
@AGalabov Thank you for your understanding.
I myself am not in such an urgent need, so there is no need for you to respond so quickly. |
@AGalabov Any updates on this? I found a workaround that intentionally delays the loading of the schemas. import { z } from 'zod';
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
// import { errorResponse } from './schema.mjs';
extendZodWithOpenApi(z);
const { errorResponse } = await import('./schema.mjs'); |
Hey @j-murata sorry for the delay. I am waiting on a Code review. We were kind of busy at work, but I'll ping around today. |
…available-without-zod-overrides use zod schema description when generating components instead of only .openapi
@AGalabov |
@j-murata sorry, I kind of missed your message since the issue was preemptively closed by me :/ I saw it randomly in my mail. You are correct I've missed a place in the code where we were relying on the description from |
…b.com:asteasolutions/zod-to-openapi into enhancement/#99-automatic-registration
…ption-not-being-used Bugfix/#104 nested description not being used
@AGalabov |
I am aware that the extension method
.openapi()
can be used to set the description, as shown above.However, currently, it appears that descriptions of schema other than the root schema that is the target of
register()
, i.e., child or grandchild schema, are not automatically inherited. (See #95. My apologies if I am wrong.)I think it is great that the
.openapi()
method provides a way to override the description, but it would be even better if the original description could be inherited by default.Currently, I am handling this by calling my own recursive functions such as the following in advance, but it would be very helpful if these could be done inside functions such as
register()
.The text was updated successfully, but these errors were encountered: