Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kirpit opened this issue May 22, 2017 · 7 comments
Open

Comments

@kirpit
Copy link
Contributor

kirpit commented May 22, 2017

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
Suggest a Fix

Can help if the issue is addressed correctly.

@macjohnny
Copy link
Contributor

@kirpit recently, some changes regarding enums have been comitted. could you retry with the current master and close the issue if it is resolved?

@kirpit
Copy link
Contributor Author

kirpit commented Oct 25, 2017

Will do that a.s.a.p (that probably means sometime in the weekend). Thanks for the heads up!

@kenisteward
Copy link
Contributor

@kirpit I'm fixing this as a part of a PR right now for typescript-node. typescript-angular is already fixed i'd have to look into others as they all have different model generators :x.

kenisteward added a commit to kenisteward/swagger-codegen that referenced this issue Nov 1, 2017
@kirpit
Copy link
Contributor Author

kirpit commented Nov 4, 2017

Unfortunately the problem is still consist with minor improvement on typescript-angular (previously known as typescript-angular2) definition:

export interface MyData {
    myNumbersField?: Array<number>;  // <------ Definition looks like fixed
    myStringsField?: ArrayOfStringChoicesModel;
}

However, enum set of values are not there at all, so they have no effect.

Tried with typescript-angular, typescript-angularjs and typescript-fetch but not others, against current master 4dafa5b.

@kenisteward
Copy link
Contributor

kenisteward commented Nov 4, 2017 via email

@kenisteward
Copy link
Contributor

kenisteward commented Nov 4, 2017

@kirpit what you linked me is showing 2.2.3 not 2.3.0 snapahot try doing mvn clean package on current master. I know the arrayofstringchoices model is probably not fully correct but the other should be. Also my ts-node pr has not been merged yet #6862

@kirpit
Copy link
Contributor Author

kirpit commented Nov 4, 2017

It is 2.3.0, the latest commit content must have confused you.

$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar version
2.3.0-SNAPSHOT

And I already compiled with mvn clean package from the newly cloned latest master.

wing328 pushed a commit that referenced this issue Nov 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants