Skip to content

Commit

Permalink
[typescript] Clean up modelPropertyNaming across generators
Browse files Browse the repository at this point in the history
Fixes OpenAPITools#2976

Generators without runtime models conversion use "original" property naming by default. It's still possible to change it via cli options

Generators with runtime conversion keep using "camelCase"
  • Loading branch information
amakhrov committed Feb 25, 2020
1 parent c0fcffd commit da3c699
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
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)";

public static final String MODEL_PROPERTY_NAMING_DESC_WITH_WARNING = CodegenConstants.MODEL_PROPERTY_NAMING_DESC
+ ". Only change it if you provide your own run-time code for (de-)serialization of models";

public static final String NULL_SAFE_ADDITIONAL_PROPS = "nullSafeAdditionalProps";
public static final String NULL_SAFE_ADDITIONAL_PROPS_DESC = "Set to make additional properties types declare that their indexer may return undefined";

// 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));

protected String modelPropertyNaming = "camelCase";
protected String modelPropertyNaming = "original";
protected ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = ENUM_PROPERTY_NAMING_TYPE.PascalCase;
protected Boolean supportsES6 = false;
protected Boolean nullSafeAdditionalProps = false;
Expand Down Expand Up @@ -170,7 +173,7 @@ public AbstractTypeScriptClientCodegen() {

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.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_DESC_WITH_WARNING).defaultValue(this.modelPropertyNaming));
cliOptions.add(new CliOption(CodegenConstants.SUPPORTS_ES6, CodegenConstants.SUPPORTS_ES6_DESC).defaultValue(String.valueOf(this.getSupportsES6())));
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package." +
" Required to generate a full package"));
Expand All @@ -179,7 +182,12 @@ public AbstractTypeScriptClientCodegen() {
"When setting this property to true, the version will be suffixed with -SNAPSHOT." + this.SNAPSHOT_SUFFIX_FORMAT.get().toPattern(),
false));
this.cliOptions.add(new CliOption(NULL_SAFE_ADDITIONAL_PROPS, NULL_SAFE_ADDITIONAL_PROPS_DESC).defaultValue(String.valueOf(this.getNullSafeAdditionalProps())));
}

protected void supportModelPropertyNaming(String defaultModelPropertyNaming) {
removeOption(CodegenConstants.MODEL_PROPERTY_NAMING);
setModelPropertyNaming(defaultModelPropertyNaming);
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue(modelPropertyNaming));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public TypeScriptFetchClientCodegen() {
typeMapping.put("date", "Date");
typeMapping.put("DateTime", "Date");

supportModelPropertyNaming("camelCase");
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.TRUE.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public TypeScriptNodeClientCodegen() {
modelPackage = "model";
apiPackage = "api";

supportModelPropertyNaming("camelCase");
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public TypeScriptReduxQueryClientCodegen() {
typeMapping.put("date", "Date");
typeMapping.put("DateTime", "Date");

supportModelPropertyNaming("camelCase");
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.TRUE.toString()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.codegen.typescript.fetch;

import io.swagger.v3.oas.models.OpenAPI;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.TypeScriptFetchClientCodegen;
import org.testng.Assert;
Expand Down Expand Up @@ -56,4 +57,16 @@ public void testWithoutSnapshotVersion() {

}

@Test
public void toParamName() {
TypeScriptFetchClientCodegen codegen = new TypeScriptFetchClientCodegen();
codegen.processOpts();
Assert.assertEquals(codegen.toParamName("valid_param"), "validParam");

codegen = new TypeScriptFetchClientCodegen();
codegen.additionalProperties().put(CodegenConstants.MODEL_PROPERTY_NAMING, "original");
codegen.processOpts();
Assert.assertEquals(codegen.toParamName("valid_param"), "valid_param");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@


public class TypeScriptAngularClientCodegenTest {
@Test
public void toParamName() {
TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();
codegen.processOpts();
Assert.assertEquals(codegen.toParamName("valid_param"), "valid_param");

codegen = new TypeScriptAngularClientCodegen();
codegen.additionalProperties().put(CodegenConstants.MODEL_PROPERTY_NAMING, "camelCase");
codegen.processOpts();
Assert.assertEquals(codegen.toParamName("valid_param"), "validParam");
}

@Test
public void toEnumVarName() {
TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();
Expand Down

0 comments on commit da3c699

Please sign in to comment.