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

Generate true Typescript enums #1153

Closed
rnm-sxm opened this issue Jan 11, 2024 · 13 comments · Fixed by #1160
Closed

Generate true Typescript enums #1153

rnm-sxm opened this issue Jan 11, 2024 · 13 comments · Fixed by #1160
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested
Milestone

Comments

@rnm-sxm
Copy link
Contributor

rnm-sxm commented Jan 11, 2024

Orval does not generate true Typescript enums. Instead it generates const object with a set of readonly keys/values

What are the steps to reproduce this issue?

  1. Use orval to generate a Typecript file using this OpenAPI schema
openapi":  "3.0.3"
info:
  version: 0.1
  title: Enum Keyword Test
  description: Generated enums
paths:
  /color1:
    get:
      parameters:
      - in: query
        name: color1
        required: true
        schema:
          $ref: '#/components/schemas/Color1'
      responses:
        '200':
          description: OK
  /color2:
    get:
      parameters:
      - in: query
        name: color2
        required: true
        schema:
          $ref: '#/components/schemas/Color2'
      responses:
        '200':
          description: OK
components:
  schemas:
    Color1:
      type: string
      enum:
        - black
        - white
        - red
        - green
        - blue
    Color2:
      type: number
      enum:
        - 0
        - 10
        - 20
        - 30
        - 40
      x-enumNames:
        - black
        - white
        - red
        - green
        - blue

What happens?

The generated typescript includes these two enums:

export type Color2 = (typeof Color2)[keyof typeof Color2];

// eslint-disable-next-line @typescript-eslint/no-redeclare
export const Color2 = {
  black: 0,
  white: 10,
  red: 20,
  green: 30,
  blue: 40,
} as const;

export type Color1 = (typeof Color1)[keyof typeof Color1];

// eslint-disable-next-line @typescript-eslint/no-redeclare
export const Color1 = {
  black: "black",
  white: "white",
  red: "red",
  green: "green",
  blue: "blue",
} as const;

What were you expecting to happen?

Typescript Enums should be generated:

export enum Color2 {
  black = 0,
  white = 10,
  red = 20,
  green = 30,
  blue = 40,
};

export enum Color1 {
  black = "black",
  white = "white",
  red = "red",
  green = "green",
  blue = "blue",
};

Any logs, error output, etc?

none.

Any other comments?

none.

What versions are you using?

Operating System: Xubuntu 22.04
Package Version: "orval": "^6.23.0"
Browser Version: N/A

@melloware
Copy link
Collaborator

melloware commented Jan 11, 2024

This was added in 6.21.0: #907

Is it not working for you?

#1058

@melloware melloware added the question Further information is requested label Jan 11, 2024
@rnm-sxm
Copy link
Contributor Author

rnm-sxm commented Jan 11, 2024

Thanks for pointing this out. This option is apparently not enabled by default (I'm using CLI), and is not documented on the main web site.

@melloware
Copy link
Collaborator

melloware commented Jan 11, 2024

PR for documentation update would be great! cc @soartec-lab

@melloware melloware added the documentation Improvements or additions to documentation label Jan 11, 2024
@rnm-sxm
Copy link
Contributor Author

rnm-sxm commented Jan 11, 2024

There is still a bug related to the way the x-enumNames is used. It ends up generating enums with colons (Color2) instead of equals:

export enum Color2 {
  black: 0,
  white: 10,
  red: 20,
  green: 30,
  blue: 40,

}
export enum Color1 {
  black= 'black',
  white= 'white',
  red= 'red',
  green= 'green',
  blue= 'blue',
}

@melloware
Copy link
Collaborator

PR welcome

@melloware
Copy link
Collaborator

Cc @dsthode who made this change.

@soartec-lab
Copy link
Member

Thank you for sharing this issue. I will also check the PR. looking forward to it 🙌

@rnm-sxm
Copy link
Contributor Author

rnm-sxm commented Jan 12, 2024

The fix appears to be a one-liner, but I'm not familiar with this project - I don't know how to build it.

packages/core/src/getters/enum.ts, line 93 should be

        `  ${keyword.isIdentifierNameES5(name) ? name : `'${name}'`}= ${val},\n`

@melloware
Copy link
Collaborator

@soartec-lab are you submitting this PR or is @rnm-sxm ?

@melloware melloware added this to the 6.24.0 milestone Jan 12, 2024
@soartec-lab
Copy link
Member

soartec-lab commented Jan 12, 2024

No, I haven't started yet.

@rnm-sxm

If you don't, I can also respond to this next to the other responses.

@soartec-lab
Copy link
Member

@melloware

I can move forward with this response once the current response is finished, but out of respect for @rnm-sxm who reported the issue, I'll wait for a moment for a response 👍

@melloware
Copy link
Collaborator

OK I assigned to both of you and you guys can work it out!

@rnm-sxm
Copy link
Contributor Author

rnm-sxm commented Jan 12, 2024

@soartec-lab Thanks for taking this over.

@rnm-sxm rnm-sxm removed their assignment Jan 12, 2024
melloware added a commit that referenced this issue Jan 16, 2024
anymaniax pushed a commit that referenced this issue Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants