diff --git a/docs/generators/javascript-flowtyped.md b/docs/generators/javascript-flowtyped.md index 9c354db86bb..8cf943fa317 100644 --- a/docs/generators/javascript-flowtyped.md +++ b/docs/generators/javascript-flowtyped.md @@ -7,6 +7,7 @@ sidebar_label: javascript-flowtyped | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| diff --git a/docs/generators/typescript-angular.md b/docs/generators/typescript-angular.md index 5324f24baa6..13c700448a0 100644 --- a/docs/generators/typescript-angular.md +++ b/docs/generators/typescript-angular.md @@ -8,6 +8,7 @@ sidebar_label: typescript-angular |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |apiModulePrefix|The prefix of the generated ApiModule.| |null| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |fileNaming|Naming convention for the output files: 'camelCase', 'kebab-case'.| |camelCase| |modelFileSuffix|The suffix of the file of the generated model (model<suffix>.ts).| |null| diff --git a/docs/generators/typescript-angularjs.md b/docs/generators/typescript-angularjs.md index 09beb233b1c..ac3aaf63d60 100644 --- a/docs/generators/typescript-angularjs.md +++ b/docs/generators/typescript-angularjs.md @@ -7,6 +7,7 @@ sidebar_label: typescript-angularjs | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false| diff --git a/docs/generators/typescript-aurelia.md b/docs/generators/typescript-aurelia.md index 8028d707794..7f934105a89 100644 --- a/docs/generators/typescript-aurelia.md +++ b/docs/generators/typescript-aurelia.md @@ -7,6 +7,7 @@ sidebar_label: typescript-aurelia | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| diff --git a/docs/generators/typescript-axios.md b/docs/generators/typescript-axios.md index 29f419bb838..f34971518b6 100644 --- a/docs/generators/typescript-axios.md +++ b/docs/generators/typescript-axios.md @@ -7,6 +7,7 @@ sidebar_label: typescript-axios | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| diff --git a/docs/generators/typescript-fetch.md b/docs/generators/typescript-fetch.md index c319cec9c19..695c85c033e 100644 --- a/docs/generators/typescript-fetch.md +++ b/docs/generators/typescript-fetch.md @@ -7,6 +7,7 @@ sidebar_label: typescript-fetch | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| diff --git a/docs/generators/typescript-inversify.md b/docs/generators/typescript-inversify.md index 68a4f9c65ae..62170b6117c 100644 --- a/docs/generators/typescript-inversify.md +++ b/docs/generators/typescript-inversify.md @@ -7,6 +7,7 @@ sidebar_label: typescript-inversify | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| diff --git a/docs/generators/typescript-jquery.md b/docs/generators/typescript-jquery.md index 0ac83747079..8a00cf0d97b 100644 --- a/docs/generators/typescript-jquery.md +++ b/docs/generators/typescript-jquery.md @@ -7,6 +7,7 @@ sidebar_label: typescript-jquery | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |jqueryAlreadyImported|When using this in legacy app using mix of typescript and javascript, this will only declare jquery and not import it| |false| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| diff --git a/docs/generators/typescript-node.md b/docs/generators/typescript-node.md index b35794784fa..7baeb924014 100644 --- a/docs/generators/typescript-node.md +++ b/docs/generators/typescript-node.md @@ -7,6 +7,7 @@ sidebar_label: typescript-node | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| diff --git a/docs/generators/typescript-redux-query.md b/docs/generators/typescript-redux-query.md index db2f20c3e07..e888c68f49f 100644 --- a/docs/generators/typescript-redux-query.md +++ b/docs/generators/typescript-redux-query.md @@ -7,6 +7,7 @@ sidebar_label: typescript-redux-query | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| diff --git a/docs/generators/typescript-rxjs.md b/docs/generators/typescript-rxjs.md index 90c85bbf143..41811e3d1ff 100644 --- a/docs/generators/typescript-rxjs.md +++ b/docs/generators/typescript-rxjs.md @@ -7,6 +7,7 @@ sidebar_label: typescript-rxjs | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| +|enumNameSuffix|Suffix that will be appended to all enum names. A special 'v4-compat' value enables the backward-compatible behavior (as pre v4.2.3)| |v4-compat| |enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| |npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java index 3db985cb8c5..75a7c18c783 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java @@ -51,6 +51,11 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp public static final String NPM_VERSION = "npmVersion"; public static final String SNAPSHOT = "snapshot"; + public static final String ENUM_NAME_SUFFIX_V4_COMPAT = "v4-compat"; + public static final String ENUM_NAME_SUFFIX_DESC_CUSTOMIZED = CodegenConstants.ENUM_NAME_SUFFIX_DESC + + " A special '" + ENUM_NAME_SUFFIX_V4_COMPAT + "' value enables the backward-compatible behavior (as pre v4.2.3)"; + + // NOTE: SimpleDateFormat is not thread-safe and may not be static unless it is thread-local @SuppressWarnings("squid:S5164") protected static final ThreadLocal SNAPSHOT_SUFFIX_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT)); @@ -62,7 +67,9 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp protected String npmName = null; protected String npmVersion = "1.0.0"; - protected String enumSuffix = "Enum"; + protected String enumSuffix = ENUM_NAME_SUFFIX_V4_COMPAT; + protected Boolean isEnumSuffixV4Compat = false; + protected String classEnumSeparator = "."; public AbstractTypeScriptClientCodegen() { @@ -151,11 +158,13 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp typeMapping.put("DateTime", "Date"); typeMapping.put("binary", "any"); typeMapping.put("File", "any"); + typeMapping.put("file", "any"); typeMapping.put("ByteArray", "string"); typeMapping.put("UUID", "string"); typeMapping.put("URI", "string"); typeMapping.put("Error", "Error"); + cliOptions.add(new CliOption(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX_DESC_CUSTOMIZED).defaultValue(this.enumSuffix)); cliOptions.add(new CliOption(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_DESC).defaultValue(this.enumPropertyNaming.name())); cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue(this.modelPropertyNaming)); cliOptions.add(new CliOption(CodegenConstants.SUPPORTS_ES6, CodegenConstants.SUPPORTS_ES6_DESC).defaultValue(String.valueOf(this.getSupportsES6()))); @@ -179,6 +188,9 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp LOGGER.info("Warning: Environment variable 'TS_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } + if (additionalProperties.containsKey(CodegenConstants.ENUM_NAME_SUFFIX)) { + enumSuffix = additionalProperties.get(CodegenConstants.ENUM_NAME_SUFFIX).toString(); + } if (additionalProperties.containsKey(CodegenConstants.ENUM_PROPERTY_NAMING)) { setEnumPropertyNaming((String) additionalProperties.get(CodegenConstants.ENUM_PROPERTY_NAMING)); } @@ -195,6 +207,11 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp if (additionalProperties.containsKey(NPM_NAME)) { this.setNpmName(additionalProperties.get(NPM_NAME).toString()); } + + if (enumSuffix.equals(ENUM_NAME_SUFFIX_V4_COMPAT)) { + isEnumSuffixV4Compat = true; + enumSuffix = modelNameSuffix + "Enum"; + } } @Override @@ -298,40 +315,55 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp @Override public String toModelName(final String name) { + String fullModelName = name; + fullModelName = addPrefix(fullModelName, modelNamePrefix); + fullModelName = addSuffix(fullModelName, modelNameSuffix); + return toTypescriptTypeName(fullModelName, "Model"); + } + + protected String addPrefix(String name, String prefix) { + if (!StringUtils.isEmpty(prefix)) { + name = prefix + "_" + name; + } + return name; + } + + protected String addSuffix(String name, String suffix) { + if (!StringUtils.isEmpty(suffix)) { + name = name + "_" + suffix; + } + + return name; + } + + protected String toTypescriptTypeName(final String name, String safePrefix) { ArrayList exceptions = new ArrayList(Arrays.asList("\\|", " ")); String sanName = sanitizeName(name, "(?![| ])\\W", exceptions); - if (!StringUtils.isEmpty(modelNamePrefix)) { - sanName = modelNamePrefix + "_" + sanName; - } - - if (!StringUtils.isEmpty(modelNameSuffix)) { - sanName = sanName + "_" + modelNameSuffix; - } + sanName = camelize(sanName); // model name cannot use reserved keyword, e.g. return + // this is unlikely to happen, because we have just camelized the name, while reserved words are usually all lowcase if (isReservedWord(sanName)) { - String modelName = camelize("model_" + sanName); + String modelName = safePrefix + sanName; LOGGER.warn(sanName + " (reserved word) cannot be used as model name. Renamed to " + modelName); return modelName; } // model name starts with number if (sanName.matches("^\\d.*")) { - String modelName = camelize("model_" + sanName); // e.g. 200Response => Model200Response (after camelize) + String modelName = safePrefix + sanName; // e.g. 200Response => Model200Response LOGGER.warn(sanName + " (model name starts with number) cannot be used as model name. Renamed to " + modelName); return modelName; } if (languageSpecificPrimitives.contains(sanName)) { - String modelName = camelize("model_" + sanName); + String modelName = safePrefix + sanName; LOGGER.warn(sanName + " (model name matches existing language type) cannot be used as a model name. Renamed to " + modelName); return modelName; } - // camelize the model name - // phone_number => PhoneNumber - return camelize(sanName); + return sanName; } @Override @@ -584,12 +616,9 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp @Override public String toEnumName(CodegenProperty property) { - String enumName = toModelName(property.name) + enumSuffix; - if (enumName.matches("\\d.*")) { // starts with number - return "_" + enumName; - } else { - return enumName; - } + String enumName = property.name; + enumName = addSuffix(enumName, enumSuffix); + return toTypescriptTypeName(enumName, "_"); } protected void setEnumPropertyNaming(String naming) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java index fc0f886c1df..bbf91dbd9b4 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java @@ -160,7 +160,6 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode setStringEnums(Boolean.valueOf(additionalProperties.get(STRING_ENUMS).toString())); additionalProperties.put("stringEnums", getStringEnums()); if (getStringEnums()) { - enumSuffix = ""; classEnumSeparator = ""; } } @@ -228,6 +227,23 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode if (additionalProperties.containsKey(FILE_NAMING)) { this.setFileNaming(additionalProperties.get(FILE_NAMING).toString()); } + + if (isEnumSuffixV4Compat) { + applyEnumSuffixV4CompatMode(); + } + } + + private void applyEnumSuffixV4CompatMode() { + String fullModelSuffix = modelSuffix + modelNameSuffix; + if (stringEnums) { + // with stringEnums, legacy code would discard "Enum" suffix altogether + // resulting in smth like PetModelTypeModeL + enumSuffix = fullModelSuffix; + } else { + // without stringEnums, "Enum" was appended to model suffix, e.g. PetModel.TypeModelEnum + enumSuffix = fullModelSuffix + "Enum"; + } + } private void addNpmPackageGeneration(SemVer ngVersion) { @@ -605,10 +621,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode @Override public String toModelName(String name) { - if (modelSuffix.length() != 0 && !name.endsWith(modelSuffix)) { - name = name + modelSuffix; - } - + name = addSuffix(name, modelSuffix); return super.toModelName(name); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java index 1aaf04f213f..d65ba459df6 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java @@ -25,6 +25,7 @@ import java.util.Map; public class TypeScriptAngularClientOptionsProvider implements OptionsProvider { public static final String SUPPORTS_ES6_VALUE = "false"; + public static final String ENUM_NAME_SUFFIX = "Enum"; public static final String STRING_ENUMS_VALUE = "false"; public static final String SORT_PARAMS_VALUE = "false"; public static final String SORT_MODEL_PROPERTIES_VALUE = "false"; @@ -58,6 +59,7 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider { .put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE) .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) .put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE) + .put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX) .put(TypeScriptAngularClientCodegen.STRING_ENUMS, STRING_ENUMS_VALUE) .put(TypeScriptAngularClientCodegen.NPM_NAME, NMP_NAME) .put(TypeScriptAngularClientCodegen.NPM_VERSION, NMP_VERSION) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularJsClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularJsClientOptionsProvider.java index 8c6f9efde04..11aa70a681a 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularJsClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularJsClientOptionsProvider.java @@ -24,6 +24,7 @@ import java.util.Map; public class TypeScriptAngularJsClientOptionsProvider implements OptionsProvider { public static final String SUPPORTS_ES6_VALUE = "false"; + public static final String ENUM_NAME_SUFFIX = "Enum"; public static final String SORT_PARAMS_VALUE = "false"; public static final String SORT_MODEL_PROPERTIES_VALUE = "false"; public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; @@ -43,6 +44,7 @@ public class TypeScriptAngularJsClientOptionsProvider implements OptionsProvider return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE) .put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, SORT_MODEL_PROPERTIES_VALUE) .put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE) + .put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX) .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) .put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE) .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAureliaClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAureliaClientOptionsProvider.java index 1105fa14a45..2ec182979cc 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAureliaClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAureliaClientOptionsProvider.java @@ -28,6 +28,7 @@ public class TypeScriptAureliaClientOptionsProvider implements OptionsProvider { public static final String SORT_MODEL_PROPERTIES_VALUE = "false"; public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; public static final Boolean SUPPORTS_ES6_VALUE = false; + public static final String ENUM_NAME_SUFFIX = "Enum"; public static final String ENUM_PROPERTY_NAMING_VALUE = "PascalCase"; public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase"; private static final String NPM_NAME = "npmName"; @@ -49,6 +50,7 @@ public class TypeScriptAureliaClientOptionsProvider implements OptionsProvider { .put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE) .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) .put(CodegenConstants.SUPPORTS_ES6, String.valueOf(SUPPORTS_ES6_VALUE)) + .put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX) .put(TypeScriptAureliaClientCodegen.NPM_NAME, NPM_NAME) .put(TypeScriptAureliaClientCodegen.NPM_VERSION, NPM_VERSION) .put(TypeScriptAureliaClientCodegen.SNAPSHOT, Boolean.FALSE.toString()) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java index 6bf1bb7b082..fe637cfe52b 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java @@ -28,6 +28,7 @@ public class TypeScriptFetchClientOptionsProvider implements OptionsProvider { public static final String SORT_MODEL_PROPERTIES_VALUE = "false"; public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; public static final Boolean SUPPORTS_ES6_VALUE = false; + public static final String ENUM_NAME_SUFFIX = "Enum"; public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase"; public static final String ENUM_PROPERTY_NAMING_VALUE = "PascalCase"; private static final String NMP_NAME = "npmName"; @@ -51,6 +52,7 @@ public class TypeScriptFetchClientOptionsProvider implements OptionsProvider { .put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE) .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) .put(CodegenConstants.SUPPORTS_ES6, String.valueOf(SUPPORTS_ES6_VALUE)) + .put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX) .put(TypeScriptFetchClientCodegen.NPM_NAME, NMP_NAME) .put(TypeScriptFetchClientCodegen.NPM_VERSION, NMP_VERSION) .put(TypeScriptFetchClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNodeClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNodeClientOptionsProvider.java index c46fea54e3c..a369f1a2f56 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNodeClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNodeClientOptionsProvider.java @@ -26,6 +26,7 @@ import java.util.Map; public class TypeScriptNodeClientOptionsProvider implements OptionsProvider { public static final String SUPPORTS_ES6_VALUE = "false"; + public static final String ENUM_NAME_SUFFIX = "Enum"; public static final String SORT_PARAMS_VALUE = "false"; public static final String SORT_MODEL_PROPERTIES_VALUE = "false"; public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; @@ -49,6 +50,7 @@ public class TypeScriptNodeClientOptionsProvider implements OptionsProvider { return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE) .put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, SORT_MODEL_PROPERTIES_VALUE) .put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE) + .put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX) .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) .put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE) .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java index 58b8848a6f0..ec451b308d3 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java @@ -227,6 +227,7 @@ public class TypeScriptFetchModelTest { // TODO: update yaml file. final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml"); final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + codegen.processOpts(); codegen.setOpenAPI(openAPI); final Schema schema = openAPI.getComponents().getSchemas().get("EnumArrays"); @@ -264,6 +265,7 @@ public class TypeScriptFetchModelTest { public void enumMdoelValueTest() { final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml"); final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + codegen.processOpts(); codegen.setOpenAPI(openAPI); final Schema schema = openAPI.getComponents().getSchemas().get("Enum_Test"); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java index 6a538c69273..a1a07d28aa6 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java @@ -10,6 +10,7 @@ import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponses; import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenOperation; +import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.TestUtils; import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; import org.testng.Assert; @@ -54,6 +55,57 @@ public class TypeScriptAngularClientCodegenTest { Assert.assertEquals(codegen.toModelName("Error"), "ErrorMySuffix"); } + @Test + public void testToEnumName() { + TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen(); + codegen.additionalProperties().put(CodegenConstants.ENUM_NAME_SUFFIX, "Enum"); + codegen.processOpts(); + + Assert.assertEquals(codegen.toEnumName(makeEnumProperty("TestName")), "TestNameEnum"); + Assert.assertEquals(codegen.toEnumName(makeEnumProperty("123")), "_123Enum"); + + // enum value should not use model suffix + codegen = new TypeScriptAngularClientCodegen(); + codegen.additionalProperties().put(TypeScriptAngularClientCodegen.MODEL_SUFFIX, "Model"); + codegen.additionalProperties().put(CodegenConstants.ENUM_NAME_SUFFIX, "Enum2"); + codegen.processOpts(); + Assert.assertEquals(codegen.toEnumName(makeEnumProperty("TestName")), "TestNameEnum2"); + + codegen = new TypeScriptAngularClientCodegen(); + codegen.additionalProperties().put(CodegenConstants.ENUM_NAME_SUFFIX, ""); + codegen.processOpts(); + Assert.assertEquals(codegen.toEnumName(makeEnumProperty("TestName")), "TestName"); + } + + @Test + public void testToEnumNameCompatMode() { + TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen(); + // default - stringEnums=false + codegen.processOpts(); + + Assert.assertEquals(codegen.toEnumName(makeEnumProperty("TestName")), "TestNameEnum"); + + // model suffix is prepended to "Enum" suffix + codegen = new TypeScriptAngularClientCodegen(); + codegen.additionalProperties().put(CodegenConstants.MODEL_NAME_SUFFIX, "Model1"); + codegen.additionalProperties().put(TypeScriptAngularClientCodegen.MODEL_SUFFIX, "Model2"); + codegen.processOpts(); + Assert.assertEquals(codegen.toEnumName(makeEnumProperty("TestName")), "TestNameModel2Model1Enum"); + + codegen = new TypeScriptAngularClientCodegen(); + codegen.additionalProperties().put(TypeScriptAngularClientCodegen.STRING_ENUMS, true); + codegen.additionalProperties().put(CodegenConstants.MODEL_NAME_SUFFIX, "Model1"); + codegen.additionalProperties().put(TypeScriptAngularClientCodegen.MODEL_SUFFIX, "Model2"); + codegen.processOpts(); + Assert.assertEquals(codegen.toEnumName(makeEnumProperty("TestName")), "TestNameModel2Model1"); + } + + private CodegenProperty makeEnumProperty(String name) { + CodegenProperty enumProperty = new CodegenProperty(); + enumProperty.name = name; + return enumProperty; + } + @Test public void testModelFileSuffix() { TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();