cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Generate enum as export type #313

Closed ThomasDevGif closed 9 months ago

ThomasDevGif commented 9 months ago

Hello,

I have a Java api and I use swagger-maven-plugin to generate an api-file.yaml. Then I use ng-openapi-gen and the api-file.yaml to generate the api in my angular application.

The problem is that I cannot reuse my Java enum as type in my angular application.

Here is the api-file.yaml content :

openapi: 3.0.1

paths:
  /users:
    get:
      operationId: getUser
      responses:
        default:
          description: default response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/UserDto'
  /users/{userId}/etiquettes:
    get:
      operationId: getEtiquettes
      parameters:
      - name: userId
        in: path
        required: true
        schema:
          type: integer
          format: int64
      responses:
        default:
          description: default response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/EtiquetteDto'
components:
  schemas:
    UserDto:
      type: object
      properties:
        userId:
          type: string
        etiquettes:
          type: array
          items:
            type: string
            enum:
              - WHITE
              - GREY
              - BLACK
              - RED
              - GREEN
              - BLUE
              - YELLOW
              - ORANGE
    EtiquetteDto:
      type: object
      properties:
        color:
          type: string
          enum:
          - WHITE
          - GREY
          - BLACK
          - RED
          - GREEN
          - BLUE
          - YELLOW
          - ORANGE
        name:
          type: string

And the generated models using ng-openapi-gen :

export interface EtiquetteDto {
  color?: 'WHITE' | 'GREY' | 'BLACK' | 'RED' | 'GREEN' | 'BLUE' | 'YELLOW' | 'ORANGE';
  name?: string;
}

export interface UserDto {
  etiquettes?: Array<'WHITE' | 'GREY' | 'BLACK' | 'RED' | 'GREEN' | 'BLUE' | 'YELLOW' | 'ORANGE'>;
  userId?: string;
}

Is there any way to also generate an export type Color so I can use it in the angular application ?

Thanks in advance,

ThomasDevGif commented 9 months ago

The problem was before the yaml generation. I had to add the swagger annotations @Schema(enumAsRef = true) in each enum file.