[BUG] [typescript] [axios, Fetch, Nestjs] AdditionalProperties Array Of Enums References Invalid Enum Name 'inner'

by ADMIN 115 views

BUG: AdditionalProperties Array of Enums References Invalid Enum Name 'inner'

This article discusses a bug in the OpenAPI Generator, specifically when generating TypeScript models with additional properties that are arrays of enums. The bug causes the generated code to reference an invalid enum name, resulting in compilation errors.

The bug is triggered when the OpenAPI declaration file contains a schema with an additional property that is an array of enums. The generated TypeScript code will contain a reference to an enum that does not exist, causing a compilation error.

OpenAPI Declaration File Content

The following is an example of an OpenAPI declaration file that triggers the bug:

"components": {
    "schemas": {
      "TestResponse": {
        "type": "object",
        "properties": {
          "test": {
            "type": "object",
            "additionalProperties": {
              "type": "array",
              "items": {
                "type": "string",
                "enum": ["TEST"]
              }
            }
          }
        }
      }
    }
  }

Generation Details

The bug can be reproduced by running the OpenAPI Generator with the following command:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
   -i https://gist.githubusercontent.com/shaun-jacks/7867ddaa45841edfc37ce4079c7693a4/raw/36a3484905383fa5f98c5032349e6f488a535314/gistfile1.txt \
   -g typescript-nestjs \
   -o ./typescript-nestjs

Actual Output

The generated TypeScript code will contain a reference to an invalid enum name, resulting in a compilation error:

/**
 * Test title
 * Test description
 *
 * The version of the OpenAPI document: version
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


export interface TestResponse { 
    test?: { [key: string]: Array<InnerEnum>; };
}
export namespace TestResponse {
    export type TestEnum = 'TEST';
    export const TestEnum = {
        Test: 'TEST' as TestEnum
    };
}

Notice how the TestResponse interface contains a property test with a type of Array<InnerEnum>, but the InnerEnum type is not defined anywhere.

Expected Output

The expected output is a TypeScript code that correctly references the enum name:

/**
 * Test title
 * Test description
 *
 * The version of the OpenAPI document: version
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


export interface TestResponse { 
    test?: { [key: string]: Array<TestResponse.TestEnum>; };
}
export namespace TestResponse {
    export type TestEnum = 'TEST';
    export const TestEnum = {
        Test: 'TEST' as TestEnum
    };
}

Related Issues/PRs

This issue is related to the following GitHub issue: https://github.com/openapitools/openapi-generator/issues/20790

Suggest a Fix

One possible fix for this issue is to modify the OpenAPI Generator to correctly handle additional properties that are arrays of enums. This can be achieved by adding a check to ensure that the enum name is valid before generating the code.

Debug Models

When running with debugModels, the OpenAPI Generator will output additional information that can help diagnose the issue. For example:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
   -i https://gist.githubusercontent.com/shaun-jacks/7867ddaa45841edfc37ce4079c7693a4/raw/36a3484905383fa5f98c5032349e6f488a535314/gistfile1.txt \
   -g typescript-nestjs \
   -o ./typescript-nestjs --global-property debugModels

The output will contain information about the additional property, including the enum name:

"mostInnerItems" : {
        "openApiType" : "string",
        "baseName" : "inner",
        "getter" : "getInner",
        "setter" : "setInner",
        "dataType" : "string",
        "datatypeWithEnum" : "InnerEnum",
        "name" : "inner",
        "defaultValue" : "undefined",
        "defaultValueWithParam" : " = data.inner;",
        "baseType" : "string",
        "example" : "null",
        "jsonSchema" : "{\n  \"enum\" : [ \"TEST\" ],\n  \"type\" : \"string\"\n}",
        "exclusiveMinimum" : false,
        "exclusiveMaximum" : false,
        "required" : false,

Notice how the baseName is inner, and datatypeWithEnum is InnerEnum. This information can be used to diagnose the issue and suggest a fix.
Q&A: AdditionalProperties Array of Enums References Invalid Enum Name 'inner'

A: The issue is that the OpenAPI Generator is generating TypeScript code that references an invalid enum name when dealing with additional properties that are arrays of enums.

A: The expected behavior is that the OpenAPI Generator should correctly reference the enum name in the generated TypeScript code.

A: The cause of the issue is that the OpenAPI Generator is not correctly handling additional properties that are arrays of enums.

A: You can reproduce the issue by running the OpenAPI Generator with the following command:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
   -i https://gist.githubusercontent.com/shaun-jacks/7867ddaa45841edfc37ce4079c7693a4/raw/36a3484905383fa5f98c5032349e6f488a535314/gistfile1.txt \
   -g typescript-nestjs \
   -o ./typescript-nestjs

A: The actual output is a TypeScript code that references an invalid enum name:

/**
 * Test title
 * Test description
 *
 * The version of the OpenAPI document: version
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


export interface TestResponse { 
    test?: { [key: string]: Array<InnerEnum>; };
}
export namespace TestResponse {
    export type TestEnum = 'TEST';
    export const TestEnum = {
        Test: 'TEST' as TestEnum
    };
}

A: The expected output is a TypeScript code that correctly references the enum name:

/**
 * Test title
 * Test description
 *
 * The version of the OpenAPI document: version
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


export interface TestResponse { 
    test?: { [key: string]: Array<TestResponse.TestEnum>; };
}
export namespace TestResponse {
    export type TestEnum = 'TEST';
    export const TestEnum = {
        Test: 'TEST' as TestEnum
    };
}

A: One possible fix for this issue is to modify the OpenAPI Generator to correctly handle additional properties that are arrays of enums. This can be achieved by adding a check to ensure that the enum name is valid before generating the code.

A: This issue is related to the GitHub issue #20790, which is a similar issue with the OpenAPI Generator.

A: You can get more information about the issue by running the OpenAPI Generator with the --global-property debugModels flag:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
   -i https://gist.githubusercontent.com/shaun-jacks/7867ddaa45841edfc37ce4079c7693a4/raw/36a3484905383fa5f98c5032349e6f488a535314/gistfile1.txt \
   -g typescript-nestjs \
   -o ./typescript-nestjs --global-property debugModels

This will output additional information about the issue, including the enum name.