openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Enum generation: duplicate identifiers

Open DanDeMicco opened this issue 1 year ago • 0 comments

Description

A brief description of the bug. When generating enum values there is a possibility of name collision. For example, if an enum contains a list of timezone values, there may be collisions due to use of + and -.

Sample enum:

description: The default time zone setting for this entity.
type: string
example: America/New_York
enum:
  - Africa/Abidjan
  - Africa/Accra
  - Etc/GMT
  - Etc/GMT+0
  - Etc/GMT+1
  - Etc/GMT-1

this will result in a duplicate identifer being outputted:

Etc_GMT_1 = "Etc/GMT+1",
Etc_GMT_1 = "Etc/GMT-1",
Name Version
openapi-typescript 7.3.0
Node.js v18.20.4
OS + version macOS 14.6.1

Reproduction

How can this be reproduced / when did the error occur?

  1. create an enum value with timezone data above
npx openapi-typescript ./openapi.yaml -o ./src/typings/schema.ts --enum --alphabetize --dedupe-enums
  1. see output and notice duplicated value in enum

Expected result There should be no duplicated enums. One approach would be to surround in quotes if a special character is detected instead of replacing all special characters with _, or can have different replacement logic (e.g. + -> plus)

(in case it’s not obvious)

Checklist

DanDeMicco avatar Aug 22 '24 18:08 DanDeMicco