ng-openapi-gen icon indicating copy to clipboard operation
ng-openapi-gen copied to clipboard

New enumStyle: raw

Open Leonardonline opened this issue 3 years ago • 6 comments

It is possible to add an option for generate enum with string as is? Like alias option that generate only type, but generating enum instead (that can be used runtime)

Leonardonline avatar Feb 24 '22 10:02 Leonardonline

Sorry, I didn't understand your question... Enums defined as top-level schemes are already supported. For inline enums, we always just output the possible values as an union type. There were many other requests for this, but it just out of scope.

luisfpg avatar Mar 25 '22 14:03 luisfpg

Sorry for the unclear request!

My request would be to add an option to enumStyle (in the ng-openapi-gen-schema.json) to generate an enum (not a type) with the strings as they are without changing the case of the characters (as with the alias option that generate a type).

Leonardonline avatar Mar 29 '22 13:03 Leonardonline

I'm still not sure I understand. Can you give an example on what do you expect the output would look like?

luisfpg avatar Mar 29 '22 14:03 luisfpg

I'm very sorry for my bad explanation!

Here an example of the current and requested output

// SOURCE JSON
"ScanStatus" : {
    "enum" : [
        "Ok",
        "QRWithRef",
        "HUBError"
    ],
    "type" : "string"
},
With "enumStyle" : "alias" will generate
export type ScanStatus = 'Ok' | 'QRWithRef' | 'HUBError'; 

Values are correct but type can't be used at runtime.

With "enumStyle" : "pascal" will generate
export enum ScanStatus {
  Ok = 'Ok',
  QrWithRef = 'QRWithRef', // key dosen't match value
  HubError = 'HUBError' // key dosen't match value
}

QrWithRef !== QRWithRef

The requested behaviour will generate enum like this:
export enum ScanStatus {
  Ok = 'Ok',
  QRWithRef = 'QRWithRef', 
  HUBError = 'HUBError'
}

Leonardonline avatar Mar 30 '22 06:03 Leonardonline

Ah, now I got it. I'd propose adding then an enum style 'raw' which does no case modifications. However, nowadays I have minimal time to work on this project. If you're willing to submit a PR, it would be appreciated. Basically you'd need to change the ng-openapi-gen-schema.json file to describe the new option, and change the enumName function in gen-utils.ts to handle that case (if 'upper' else if 'raw' else /* pascal */). Also add a test to it.

luisfpg avatar Mar 30 '22 10:03 luisfpg

Hi @Leonardonline, as enums will be opaque, i.e. there is no way to know the value of an enum during the compilation time, there is a better alternative see my answer here

Instead of generating a TS enum

const enum Color {
    Red = 'Red',
    Green = "Green"
}

Which would be a breaking change for the existing code, it is much better to generate a custom enum:

export const ColorType = {
  RED: 'Red',
  GREEN: 'Green',
} as const;

export type ColorType = typeof ColorType[keyof typeof ColorType];

This way you can do both of the following:

const color: ColorType= "Green"; // compatible with the current string union way
// And if you need to create a color from the enum like value:
const anotherColor: ColorType = ColorType.RED;

Prisma chose this way of generating enums instead of the TS enums one.

Xample avatar May 11 '22 09:05 Xample