Skip to content

[BUG][TypeScript] Using oneOf with arrays generates code that does not compile #22779

@psegalen

Description

@psegalen

Bug Report Checklist

I'm sorry, being on a closed-source project, I can't share any code BUT I've found the exact lines of code that are involved in my problem so I think the fix should be really straigthforward.

Description

My openapi.json file contains a web service that accepts a filter parameter with a value using the oneOf property with several primitive types and an array of Dates.
Everything is correctly generated except for this particular filter's value file that contains code that can't be transpiled because it has a wrong syntax, mainly because of unbalanced parentheses but not only that.

openapi-generator version

7.19.0 (problem is there since 7.15.0, 7.14.0 generates a correct code)

OpenAPI declaration file content or url

Just a segment of it exposing the problem:

      "Filter" : {
        "type" : "object",
        "required" : [ "value", "columnId" ],
        "properties" : {
          "value" : {
            "oneOf" : [ {
              "type" : "boolean"
            }, {
              "type" : "integer",
              "format" : "int64"
            }, {
              "$ref" : "#/components/schemas/LocalDateTime"
            }, {
              "type" : "array",
              "items" : {
                "$ref" : "#/components/schemas/LocalDateTime"
              }
            } ]
          },
          "columnId" : {
            "type" : "string",
          }
        }
      },
Suggest a fix

The mustache template is incorrect, here are the lines involved in my problem, you'll see they're easy to fix since it's only syntax problems:

First issue is there: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache#L72
The following code is wrong:

    if (Array.isArray(json)) {
        if (json.every(item => !(isNaN(new Date(json).getTime()))) {
            return json.map(value => new Date(json);
        }
    }

Second line is missing a closing parenthese AND new Date(json) should be new Date(item) and third line is also missing a closing parenthese AND new Date(json) should be new Date(value).

Second issue is there: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache#L190
The following code is wrong:

    if (Array.isArray(value)) {
        if (value.every(item => item instanceof Date) {
            return value.map(value => value.toISOString().substring(0,10)));
        }
    }

Second line is missing a closing parenthese after Date and third one has an extra closing parenthese, also, there's a naming collision on the third line (value is the name of the array AND the name of an item).
There is the same issue with the code block that follows (lines 196 to 200).

Sorry for not posting a complete repro but if you look at the source, you'll quickly see the generated code can't transpile.

I'll prepare a PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions