-
Notifications
You must be signed in to change notification settings - Fork 67
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Automatically replace known schemas with $ref
s
#142
Comments
@rattrayalex can you take a look at #99. I think this is exactly what we've implemented there. However this is not yet released since we want to merge one more PR that has a breaking change |
@AGalabov for our use case we don't want the ref id to live on metadata props attached to the zod schema, we want the registry to be the sole source of that information. So if we've called For this to work on every schema the generator would have to call a method like So for example when the generator gets to the Basically we need to declare what goes in |
Touche, that's what I did @jedwards1211 🤷 I added what you were after here: https://github.com/samchungy/zod-openapi#manually-registering-schema |
Hey @AGalabov , congrats on the v5.0 release! Excited to try it out! I saw in the other thread your comment:
Would you be open to a PR implementing this, similar to the functionality Sam has in |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
It'd be very helpful if this library could automatically replace zod schemas with their
$ref
s once they are registered, at the time the document is generated.This could perhaps be opt-in, perhaps with a
generator.replaceRefs()
or similar command.Given this code:
Current output:
Desired output:
This should also enable recursive/cyclical schemas, which currently through an error: #140
Currently, we do this manually; if it helps, here's our
replaceRefs
code:code
```ts export function replaceRefs( schema: z.ZodTypeAny, schemaRefs: Map ): T { const visited = new Set(); return recurse(schema) as T; function recurse(schema: z.ZodTypeAny): z.ZodTypeAny { const ref = schemaRefs.get(schema); if (ref) return ref; if (visited.has(schema)) throw new Error("circular references not supported yet"); visited.add(schema);}
}
The text was updated successfully, but these errors were encountered: