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 2 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 = true;
@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.setUseEnumExtension(convertPropertyToBooleanAndWriteBack(USE_LOWER_CAMEL_CASE));
} else {
additionalProperties.put(USE_ENUM_EXTENSION, 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 @@ -387,7 +396,7 @@ public String toVarName(String name) {
name = name.replaceAll("^_", "");

// if it's all upper case, do nothing
if (name.matches("^[A-Z_]*$")) {
if (!useLowerCamelCase && name.matches("^[A-Z_]*$")) {
return name;
}

Expand All @@ -399,9 +408,14 @@ public String toVarName(String name) {
// remove the rest
name = sanitizeName(name);

// camelize (lower first character) the variable name
// pet_id => petId
name = camelize(name, LOWERCASE_FIRST_LETTER);
if (useLowerCamelCase) {
//to camelize it correctly it needs to be lower cased
name = camelize(name.toLowerCase(), LOWERCASE_FIRST_LETTER);
} else {
// camelize (lower first character) the variable name
// pet_id => petId
name = camelize(name, LOWERCASE_FIRST_LETTER);
}

if (name.matches("^\\d.*")) {
name = "n" + name;
Expand Down
Loading