Skip to content

[zod-openapi] generateSchema always returns 3.1 SchemaObject even when 3.0 is requested #263

@naighes

Description

@naighes

The function generateSchema accepts an openApiVersion parameter with values '3.0' | '3.1'.
However, even when '3.0' is passed, the returned type is always the 3.1 SchemaObject:

const schema = generateSchema(some_schema, true, '3.0')
// typescript infers schema as SchemaObject (3.1)

This causes type errors when using openapi3-ts/oas30.
I think it's going to be quite tricky, but I guess you should do something like this:

export function generateSchema<
  V extends '3.0' | '3.1' = '3.1'
>(
  zodRef: OpenApiZodAny,
  useOutput = false,
  openApiVersion?: V,
): V extends '3.0' ? SchemaObject30 : SchemaObject31 {
  // ...snip...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions