asteasolutions / zod-to-openapi

A library that generates OpenAPI (Swagger) docs from Zod schemas
MIT License
955 stars 60 forks source link

Combining `partial` with `null` in a union type results in double producing `null` type matcher #233

Open brent-statsig opened 5 months ago

brent-statsig commented 5 months ago

The code below is causing problems for my use case, and I can't successfully generate a valid openapi definition because of it. Let me know if you need any more context!

Offending ZOD repro

export const ConditionSchema = z
  .object({
    targetValue: z.union([
      z.string().array(),
      z.number().array(),
      z.string(),
      z.number(),
      z.null(),
    ]),
  })
  .partial({ targetValue: true });

Generated openapi block


"anyOf": [
  { "type": "array", "items": { "type": "string" } },
  { "type": "array", "items": { "type": "number" } },
  { "type": "string" },
  { "type": "number" },
  { "type": "null" },
  { "type": "null" }
]
AGalabov commented 5 months ago

@brent-statsig thank you for reporting that. Right now it is kind of hard to find time for such fixes, but I'll try and get to take a look at it whenever I can. In the meantime if you have an idea on how to fix it - feel free to open up a PR