From eab9c72a42b65ed2b2bef2bdddf9c6bc9761f8dc Mon Sep 17 00:00:00 2001 From: Adrian Moos Date: Sat, 7 Oct 2017 15:42:46 +0200 Subject: [PATCH] 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 --- .../resources/typescript-angular/modelEnum.mustache | 9 ++------- .../typescript-angular/modelGeneric.mustache | 11 +++-------- .../typescript-angular-v2/default/model/order.ts | 6 +----- .../typescript-angular-v2/default/model/pet.ts | 6 +----- .../petstore/typescript-angular-v2/npm/model/order.ts | 6 +----- .../petstore/typescript-angular-v2/npm/model/pet.ts | 6 +----- .../with-interfaces/model/Order.ts | 6 +----- .../with-interfaces/model/Pet.ts | 6 +----- .../petstore/typescript-angular-v4/npm/model/order.ts | 6 +----- .../petstore/typescript-angular-v4/npm/model/pet.ts | 6 +----- 10 files changed, 13 insertions(+), 55 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular/modelEnum.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular/modelEnum.mustache index 056533539ac..a32e859196f 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular/modelEnum.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular/modelEnum.mustache @@ -1,7 +1,2 @@ -export enum {{classname}} { -{{#allowableValues}} -{{#enumVars}} - {{{name}}} = {{{value}}}{{^-last}},{{/-last}} -{{/enumVars}} -{{/allowableValues}} -} +export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}}; + diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular/modelGeneric.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular/modelGeneric.mustache index 2f96d576d43..a8b3c80e364 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular/modelGeneric.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular/modelGeneric.mustache @@ -16,13 +16,8 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{ export namespace {{classname}} { {{#vars}} {{#isEnum}} - export enum {{enumName}} { - {{#allowableValues}} - {{#enumVars}} - {{{name}}} = {{{value}}}{{^-last}},{{/-last}} - {{/enumVars}} - {{/allowableValues}} - } + export type {{enumName}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}}; {{/isEnum}} {{/vars}} -}{{/hasEnums}} \ No newline at end of file +}{{/hasEnums}} + diff --git a/samples/client/petstore/typescript-angular-v2/default/model/order.ts b/samples/client/petstore/typescript-angular-v2/default/model/order.ts index 402a86689c8..4639b8c2949 100644 --- a/samples/client/petstore/typescript-angular-v2/default/model/order.ts +++ b/samples/client/petstore/typescript-angular-v2/default/model/order.ts @@ -33,9 +33,5 @@ export interface Order { } export namespace Order { - export enum StatusEnum { - Placed = 'placed', - Approved = 'approved', - Delivered = 'delivered' - } + export type StatusEnum = 'placed' | 'approved' | 'delivered'; } diff --git a/samples/client/petstore/typescript-angular-v2/default/model/pet.ts b/samples/client/petstore/typescript-angular-v2/default/model/pet.ts index 0d6137d02cf..ce6c535b867 100644 --- a/samples/client/petstore/typescript-angular-v2/default/model/pet.ts +++ b/samples/client/petstore/typescript-angular-v2/default/model/pet.ts @@ -35,9 +35,5 @@ export interface Pet { } export namespace Pet { - export enum StatusEnum { - Available = 'available', - Pending = 'pending', - Sold = 'sold' - } + export type StatusEnum = 'available' | 'pending' | 'sold'; } diff --git a/samples/client/petstore/typescript-angular-v2/npm/model/order.ts b/samples/client/petstore/typescript-angular-v2/npm/model/order.ts index 402a86689c8..4639b8c2949 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/model/order.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/model/order.ts @@ -33,9 +33,5 @@ export interface Order { } export namespace Order { - export enum StatusEnum { - Placed = 'placed', - Approved = 'approved', - Delivered = 'delivered' - } + export type StatusEnum = 'placed' | 'approved' | 'delivered'; } diff --git a/samples/client/petstore/typescript-angular-v2/npm/model/pet.ts b/samples/client/petstore/typescript-angular-v2/npm/model/pet.ts index 0d6137d02cf..ce6c535b867 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/model/pet.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/model/pet.ts @@ -35,9 +35,5 @@ export interface Pet { } export namespace Pet { - export enum StatusEnum { - Available = 'available', - Pending = 'pending', - Sold = 'sold' - } + export type StatusEnum = 'available' | 'pending' | 'sold'; } diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/model/Order.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/model/Order.ts index 402a86689c8..4639b8c2949 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/model/Order.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/model/Order.ts @@ -33,9 +33,5 @@ export interface Order { } export namespace Order { - export enum StatusEnum { - Placed = 'placed', - Approved = 'approved', - Delivered = 'delivered' - } + export type StatusEnum = 'placed' | 'approved' | 'delivered'; } diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/model/Pet.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/model/Pet.ts index 0d6137d02cf..ce6c535b867 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/model/Pet.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/model/Pet.ts @@ -35,9 +35,5 @@ export interface Pet { } export namespace Pet { - export enum StatusEnum { - Available = 'available', - Pending = 'pending', - Sold = 'sold' - } + export type StatusEnum = 'available' | 'pending' | 'sold'; } diff --git a/samples/client/petstore/typescript-angular-v4/npm/model/order.ts b/samples/client/petstore/typescript-angular-v4/npm/model/order.ts index 402a86689c8..4639b8c2949 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/model/order.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/model/order.ts @@ -33,9 +33,5 @@ export interface Order { } export namespace Order { - export enum StatusEnum { - Placed = 'placed', - Approved = 'approved', - Delivered = 'delivered' - } + export type StatusEnum = 'placed' | 'approved' | 'delivered'; } diff --git a/samples/client/petstore/typescript-angular-v4/npm/model/pet.ts b/samples/client/petstore/typescript-angular-v4/npm/model/pet.ts index 0d6137d02cf..ce6c535b867 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/model/pet.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/model/pet.ts @@ -35,9 +35,5 @@ export interface Pet { } export namespace Pet { - export enum StatusEnum { - Available = 'available', - Pending = 'pending', - Sold = 'sold' - } + export type StatusEnum = 'available' | 'pending' | 'sold'; }