OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript] Array items are incorrectly marked as nullable

Open
#23,905 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Under some very specific conditions, | null is added to array item type when it shouldn't be there.

openapi-generator version

7.22.0

OpenAPI declaration file content or url
{
    "info": {
        "description": "Test",
        "title": "test",
        "version": "1.0"
    },
    "openapi": "3.1.0",
    "servers": [
        {
            "url": "http://localhost:5000"
        }
    ],
    "components": {
        "schemas": {
            "Response": {
                "title": "Response",
                "type": "object",
                "properties": {
                    "nullable": {
                        "type": "array",
                        "default": null,
                        "items": {
                            "anyOf": [
                                {
                                    "type": "string"
                                },
                                {
                                    "type": "null"
                                }
                            ]
                        }
                    }
                },
                "required": [
                    "nullable"
                ]
            },
            "ResponseGet": {
                "title": "ResponseGet",
                "type": "object",
                "properties": {
                    "nonNullable": {
                        "type": "array",
                        "default": null,
                        "items": {
                            "type": "string"
                        }
                    }
                },
                "required": [
                    "nonNullable"
                ]
            }
        }
    },
    "paths": {
        "/api": {
            "get": {
                "responses": {
                    "200": {
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ResponseGet"
                                }
                            }
                        },
                        "description": "OK"
                    }
                }
            }
        }
    }
}
Generation Details
{
    "generatorName": "typescript"
}
Steps to reproduce

When generating code for the above API spec, the Response class should have 'nullable': Array<string | null>; and the ResponseGet class should have 'nonNullable': Array<string>;. Instead, both receive Array<string | null>;. I'm unable to determine the exact cause; for example, changing "#/components/schemas/ResponseGet" -> "#/components/schemas/Response" causes both models to be generated correctly. Removing "default": null also seems to somehow fix the bug.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by generating the TypeScript client from the supplied OpenAPI declaration and compare the generated Response and ResponseGet model properties. Trace how the TypeScript generator combines array item schemas with default nullability, then add or update a regression test for this specification. Done means nullable uses Array<string | null> while nonNullable uses Array.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.