Skip to content

Nullable ignored if non-null default is present #2144

Closed
@luhn

Description

@luhn

openapi-typescript version

7.6.1

Node.js version

23.7.0

OS + version

macOS 15.3

Description

Take the example property defined as "nullable" using OpenAPI 3.1 syntax.

"foo": {
    "type": ["string", "null"]
}

Results in:

foo?: string | null

However, if you add a non-null default:

"foo": {
    "type": ["string", "null"],
    "default": "bar"
}

It eliminates the null type:

foo?: string

This is problematic because my API has properties that have non-null defaults, but null is still a valid value to return.

I also tried with --default-non-nullable false but the behavior is the same.

This is very possibly the same issue as #2055, but using the 3.1 syntax.

Reproduction

{
	"openapi": "3.1.1",
	"info": {
		"title": "Test",
		"version": "local",
		"license": {
			"name": "Unlicensed",
			"identifier": "UNLICENSED"
		}
	},
	"servers": [
		{"url": "https://openapi-ts.dev"}
	],
	"security": [],
	"paths": {
		"/test": {
			"post": {
				"summary": "Test",
				"operationId": "test",
				"parameters": [],
				"responses": {
					"200": {
						"description": "Successful Response",
						"content": {
							"application/json": {
								"schema": {
									"$ref": "#/components/schemas/TestSchema"
								}
							}
						}
					},
					"404": {
						"description": "Not found!"
					}
				}
			},
		}
	},
	"components": {
		"schemas": {
			"TestSchema": {
				"properties": {
					"nullable": {
						"type": ["string", "null"]
					},
					"nullable_default": {
						"type": ["string", "null"],
						"default": "foo"
					}
				},
				"type": "object",
				"required": [],
				"title": "TestSchema"
			}
		}
	}
}

Gives (in part):

        TestSchema: {
            nullable?: string | null;
            /** @default foo */
            nullable_default?: string;
        };

Expected result

        TestSchema: {
            nullable?: string | null;
            /** @default foo */
            nullable_default?: string | null;
        };

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions