diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 46b31091bc7..7a70db9363f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -2508,7 +2508,13 @@ public CodegenParameter fromParameter(Parameter param, Set imports) { // set boolean flag (e.g. isString) setParameterBooleanFlagWithCodegenProperty(p, cp); - p.dataType = cp.datatype; + String parameterDataType = this.getParameterDataType(param, property); + if (parameterDataType != null) { + p.dataType = parameterDataType; + } else { + p.dataType = cp.datatype; + } + p.dataFormat = cp.dataFormat; if(cp.isEnum) { p.datatypeWithEnum = cp.datatypeWithEnum; @@ -2719,6 +2725,17 @@ public CodegenParameter fromParameter(Parameter param, Set imports) { return p; } + /** + * Returns the data type of a parameter. + * Returns null by default to use the CodegenProperty.datatype value + * @param parameter + * @param property + * @return + */ + protected String getParameterDataType(Parameter parameter, Property property) { + return null; + } + public boolean isDataTypeBinary(String dataType) { if (dataType != null) { return dataType.toLowerCase().startsWith("byte"); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java index a529711a5cf..0192c7835a6 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java @@ -10,19 +10,11 @@ import java.util.Map; import java.util.Set; -import io.swagger.codegen.CliOption; -import io.swagger.codegen.CodegenModel; -import io.swagger.codegen.CodegenParameter; -import io.swagger.codegen.CodegenOperation; -import io.swagger.codegen.SupportingFile; +import io.swagger.codegen.*; import io.swagger.codegen.utils.SemVer; import io.swagger.models.ModelImpl; -import io.swagger.models.properties.ArrayProperty; -import io.swagger.models.properties.BooleanProperty; -import io.swagger.models.properties.FileProperty; -import io.swagger.models.properties.MapProperty; -import io.swagger.models.properties.ObjectProperty; -import io.swagger.models.properties.Property; +import io.swagger.models.parameters.Parameter; +import io.swagger.models.properties.*; public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCodegen { private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm"); @@ -174,9 +166,102 @@ public String getTypeDeclaration(Property p) { return "Blob"; } else if (p instanceof ObjectProperty) { return "any"; - } else { - return super.getTypeDeclaration(p); } + + return super.getTypeDeclaration(p); + } + + @Override + protected String getParameterDataType(Parameter parameter, Property p) { + Property inner; + if (p instanceof ArrayProperty) { + ArrayProperty mp1 = (ArrayProperty) p; + inner = mp1.getItems(); + return this.getSwaggerType(p) + "<" + this.getParameterDataType(parameter, inner) + ">"; + } else if (p instanceof MapProperty) { + MapProperty mp = (MapProperty) p; + inner = mp.getAdditionalProperties(); + return "{ [key: string]: " + this.getParameterDataType(parameter, inner) + "; }"; + } else if (p instanceof StringProperty) { + // Handle string enums + StringProperty sp = (StringProperty) p; + if (sp.getEnum() != null) { + return enumValuesToEnumTypeUnion(sp.getEnum(), "string"); + } + } else if (p instanceof IntegerProperty) { + // Handle integer enums + IntegerProperty sp = (IntegerProperty) p; + if (sp.getEnum() != null) { + return numericEnumValuesToEnumTypeUnion(new ArrayList(sp.getEnum())); + } + } else if (p instanceof LongProperty) { + // Handle long enums + LongProperty sp = (LongProperty) p; + if (sp.getEnum() != null) { + return numericEnumValuesToEnumTypeUnion(new ArrayList(sp.getEnum())); + } + } else if (p instanceof DoubleProperty) { + // Handle double enums + DoubleProperty sp = (DoubleProperty) p; + if (sp.getEnum() != null) { + return numericEnumValuesToEnumTypeUnion(new ArrayList(sp.getEnum())); + } + } else if (p instanceof FloatProperty) { + // Handle float enums + FloatProperty sp = (FloatProperty) p; + if (sp.getEnum() != null) { + return numericEnumValuesToEnumTypeUnion(new ArrayList(sp.getEnum())); + } + } else if (p instanceof DateProperty) { + // Handle date enums + DateProperty sp = (DateProperty) p; + if (sp.getEnum() != null) { + return enumValuesToEnumTypeUnion(sp.getEnum(), "string"); + } + } else if (p instanceof DateTimeProperty) { + // Handle datetime enums + DateTimeProperty sp = (DateTimeProperty) p; + if (sp.getEnum() != null) { + return enumValuesToEnumTypeUnion(sp.getEnum(), "string"); + } + } + return this.getTypeDeclaration(p); + } + + /** + * Converts a list of strings to a literal union for representing enum values as a type. + * Example: 'available' | 'pending' | 'sold' + * + * @param values list of allowed enum values + * @param dataType either "string" or "number" + * @return + */ + protected String enumValuesToEnumTypeUnion(List values, String dataType) { + StringBuilder b = new StringBuilder(); + boolean isFirst = true; + for (String value: values) { + if (!isFirst) { + b.append(" | "); + } + b.append(toEnumValue(value.toString(), dataType)); + isFirst = false; + } + return b.toString(); + } + + /** + * Converts a list of numbers to a literal union for representing enum values as a type. + * Example: 3 | 9 | 55 + * + * @param values + * @return + */ + protected String numericEnumValuesToEnumTypeUnion(List values) { + List stringValues = new ArrayList<>(); + for (Number value: values) { + stringValues.add(value.toString()); + } + return enumValuesToEnumTypeUnion(stringValues, "number"); } @Override diff --git a/samples/client/petstore/typescript-angular-v2/default/.swagger-codegen/VERSION b/samples/client/petstore/typescript-angular-v2/default/.swagger-codegen/VERSION index 50794f17f1a..2bf1c1ccf36 100644 --- a/samples/client/petstore/typescript-angular-v2/default/.swagger-codegen/VERSION +++ b/samples/client/petstore/typescript-angular-v2/default/.swagger-codegen/VERSION @@ -1 +1 @@ -2.3.1-SNAPSHOT \ No newline at end of file +2.3.1 diff --git a/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts index 3f5b8fd227e..30354b57437 100644 --- a/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts @@ -96,7 +96,7 @@ export class PetService { * @summary Finds Pets by status * @param status Status values that need to be considered for filter */ - public findPetsByStatus(status: Array, extraHttpRequestParams?: RequestOptionsArgs): Observable> { + public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: RequestOptionsArgs): Observable> { return this.findPetsByStatusWithHttpInfo(status, extraHttpRequestParams) .map((response: Response) => { if (response.status === 204) { @@ -305,7 +305,7 @@ export class PetService { * @param status Status values that need to be considered for filter */ - public findPetsByStatusWithHttpInfo(status: Array, extraHttpRequestParams?: RequestOptionsArgs): Observable { + public findPetsByStatusWithHttpInfo(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: RequestOptionsArgs): Observable { if (status === null || status === undefined) { throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.'); } diff --git a/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts index 3f5b8fd227e..30354b57437 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts @@ -96,7 +96,7 @@ export class PetService { * @summary Finds Pets by status * @param status Status values that need to be considered for filter */ - public findPetsByStatus(status: Array, extraHttpRequestParams?: RequestOptionsArgs): Observable> { + public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: RequestOptionsArgs): Observable> { return this.findPetsByStatusWithHttpInfo(status, extraHttpRequestParams) .map((response: Response) => { if (response.status === 204) { @@ -305,7 +305,7 @@ export class PetService { * @param status Status values that need to be considered for filter */ - public findPetsByStatusWithHttpInfo(status: Array, extraHttpRequestParams?: RequestOptionsArgs): Observable { + public findPetsByStatusWithHttpInfo(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: RequestOptionsArgs): Observable { if (status === null || status === undefined) { throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.'); } diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/.swagger-codegen/VERSION b/samples/client/petstore/typescript-angular-v2/with-interfaces/.swagger-codegen/VERSION index 50794f17f1a..2bf1c1ccf36 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/.swagger-codegen/VERSION +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/.swagger-codegen/VERSION @@ -1 +1 @@ -2.3.1-SNAPSHOT \ No newline at end of file +2.3.1 diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts index 5f5d730060c..565e6d3dbe4 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts @@ -97,7 +97,7 @@ export class PetService implements PetServiceInterface { * @summary Finds Pets by status * @param status Status values that need to be considered for filter */ - public findPetsByStatus(status: Array, extraHttpRequestParams?: RequestOptionsArgs): Observable> { + public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: RequestOptionsArgs): Observable> { return this.findPetsByStatusWithHttpInfo(status, extraHttpRequestParams) .map((response: Response) => { if (response.status === 204) { @@ -306,7 +306,7 @@ export class PetService implements PetServiceInterface { * @param status Status values that need to be considered for filter */ - public findPetsByStatusWithHttpInfo(status: Array, extraHttpRequestParams?: RequestOptionsArgs): Observable { + public findPetsByStatusWithHttpInfo(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: RequestOptionsArgs): Observable { if (status === null || status === undefined) { throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.'); } diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.serviceInterface.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.serviceInterface.ts index 968ed61c846..160b06ffaca 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.serviceInterface.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.serviceInterface.ts @@ -42,7 +42,7 @@ export interface PetServiceInterface { * Multiple status values can be provided with comma separated strings * @param status Status values that need to be considered for filter */ - findPetsByStatus(status: Array, extraHttpRequestParams?: any): Observable>; + findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: any): Observable>; /** * Finds Pets by tags diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/.swagger-codegen/VERSION b/samples/client/petstore/typescript-angular-v4.3/npm/.swagger-codegen/VERSION index 50794f17f1a..2bf1c1ccf36 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/.swagger-codegen/VERSION +++ b/samples/client/petstore/typescript-angular-v4.3/npm/.swagger-codegen/VERSION @@ -1 +1 @@ -2.3.1-SNAPSHOT \ No newline at end of file +2.3.1 diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts index 40fb7221bd3..5dc987751bc 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts @@ -173,10 +173,10 @@ export class PetService { * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body. * @param reportProgress flag to report request and response progress. */ - public findPetsByStatus(status: Array, observe?: 'body', reportProgress?: boolean): Observable>; - public findPetsByStatus(status: Array, observe?: 'response', reportProgress?: boolean): Observable>>; - public findPetsByStatus(status: Array, observe?: 'events', reportProgress?: boolean): Observable>>; - public findPetsByStatus(status: Array, observe: any = 'body', reportProgress: boolean = false ): Observable { + public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, observe?: 'body', reportProgress?: boolean): Observable>; + public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, observe?: 'response', reportProgress?: boolean): Observable>>; + public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, observe?: 'events', reportProgress?: boolean): Observable>>; + public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, observe: any = 'body', reportProgress: boolean = false ): Observable { if (status === null || status === undefined) { throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.'); } diff --git a/samples/client/petstore/typescript-angular-v4/npm/.swagger-codegen/VERSION b/samples/client/petstore/typescript-angular-v4/npm/.swagger-codegen/VERSION index 50794f17f1a..2bf1c1ccf36 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/.swagger-codegen/VERSION +++ b/samples/client/petstore/typescript-angular-v4/npm/.swagger-codegen/VERSION @@ -1 +1 @@ -2.3.1-SNAPSHOT \ No newline at end of file +2.3.1 diff --git a/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts index 3f5b8fd227e..30354b57437 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts @@ -96,7 +96,7 @@ export class PetService { * @summary Finds Pets by status * @param status Status values that need to be considered for filter */ - public findPetsByStatus(status: Array, extraHttpRequestParams?: RequestOptionsArgs): Observable> { + public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: RequestOptionsArgs): Observable> { return this.findPetsByStatusWithHttpInfo(status, extraHttpRequestParams) .map((response: Response) => { if (response.status === 204) { @@ -305,7 +305,7 @@ export class PetService { * @param status Status values that need to be considered for filter */ - public findPetsByStatusWithHttpInfo(status: Array, extraHttpRequestParams?: RequestOptionsArgs): Observable { + public findPetsByStatusWithHttpInfo(status: Array<'available' | 'pending' | 'sold'>, extraHttpRequestParams?: RequestOptionsArgs): Observable { if (status === null || status === undefined) { throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.'); }