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] support for enum #665

Closed
jmini opened this issue Jul 27, 2018 · 9 comments · Fixed by #7529
Closed

[typescript] support for enum #665

jmini opened this issue Jul 27, 2018 · 9 comments · Fixed by #7529

Comments

@jmini
Copy link
Member

jmini commented Jul 27, 2018

Description

Enum are not correctly generated in typescript client. Some investigation are requested to understand what is going wrong.

See also: https://www.typescriptlang.org/docs/handbook/enums.html

CC: @kayhuba

openapi-generator version

3.1.2

Command line used for generation

CLI to use the typescript-node generator.

@macjohnny
Copy link
Member

@jmini can you give an example?

@jmini
Copy link
Member Author

jmini commented Jul 30, 2018

@macjohnny thank you for asking... I wanted to do it but I forgot.

I have the feeling our problem is for enum at top level. This is well supported in Java (client/server). My colleague reported it to me as broken in Typescript.

openapi: 3.0.1
info:
  title: issue 665 test
  version: '1.0'
servers:
  - url: 'http://localhost:8000/'
paths:
  /random:
    get:
      operationId: randomOp
      responses:
        '201':
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SomeObj"
components:
  schemas:
    SomeObj:
      type: object
      properties:
        id:
          type: integer
          format: int64
        color:
          $ref: "#/components/schemas/Color"
    Color:
      type: string
      enum:
        - red
        - green
        - yellow
        - other

Generated color.ts class:

/**
 * issue 665 test
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * OpenAPI spec version: 1.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


export class Color {

    static discriminator: string | undefined = undefined;

    static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
    ];

    static getAttributeTypeMap() {
        return Color.attributeTypeMap;
    }
}

@macjohnny
Copy link
Member

@jmini

I have the feeling our problem is for enum at top level

I guess this is typescript-fetch specific, maybe #569 by @Place1 resolves this issue?

I can confirm that using enums in models works properly with the typescript-angular generator.

@jmini
Copy link
Member Author

jmini commented Jul 31, 2018

This is not solved at all by #569.

But this is probably because /openapi-generator/src/main/resources/typescript-node/model.mustache seems old compared to other typescript generator templates:

@christof-vollrath-spring-media
Copy link
Contributor

Same problem here.

For typescript-node the generator does not correctly generate typescript enums if the enum is defined in OpenApi on the top level.
In this case instead of generating an enum, a class is generated with the name of the enum and no attributes (see the example of jmini).

On the other side if it the enum is defined inside an object, everything works fine.

The bug seems to be in typescript-node/model.mustache. The template should make a difference between objects and enums, which it doesn't.

The fix shouldn't be to difficult, I am trying to fix it and making a pull request.

@ghost
Copy link

ghost commented Mar 11, 2019

@christof-vollrath-spring-media your fix worked for me. Any plans to get a PR merged into this repo?

@christof-vollrath-spring-media
Copy link
Contributor

Looks like this will be merged for the 4.0.0 milestone #2266

@ghost
Copy link

ghost commented Mar 12, 2019

That's great! Thanks for your efforts.

macjohnny pushed a commit that referenced this issue Jul 15, 2019
* [typescript-node] fixed enum generator for top level enums #665

* [typescript-node] fixed enum generator for top level enums in models.mustache #665

* Remove wrong copyright line
@macjohnny
Copy link
Member

fixed with #2266
thanks @christof-vollrath-spring-media

macjohnny pushed a commit that referenced this issue Sep 28, 2020
This fixes #665 for the consolidated typescript generator.
Original fix for typescript-node was in PR #2266, merged as
8417c5b in version 4.1.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants