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

[BUG][typescript-fetch] 7.6.0 regression: TS7053 errors in generated instanceOf* methods

Open cascornelissen opened this issue 9 months 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)?
  • [x] 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

The changes introduced in OpenAPITools/openapi-generator#18418 result in TS7053 errors in the generated instanceOf* methods on our side.

TS7053: Element implicitly has an any type because expression of type string can't be used to index type typeof Criticality
No index signature with a parameter of type string was found on type typeof Criticality
image
openapi-generator version

7.6.0 fails, 7.5.0 has no issue

OpenAPI declaration file content or url
openapi: 3.0.0
components:
  schemas:
    # -- Snipped 300 lines of other schemas --
    Criticality:
      type: string
      enum:
        - error
        - warning
        - malfunction
Generation Details

We execute the following command and depend on @openapitools/openapi-generator-cli for the generation.

While we use the stringEnums option, removing that doesn't solve the issue so that option seems unrelated.

openapi-generator-cli --openapitools 'path/to/openapitools.json' generate --generator-key some-service

The openapitools.json then looks like this:

{
    "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
    "spaces": 4,
    "generator-cli": {
        "version": "7.6.0",
        "generators": {
            "some-service": {
                "additionalProperties": {
                    "inputSpec": "path/to/some-service.yaml",
                    "output": "path/to/some-app/src/js/types/openapi/some-service",
                    "generatorName": "typescript-fetch",
                    "stringEnums": true
                }
            }
        }
    }
}
Steps to reproduce
  1. Create an OpenAPI YAML specification containing a schema as shown above
  2. Use openapi-generator to generate TypeScript types, it does so succesfully
  3. Try to import/use the generated enum, TypeScript will throw a TS7053 error

Alternatively:

Related issues/PRs

None found so far.

Suggest a fix

Either revert the PR or adjust the the TypeScript, the following cast seems to solve the issue but is less than ideal;

if ((Criticality as Record<string, Criticality>)[key] === value) {

We have also noticed that setting strict to false in tsconfig.json resolves the issue which makes sense because TS7053 refers to an implicit any. Setting strict to true and noImplicitAny to false also seems to work.

cascornelissen avatar May 23 '24 13:05 cascornelissen