astahmer / openapi-zod-client

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

Enum with a null value causes type error #236

Open BrandonGoren opened 11 months ago

BrandonGoren commented 11 months ago

Describe the bug As of 1.11.1, the following command produces an enum that does not work with zod.

openapi-zod-client https://raw.githubusercontent.com/plaid/plaid-openapi/master/2020-09-14.yml -o src/generated/plaid-api-schemas.ts --export-schemas  
/** Has a type error **/
export const OwnershipType = z.enum([
  null,
  'individual',
  'joint',
  'association',
  'trust',
])

Minimal reproduction

OwnershipType:
    nullable: true
    type: string
    enum:
      - null
      - individual
      - joint
      - association
      - trust

Expected behavior The z.enum should be nullable, and not include the null value.

jjdonov commented 2 months ago

I'm also bumping into this. Has anyone found a reliable workaround?

mykeels commented 1 month ago

I found it helpful to reference enums like:

{
  "components": {
    "schemas": {
      "Person": {
        "type": "object",
        "properties": {
          "Level": {
            "allOf": [
              {
                "$ref": "#/components/schemas/Level"
              }
            ],
            "nullable": true
          }
        }
      },
      "Level": {
        "type": "integer",
        "enum": [0, 1, 2, 3, 4, 5, 6, 9]
      }
    }
  }
}

So the Level enum itself is not nullable, but its reference in Person schema, is.

jjdonov commented 1 month ago

@mykeels unfortunately in my case we are consuming another companies api spec. We've had to resort to preprocessing it to effectively do what you described here before passing it to the library