Skip to content

Commit eab9c72

Browse files
bedag-moowing328
authored andcommitted
represent swagger enums as union of literal types (#6233)
* represent swagger enums as union of literal types enabling their easy use in angular templates, structural subtyping among enums (in particular, different instances of the same enum are now mutually assignable), improving type safety by preventing incorrect widening, and permitting numeric enum values (albeit without descriptive names) Fixes #6206, #5146, #3500 * update samples * restore blank lines at end of file * fix typo
1 parent cc756b5 commit eab9c72

File tree

10 files changed

+13
-55
lines changed

10 files changed

+13
-55
lines changed
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
export enum {{classname}} {
2-
{{#allowableValues}}
3-
{{#enumVars}}
4-
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
5-
{{/enumVars}}
6-
{{/allowableValues}}
7-
}
1+
export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}};
2+

modules/swagger-codegen/src/main/resources/typescript-angular/modelGeneric.mustache

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
1616
export namespace {{classname}} {
1717
{{#vars}}
1818
{{#isEnum}}
19-
export enum {{enumName}} {
20-
{{#allowableValues}}
21-
{{#enumVars}}
22-
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
23-
{{/enumVars}}
24-
{{/allowableValues}}
25-
}
19+
export type {{enumName}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}};
2620
{{/isEnum}}
2721
{{/vars}}
28-
}{{/hasEnums}}
22+
}{{/hasEnums}}
23+

samples/client/petstore/typescript-angular-v2/default/model/order.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,5 @@ export interface Order {
3333

3434
}
3535
export namespace Order {
36-
export enum StatusEnum {
37-
Placed = <any> 'placed',
38-
Approved = <any> 'approved',
39-
Delivered = <any> 'delivered'
40-
}
36+
export type StatusEnum = 'placed' | 'approved' | 'delivered';
4137
}

samples/client/petstore/typescript-angular-v2/default/model/pet.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,5 @@ export interface Pet {
3535

3636
}
3737
export namespace Pet {
38-
export enum StatusEnum {
39-
Available = <any> 'available',
40-
Pending = <any> 'pending',
41-
Sold = <any> 'sold'
42-
}
38+
export type StatusEnum = 'available' | 'pending' | 'sold';
4339
}

samples/client/petstore/typescript-angular-v2/npm/model/order.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,5 @@ export interface Order {
3333

3434
}
3535
export namespace Order {
36-
export enum StatusEnum {
37-
Placed = <any> 'placed',
38-
Approved = <any> 'approved',
39-
Delivered = <any> 'delivered'
40-
}
36+
export type StatusEnum = 'placed' | 'approved' | 'delivered';
4137
}

samples/client/petstore/typescript-angular-v2/npm/model/pet.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,5 @@ export interface Pet {
3535

3636
}
3737
export namespace Pet {
38-
export enum StatusEnum {
39-
Available = <any> 'available',
40-
Pending = <any> 'pending',
41-
Sold = <any> 'sold'
42-
}
38+
export type StatusEnum = 'available' | 'pending' | 'sold';
4339
}

samples/client/petstore/typescript-angular-v2/with-interfaces/model/Order.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,5 @@ export interface Order {
3333

3434
}
3535
export namespace Order {
36-
export enum StatusEnum {
37-
Placed = <any> 'placed',
38-
Approved = <any> 'approved',
39-
Delivered = <any> 'delivered'
40-
}
36+
export type StatusEnum = 'placed' | 'approved' | 'delivered';
4137
}

samples/client/petstore/typescript-angular-v2/with-interfaces/model/Pet.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,5 @@ export interface Pet {
3535

3636
}
3737
export namespace Pet {
38-
export enum StatusEnum {
39-
Available = <any> 'available',
40-
Pending = <any> 'pending',
41-
Sold = <any> 'sold'
42-
}
38+
export type StatusEnum = 'available' | 'pending' | 'sold';
4339
}

samples/client/petstore/typescript-angular-v4/npm/model/order.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,5 @@ export interface Order {
3333

3434
}
3535
export namespace Order {
36-
export enum StatusEnum {
37-
Placed = <any> 'placed',
38-
Approved = <any> 'approved',
39-
Delivered = <any> 'delivered'
40-
}
36+
export type StatusEnum = 'placed' | 'approved' | 'delivered';
4137
}

samples/client/petstore/typescript-angular-v4/npm/model/pet.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,5 @@ export interface Pet {
3535

3636
}
3737
export namespace Pet {
38-
export enum StatusEnum {
39-
Available = <any> 'available',
40-
Pending = <any> 'pending',
41-
Sold = <any> 'sold'
42-
}
38+
export type StatusEnum = 'available' | 'pending' | 'sold';
4339
}

0 commit comments

Comments
 (0)