swagger-api / swagger-api/swagger-codegen

[TypeScript-*] Generating array of enum values fails to create TS enums

Open
#5,681 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Basically trying to combine type: array and enum: [ ... ] within a single field, so we can limit the possible parameters to be passed, as seen in the official docs here.

There are two ways to do this; one is specifying directly in-place, another is to give reference to a definition, and the both cases fail similarly to create the enum values on TypeScript models. In other words, enum definitions within the swagger spec fades away and has no effect.

Swagger-codegen version

Latest master; 802641b.

Swagger declaration file content or url
swagger: "2.0"
info:
  version: "1.0"
  title: "Hello World API"
paths:
  /hello:
    get:
      parameters:
        - name: myData
          in: body
          schema:
            properties:
              myNumbersField:
                # In-place field spec:
                type: array
                items:
                  type: number
                  enum:
                    - 1
                    - 2
              myStringsField:
                # Reference to the definition: 
                $ref: '#/definitions/ArrayOfStringChoicesModel'
      responses:
        200:
          description: Returns the greeting.

definitions:
  ArrayOfStringChoicesModel:
    type: array
    items:
      type: string
      enum:
        - foo
        - bar

Example output (typescript/fetch):

export interface ArrayOfStringChoicesModel extends Array<string> {
}

export interface MyData {
    "myNumbersField"?: Array<number>;
    "myStringsField"?: ArrayOfStringChoicesModel;
}

That has no enum values defined at all.

Even though, I change the field type from array to any primitive, in-place enum definitions still disappear when generated:

  myNumbersField:
    type: number
    enum:
      - 1
      - 2

  ArrayOfStringChoicesModel:
    type: string
    enum:
      - foo
      - bar

Example output (typescript/angular2 this time):

export interface MyData {
    myNumbersField?: number; // <------ where are the numeric enums?
    myStringsField?: models.ArrayOfStringChoicesModel;
}

export enum ArrayOfStringChoicesModel {
    Foo = <any> 'foo',
    Bar = <any> 'bar'
}
Command line used for generation

Regular java -jar swagger-codegen-cli.jar generate -i spec.yml -l {language} call with any of the typescript/framework pair as far as I'm aware of:

  • typescript-angular2
  • typescript-angular
  • typescript-jquery
  • typescript-node
  • typescript-fetch
Steps to reproduce

Most convenient way to reproduce (perhaps with the latest stable) is swagger editor:

  1. Go to http://editor.swagger.io/
  2. Paste the above spec yaml
  3. Generate client / download any of the TypeScript choice.
  4. Alternatively, run the generate command with the given yaml spec
Related issues
  • This also reproduces the bug #3616 for typescript/angular2, which will be ignored here.
  • Not to be confused with already resolved issue #4577.
  • Might have weak relation to #2739.
Suggest a Fix

Can help if the issue is addressed correctly.

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 by running the provided Swagger 2.0 YAML through the swagger-codegen CLI for the listed TypeScript generators and compare the generated models with the examples. Trace TypeScript client generation from that command and verify that both inline and referenced array-item enums are preserved in the output for all affected generators.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.