Skip to content

Commit

Permalink
represent swagger enums as union of literal types (swagger-api#6233)
Browse files Browse the repository at this point in the history
* 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 swagger-api#6206, swagger-api#5146, swagger-api#3500

* update samples

* restore blank lines at end of file

* fix typo
  • Loading branch information
bedag-moo authored and fvarose committed Oct 12, 2017
1 parent d135f50 commit 4c5cf54
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
export enum {{classname}} {
{{#allowableValues}}
{{#enumVars}}
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}};

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
export namespace {{classname}} {
{{#vars}}
{{#isEnum}}
export enum {{enumName}} {
{{#allowableValues}}
{{#enumVars}}
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
export type {{enumName}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}};
{{/isEnum}}
{{/vars}}
}{{/hasEnums}}
}{{/hasEnums}}

Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@ export interface Order {

}
export namespace Order {
export enum StatusEnum {
Placed = <any> 'placed',
Approved = <any> 'approved',
Delivered = <any> 'delivered'
}
export type StatusEnum = 'placed' | 'approved' | 'delivered';
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,5 @@ export interface Pet {

}
export namespace Pet {
export enum StatusEnum {
Available = <any> 'available',
Pending = <any> 'pending',
Sold = <any> 'sold'
}
export type StatusEnum = 'available' | 'pending' | 'sold';
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@ export interface Order {

}
export namespace Order {
export enum StatusEnum {
Placed = <any> 'placed',
Approved = <any> 'approved',
Delivered = <any> 'delivered'
}
export type StatusEnum = 'placed' | 'approved' | 'delivered';
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,5 @@ export interface Pet {

}
export namespace Pet {
export enum StatusEnum {
Available = <any> 'available',
Pending = <any> 'pending',
Sold = <any> 'sold'
}
export type StatusEnum = 'available' | 'pending' | 'sold';
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@ export interface Order {

}
export namespace Order {
export enum StatusEnum {
Placed = <any> 'placed',
Approved = <any> 'approved',
Delivered = <any> 'delivered'
}
export type StatusEnum = 'placed' | 'approved' | 'delivered';
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,5 @@ export interface Pet {

}
export namespace Pet {
export enum StatusEnum {
Available = <any> 'available',
Pending = <any> 'pending',
Sold = <any> 'sold'
}
export type StatusEnum = 'available' | 'pending' | 'sold';
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@ export interface Order {

}
export namespace Order {
export enum StatusEnum {
Placed = <any> 'placed',
Approved = <any> 'approved',
Delivered = <any> 'delivered'
}
export type StatusEnum = 'placed' | 'approved' | 'delivered';
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,5 @@ export interface Pet {

}
export namespace Pet {
export enum StatusEnum {
Available = <any> 'available',
Pending = <any> 'pending',
Sold = <any> 'sold'
}
export type StatusEnum = 'available' | 'pending' | 'sold';
}

0 comments on commit 4c5cf54

Please sign in to comment.