astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)
openapi-zod-client.vercel.app
717 stars 80 forks source link

Openapi property with the type const resolving as z.unknown() #290

Open zamaraevk opened 4 months ago

zamaraevk commented 4 months ago

Read before opening

Describe the bug A clear and concise description of what the bug is.

I am trying to generate zod schema from openapi json file where property type as const and it's generates z.unknown()

Minimal reproduction

"components": { "schemas": { "Pet": { "type": "object", "required": ["id", "name"], "properties": { "id": { "type": "integer", "format": "int64" }, "name": { "type": "string" }, "tag": { "type": "string" }, "type": { "const": "BULLDOG", "title": "Type of dog" } } },

RESULT:

const Pet = z .object({ id: z.number().int(), name: z.string(), tag: z.string().optional(), type: z.unknown().optional() }) .passthrough();

Expected behavior A clear and concise description of what you expected to happen. Ideally, what the generated code would look like.

Should be:

z.literal("BULLDOG").optional()

Additional context Add any other context about the problem here.

LINK to Playground