Openapi-generator: [typescript] support for enum

Created on 27 Jul 2018  路  9Comments  路  Source: OpenAPITools/openapi-generator

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.

TypeScript

Most helpful comment

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.

All 9 comments

@jmini can you give an example?

@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;
    }
}

@jmini

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

I guess this is typescript-fetch specific, maybe https://github.com/OpenAPITools/openapi-generator/pull/569 by @Place1 resolves this issue?

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

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:

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.

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

Looks like this will be merged for the 4.0.0 milestone https://github.com/OpenAPITools/openapi-generator/pull/2266

That's great! Thanks for your efforts.

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

Was this page helpful?
0 / 5 - 0 ratings