openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Support null in OpenAPI 3.1 union types

Open tyronen opened this issue 3 years ago • 3 comments

Description

OpenAPI 3.1 dropped support for nullable. Instead, callers are expected to use array types:

# OpenAPI v3.0
type: string
nullable: true

# OpenAPI v3.1
type:
- "string"
- "null" 

Reproduction

For a type like the one above, the generated type is OneOf<[string, Record<string, never>]>.

As far as I can tell, Record<string, never> is returned when the tool cannot tell what type it should be generating for.

Expected result

The generated type should be OneOf<[string, null]>

Checklist

tyronen avatar Jul 07 '23 15:07 tyronen

Actually it appears the quotes around "null" are necessary; it won't work without it.

tyronen avatar Jul 10 '23 09:07 tyronen

Yes null is a valid YAML datatype, but not in JSON. It must be the literal string "null" as you described.

drwpow avatar Jul 10 '23 13:07 drwpow

Your issue was still valid, though. The next release of 6.3.4 will improve the 3.1 nullable handling considerably.

drwpow avatar Jul 10 '23 15:07 drwpow