[typescript] Append enum suffix without model suffix (#5138)

* [typescript] Generate enum name independently on model name, ignoring modelSuffix

* Add `enumSufix` cli option for all typescript generators

* Add ENUM_NAME_SUFFIX to to TypeScript***ClientOptionsProvider

* Add a typeMapping for OAS2 built-in "file" type

* Re-generate samples

* Introduce `v4-compat` mode for enumNamePrefix

* Update TypeScriptFetchModelTest - make sure codegen options are processed before running assertions

* Regenerate samples

* Regenerate docs

* Regenerate docs
This commit is contained in:
Alexey Makhrov 2020-01-31 03:17:22 -08:00 committed by GitHub
parent 92739a967a
commit e32a2f0121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 142 additions and 25 deletions

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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<SimpleDateFormat> 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<String> exceptions = new ArrayList<String>(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) {

View File

@ -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);
}

View File

@ -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)

View File

@ -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)

View File

@ -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())

View File

@ -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)

View File

@ -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)

View File

@ -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");

View File

@ -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();