Using zod 4, and fastify-swagger reports "/$defs/__schema0 does not exist in document"
Prerequisites
- [x] I have written a descriptive issue title
- [x] I have searched existing issues to ensure the bug has not already been reported
Fastify version
5.2.1
Plugin version
No response
Node.js version
22.12.
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
windows 11
Description
I customized a request body schema ,including a json format object. Here is the example.
type Literal = boolean | number | string | null | undefined type Json = Literal | { [key: string]: Json } | Json[] const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]) const jsonSchema: z.ZodSchema<Json> = z.lazy(() => z.union([literalSchema, z.array(jsonSchema), z.record(z.string(), jsonSchema)]))
export const testModel = z.object({ id: z.string(), content: jsonSchema.nullish() })
When I open the Swagger UI url,it reports error: Could not resolve reference: Could not resolve pointer: /$defs/__schema0 does not exist in document.
How can I fix it ,or it just not match?
Link to code that reproduces the bug
1
Expected Behavior
No response
I'm having the same issue. Apparently, recursive zod schemas doesn't seem to work well with fastify-swagger
What type-provider are you using? fastify-type-provider-zod has extra logic in it to handle this scenario.