Automatically replace known schemas with $ref
s
#190
Replies: 4 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 |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
Touche, that's what I did @jedwards1211 🤷 I added what you were after here: https://github.com/samchungy/zod-openapi#manually-registering-schema |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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);}
}
Beta Was this translation helpful? Give feedback.
All reactions