Skip to content

Zod schema should not add a default based on openAPI default attribute #2633

@GeeWee

Description

@GeeWee

I think there might be something weird in regards to how the zod schema is generated if an OpenAPI spec uses the default parameter.

For requests, the default parameter means "The default value is the one that the server uses if the client does not supply the parameter value in the request." (docs).

This means that the default value does not need to be set by the client, but can be optionally - aka it is informative "if you don't send anything, this is the option the server will pick). However currently, the zod schemas treat a default value as something to set on the client side if the value is not provided. See the repro example below.

Related issues: #1922 which I think where the behaviour was introduced and maybe #2577 that seems related.

Reproduction

This YAML

info:
  title: Untitled service
  version: 1.0.0
openapi: 3.1.0
paths:
  /pcf/v1-preview2/estimate:
    post:
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              schema:
                type: object
                properties:
                  unit:
                    type: string
                    enum: [ m, km ]
                    default: km
                    description: Unit of distance measurement
export const postPcfV1Preview2EstimateResponseUnitDefault = "km";

export const postPcfV1Preview2EstimateResponse = zod.object({
  "unit": zod.enum(['m', 'km']).default(postPcfV1Preview2EstimateResponseUnitDefault).describe('Unit of distance measurement')
})

But I would expect it to generate the following zod schema, as the 'default' value is not something the client needs to handle.

export const postPcfV1Preview2EstimateResponse = zod.object({
  "unit": zod.enum(['m', 'km']).describe('Unit of distance measurement')
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    zodZod related issue

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions