OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.58k stars 6.52k forks source link

[typescript-angular] Allow string literal unions instead of string enums #12718

Open StefanKern opened 2 years ago

StefanKern commented 2 years ago

Is your feature request related to a problem? Please describe.

While the typescript interfaces generated for the model can be fully compatible with client side models, this is not the case with enums as different string enums cannot be compatible on the client side. Typescript enums are problematic, and the typescript community has a better solution.

Implemented this would allow the client side models and server side models to be entirely decoupled, as enums are the only thing standing in the way of that at the moment.

I realize that many people likely use the server models directly, but a flexible approach would be preferable.

Describe the solution you'd like

The typescript-angular and typescript-axios generator actually already implements this by default (stringEnums = false).

See this recent merge request: https://github.com/OpenAPITools/openapi-generator/pull/11368

It creates an object value enum as such:

export interface Model {
  type: Model.TypeEnum;
}
export namespace Model{
  export type TypeEnum = 'ONE' | 'TWO' | 'THREE';
  export const TypeEnum = {
    ONE: 'ONE' as TypeEnum,
    TWO: 'TWO' as TypeEnum,
    THREE: 'THREE' as TypeEnum,
  } as const;
}

--> This title and description is a copy of [REQ][typescript-fetch] Allow string literal unions instead of string enums, which already describes it perfectly and also applies this typescript generator.

Addition: current output of option stringEnum

export interface Model {
  type: EnumSample;
}
export enum EnumSample {
  ONE = 'ONE',
  TWO= 'TWO',
  THREE= 'THREE'
}
inkassso commented 3 months ago

What is the issue here? The typescript-angular generator already supports this, the option stringEnums is false by default, which generates the enum in the form of a union type and an accompanied object with the string literals.