fastify-swagger icon indicating copy to clipboard operation
fastify-swagger copied to clipboard

Using zod 4, and fastify-swagger reports "/$defs/__schema0 does not exist in document"

Open Chan-cdt opened this issue 8 months ago • 2 comments

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.

Image

How can I fix it ,or it just not match?

Link to code that reproduces the bug

1

Expected Behavior

No response

Chan-cdt avatar Jun 11 '25 09:06 Chan-cdt

I'm having the same issue. Apparently, recursive zod schemas doesn't seem to work well with fastify-swagger

mauriciotp avatar Aug 22 '25 12:08 mauriciotp

What type-provider are you using? fastify-type-provider-zod has extra logic in it to handle this scenario.

kibertoad avatar Sep 01 '25 09:09 kibertoad