OpenAPITools / OpenAPITools/openapi-generator

[BUG][TYPESCRIPT] typescript-angularjs enum

Open
#6,114 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Given an API with an enum:

foo:
  stype: string
  enum: [FOO, BAR]

the typescript-angularjs (and other typescript templates) generates enums such as:

export namespace Example {
  export enum FooEnum {
    FOO = <any> 'FOO',
    BAR = <any> 'BAR'
  }
}

I'm not sure about that <any> casting as it produces a different API than without it.

When compiling to .js/.d.ts the output is vastly different because of the <any> cast:

export namespace Example {
  export enum FooEnum {
    FOO,
    BAR
  }
}

This could be argued to be a bug in tsc but on the other hand I dont see the purpose of the casting and what the intended outcome is. By looking at just the type information both FOO and BAR can be anything so tsc is not entirely wrong either.

It also seems to differ with other typescript templates:

openapi-generator version

Tested 4.2.2 and 4.3.0, same issue.

OpenAPI declaration file content or url

openapi.yaml:

openapi: 3.0.0
info:
  title: foo
  version: 0.0.0
paths:
  /foo:
    post:
      requestBody:
        content:
          application/json;charset=utf-8:
            schema: {$ref: '#/components/schemas/Body'}
        required: true
      responses:
        '204':
          description: No Content
components:
  schemas:
    Body:
      type: object
      properties:
        foo:
          type: string
          enum: [FOO, BAR]

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "lib": ["es2017", "dom"],
    "rootDir": "./src",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true
  }
}
Command line used for generation
openapi-generator generate -g typescript-angularjs -i openapi.yaml -o src/typescript-angularjs
tsc
Steps to reproduce
  1. Create above files
  2. Run commands from above
  3. Check created files in dist/typescript-angularjs/model/Body.d.ts
Suggest a fix

I personally think the cast should be removed and I dont see what it intends to solve. I can gladly create a PR to fix this if someone can acknowledge the issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the typescript-angularjs modelEnum.mustache template and compare it with the typescript-axios and typescript-fetch templates; also inspect the typescript-node model.mustache cast. Run the supplied openapi.yaml generation commands and tsc, then verify the generated Body.d.ts enum output matches the intended TypeScript API without the problematic cast.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.