Skip to content

Dart: use lower camel case for values #21228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 13 additions & 0 deletions bin/configs/dart-dio-petstore-client-use-lower-camel-case.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generatorName: dart-dio
outputDir: samples/openapi3/client/petstore/dart-dio/petstore_client_lib_use_lower_camel_case
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-use-lower-camel-case.yaml
templateDir: modules/openapi-generator/src/main/resources/dart/libraries/dio
typeMappings:
Client: "ModelClient"
File: "ModelFile"
EnumClass: "ModelEnumClass"
additionalProperties:
hideGenerationTimestamp: "true"
enumUnknownDefaultCase: "true"
serializationLibrary: "json_serializable"
useLowerCamelCase: "true"
10 changes: 10 additions & 0 deletions bin/configs/dart-petstore-client-use-lower-camel-case.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
generatorName: dart
outputDir: samples/openapi3/client/petstore/dart2/petstore_client_lib_use_lower_camel_case
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-use-lower-camel-case.yaml
templateDir: modules/openapi-generator/src/main/resources/dart2
typeMappings:
Client: "ModelClient"
File: "ModelFile"
additionalProperties:
hideGenerationTimestamp: "true"
useLowerCamelCase: "true"
286 changes: 147 additions & 139 deletions docs/generators/dart-dio.md

Large diffs are not rendered by default.

