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

[BUG] [7.5.0] [typescript-fetch] generator sdk-ts generates enums in the model with object.values not supported by ES6

Open oliviermadelin opened this issue 1 year ago • 1 comments

Bug Report Checklist

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

Hi My context is an api build with spring-boot/maven and publishing a typescript sdk generated with openapi-generator-maven plugin

When upgrading from 7.4.0 to 7.5.0 , model generated for enums change: adding a new function "instanceOfEnum" using Object.values command (existing only for ES2017 or +)

But the compiler option in tsconfig.json still in ES6 (ES2015) and building sdk is impossible.

openapi-generator version

v7.5.0

OpenAPI declaration file content or url

configuration pom.xml please note using <supportsES6>true</supportsES6> to be standart with ES6

                        <configuration>
                            <inputSpec>${spec.folder.path}/api.yaml</inputSpec>
                            <generatorName>typescript-fetch</generatorName>
                            <generateSupportingFiles>true</generateSupportingFiles>
                            <output>${project.build.directory}/generated-sources/sdk-ts</output>
                            <skipIfSpecIsUnchanged>false</skipIfSpecIsUnchanged>
                            <configOptions>
                                <modelPropertyNaming>original</modelPropertyNaming>
                                <npmName>@abc/api-sdk-ts</npmName>
                                <npmRepository>https://repository/npm</npmRepository>
                                <npmVersion>${project.version}</npmVersion>
                                <supportsES6>true</supportsES6>
                                <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
                            </configOptions>
                            <typeMappings>BigDecimal=number,DateTime=string,Date=string,decimal=number</typeMappings>
                            <importMappings>Date=string,decimal=number</importMappings>
                        </configuration>
Generation Details

Generation of model typescript with command maven

mvn clean compile

Result is a file Etiquette.ts for my Enum named Etiquette. Please note the function instanceOf appears with version v7.5.0.

/**
 * 
 * @export
 */
export const Etiquette = {
    NOUVEAUTE: 'NOUVEAUTE',
    FAVORI: 'FAVORI'
} as const;
export type Etiquette = typeof Etiquette[keyof typeof Etiquette];


export function instanceOfEtiquette(value: any): boolean {
    return Object.values(Etiquette).includes(value);
}


Steps to reproduce

To build and compile the typescript sources to javascript use:

npm run build
> tsc && tsc -p tsconfig.esm.json

src/models/Etiquette.ts:28:19 - error TS2550: Property 'values' does not exist on type 'ObjectConstructor'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2017' or later.

28     return Object.values(Etiquette).includes(value);
                     ~~~~~~
Related issues/PRs

None found

Suggest a fix

Object.values is not supported by ES6/ES2015 The tsconfig.json contains compiler options.

I tried the suggestion : Try changing the 'lib' compiler option to 'es2017' or later. I modified the generated file tsconfig.json with "lib": ["es2017","dom"] or
"target": "es2017" or "target": "esnext" It works.

Presently, the file tsconfig.json generated by the plugin contains target "es6"

{
  "compilerOptions": {
    "declaration": true,
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist",
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "exclude": [
    "dist",
    "node_modules"
  ]
}

Is it a regression ? or should I change a config anywhere ?

Thanks in advance for support.

oliviermadelin avatar Apr 23 '24 11:04 oliviermadelin

This looks like a duplicate of https://github.com/OpenAPITools/openapi-generator/issues/18446

This bug also affects the typescript-axios generator.

ericcecchi avatar Apr 25 '24 19:04 ericcecchi

openapi-generator-maven plugin 7.6.0 brings the fix. The generated code is ES6 compatible.

before (7.5.0)

export function instanceOfEtiquette(value: any): boolean {
    return Object.values(Etiquette).includes(value);
}

after (7.6.0)

export function instanceOfEtiquette(value: any): boolean {
    for (const key in Etiquette) {
        if (Object.prototype.hasOwnProperty.call(Etiquette, key)) {
            if (Etiquette[key] === value) {
                return true;
            }
        }
    }
    return false;
}

oliviermadelin avatar Jun 12 '24 13:06 oliviermadelin