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

[BUG][Bounty][typescript-axios] Doesn't generate an union with null when type is an array (works in Stoplight)

Open darkbasic opened this issue 1 year ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [ ] Have you validated the input using an OpenAPI validator (example)? That domain has expired.
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

We use scramble to generate the OpenAPI 3.1 JSON schema and I want a certain field to be an union with null.

When scramble generates the following:

"requirements": {
  "type": [
    "array",
    "null"
  ],
  "items": {
    "type": "string"
  }
}

It doesn't translate into a typescript type which is an union with null:

export interface MappingItemResourceSettings {
    /**
     *
     * @type {{ [key: string]: string; }}
     * @memberof MappingItemResourceSettings
     */
    'validation'?: { [key: string]: string; };
    /**
     *
     * @type {Array<string>}
     * @memberof MappingItemResourceSettings
     */
    'requirements'?: Array<string>;
}

The expected output should be:

export interface MappingItemResourceSettings {
    /**
     *
     * @type {{ [key: string]: string; }}
     * @memberof MappingItemResourceSettings
     */
    'validation'?: { [key: string]: string; } | null;
    /**
     *
     * @type {Array<string>}
     * @memberof MappingItemResourceSettings
     */
    'requirements'?: Array<string> | null;
}
openapi-generator version

7.9.0 (AFAIK it's not a regression)

OpenAPI declaration file content or url
{
    "openapi": "3.1.0",
    "info": {
        "title": "API Portal Demo",
        "version": "1.0.0",
        "description": "This is the **mapping API** description"
    },
    "components": {
        "schemas": {
            "MappingItemResource": {
                "type": "object",
                "properties": {
                    "settings": {
                        "type": [
                            "object",
                            "null"
                        ],
                        "description": "Additional settings for mapping item validation. (JSON)",
                        "properties": {
                            "validation": {
                                "type": [
                                    "object",
                                    "null"
                                ],
                                "additionalProperties": {
                                    "type": "string"
                                }
                            },
                            "requirements": {
                                "type": [
                                    "array",
                                    "null"
                                ],
                                "items": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                },
                "required": [
                    "settings"
                ],
                "title": "MappingItemResource"
            },
        },
    }
}
Generation Details

openapi-generator-cli generate -i file.json -g typescript-axios -o src/api/mapping --additional-properties=withInterfaces=true,useSquareBracketsInArrayNames=true"

Steps to reproduce

openapi-generator-cli generate -i file.json -g typescript-axios -o src/api/mapping --additional-properties=withInterfaces=true,useSquareBracketsInArrayNames=true"

Related issues/PRs

https://github.com/dedoc/scramble/issues/70#issuecomment-2356592623

The author of Scramble thinks this is a valid OpenAPI format and the issue is in the openapi-generator typescript plugin. This affermation is backed by the fact that Stoplight correctly displays null in the union:

image

Suggest a fix

darkbasic avatar Oct 16 '24 16:10 darkbasic