280 changes: 144 additions & 136 deletions docs/generators/dart.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public abstract class AbstractDartCodegen extends DefaultCodegen {
public static final String PUB_REPOSITORY = "pubRepository";
public static final String PUB_PUBLISH_TO = "pubPublishTo";
public static final String USE_ENUM_EXTENSION = "useEnumExtension";
public static final String USE_LOWER_CAMEL_CASE = "useLowerCamelCase";

@Setter protected String pubLibrary = "openapi.api";
@Setter protected String pubName = "openapi";
Expand All @@ -57,6 +58,7 @@ public abstract class AbstractDartCodegen extends DefaultCodegen {
@Setter protected String pubRepository = null;
@Setter protected String pubPublishTo = null;
@Setter protected boolean useEnumExtension = false;
@Setter protected boolean useLowerCamelCase = false;
@Setter protected String sourceFolder = "src";
protected String libPath = "lib" + File.separator;
protected String apiDocPath = "doc/";
Expand Down Expand Up @@ -196,6 +198,7 @@ public AbstractDartCodegen() {
addOption(PUB_REPOSITORY, "Repository in generated pubspec", pubRepository);
addOption(PUB_PUBLISH_TO, "Publish_to in generated pubspec", pubPublishTo);
addOption(USE_ENUM_EXTENSION, "Allow the 'x-enum-values' extension for enums", String.valueOf(useEnumExtension));
addOption(USE_LOWER_CAMEL_CASE, "Use lower camel case", String.valueOf(useLowerCamelCase));
addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder);
}

Expand Down Expand Up @@ -302,6 +305,12 @@ public void processOpts() {
additionalProperties.put(USE_ENUM_EXTENSION, useEnumExtension);
}

if (additionalProperties.containsKey(USE_LOWER_CAMEL_CASE)) {
this.setUseLowerCamelCase(convertPropertyToBooleanAndWriteBack(USE_LOWER_CAMEL_CASE));
} else {
additionalProperties.put(USE_LOWER_CAMEL_CASE, useLowerCamelCase);
}

if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) {
String srcFolder = (String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER);
this.setSourceFolder(srcFolder.replace('/', File.separatorChar));
Expand Down Expand Up @@ -386,7 +395,7 @@ public String toVarName(String name) {
}
name = name.replaceAll("^_", "");

// if it's all upper case, do nothing
// if it's all upper case and more than two letters
if (name.matches("^[A-Z_]*$")) {
return name;
}
Expand All @@ -400,7 +409,7 @@ public String toVarName(String name) {
name = sanitizeName(name);

// camelize (lower first character) the variable name
// pet_id => petId
// pet_id => petI
name = camelize(name, LOWERCASE_FIRST_LETTER);

if (name.matches("^\\d.*")) {
Expand Down Expand Up @@ -732,9 +741,14 @@ public String toEnumVarName(String value, String datatype) {
return enumNameMapping.get(value);
}

if (value.length() == 0) {
if (value.isEmpty()) {
return "empty";
}

if (useLowerCamelCase && value.matches("^[A-Z_]*$")) {
value = value.toLowerCase();
}

if (("number".equalsIgnoreCase(datatype) ||
"double".equalsIgnoreCase(datatype) ||
"int".equalsIgnoreCase(datatype)) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected void verifyOptions() {
verify(clientCodegen).setPubPublishTo(DartClientOptionsProvider.PUB_PUBLISH_TO_VALUE);
verify(clientCodegen).setSourceFolder(DartClientOptionsProvider.SOURCE_FOLDER_VALUE);
verify(clientCodegen).setUseEnumExtension(Boolean.parseBoolean(DartClientOptionsProvider.USE_ENUM_EXTENSION));
verify(clientCodegen).setUseLowerCamelCase(Boolean.parseBoolean(DartClientOptionsProvider.USE_LOWER_CAMEL_CASE));
verify(clientCodegen).setEnumUnknownDefaultCase(Boolean.parseBoolean(DartClientOptionsProvider.ENUM_UNKNOWN_DEFAULT_CASE_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected void verifyOptions() {
verify(clientCodegen).setDateLibrary(DartDioClientCodegen.DATE_LIBRARY_DEFAULT);
verify(clientCodegen).setLibrary(DartDioClientCodegen.SERIALIZATION_LIBRARY_DEFAULT);
verify(clientCodegen).setEqualityCheckMethod(DartDioClientCodegen.EQUALITY_CHECK_METHOD_DEFAULT);
verify(clientCodegen).setUseLowerCamelCase(Boolean.parseBoolean(DartDioClientOptionsProvider.USE_LOWER_CAMEL_CASE));
verify(clientCodegen).setEnumUnknownDefaultCase(Boolean.parseBoolean(DartDioClientOptionsProvider.ENUM_UNKNOWN_DEFAULT_CASE_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class DartClientOptionsProvider implements OptionsProvider {
public static final String PUB_PUBLISH_TO_VALUE = "Publish To";
public static final String SOURCE_FOLDER_VALUE = "src";
public static final String USE_ENUM_EXTENSION = "true";
public static final String USE_LOWER_CAMEL_CASE = "false";
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
public static final String ENUM_UNKNOWN_DEFAULT_CASE_VALUE = "false";
Expand All @@ -64,6 +65,7 @@ public Map<String, String> createOptions() {
.put(DartClientCodegen.PUB_PUBLISH_TO, PUB_PUBLISH_TO_VALUE)
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE)
.put(DartClientCodegen.USE_ENUM_EXTENSION, USE_ENUM_EXTENSION)
.put(DartClientCodegen.USE_LOWER_CAMEL_CASE, USE_LOWER_CAMEL_CASE)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.put(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class DartDioClientOptionsProvider implements OptionsProvider {
public static final String PUB_REPOSITORY_VALUE = "Repository";
public static final String PUB_PUBLISH_TO_VALUE = "Publish to";
public static final String ENUM_UNKNOWN_DEFAULT_CASE_VALUE = "false";
public static final String USE_LOWER_CAMEL_CASE = "false";

@Override
public String getLanguage() {
Expand Down Expand Up @@ -67,6 +68,7 @@ public Map<String, String> createOptions() {
.put(DartDioClientCodegen.EQUALITY_CHECK_METHOD, DartDioClientCodegen.EQUALITY_CHECK_METHOD_DEFAULT)
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE)
.put(DartDioClientCodegen.USE_ENUM_EXTENSION, USE_ENUM_EXTENSION)
.put(DartDioClientCodegen.USE_LOWER_CAMEL_CASE, USE_LOWER_CAMEL_CASE)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.put(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "true")
Expand Down
Loading