Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";

protected String modelPropertyNaming = CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.camelCase.name();

public DartClientCodegen() {
super();

Expand All @@ -51,6 +53,7 @@ public DartClientCodegen() {
modelDocTemplateFiles.put("object_doc.mustache", ".md");
apiDocTemplateFiles.put("api_doc.mustache", ".md");


// default HIDE_GENERATION_TIMESTAMP to true
hideGenerationTimestamp = Boolean.TRUE;

Expand Down Expand Up @@ -101,8 +104,11 @@ public DartClientCodegen() {
// mapped to String as a workaround
typeMapping.put("binary", "String");
typeMapping.put("ByteArray", "String");

cliOptions.add(new CliOption(BROWSER_CLIENT, "Is the client browser based"));

CliOption modelPropertyNaming = new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC);
cliOptions.add(modelPropertyNaming.defaultValue("camelCase"));

cliOptions.add(new CliOption(PUB_NAME, "Name in generated pubspec"));
cliOptions.add(new CliOption(PUB_VERSION, "Version in generated pubspec"));
cliOptions.add(new CliOption(PUB_DESCRIPTION, "Description in generated pubspec"));
Expand Down Expand Up @@ -136,6 +142,13 @@ public void processOpts() {
additionalProperties.put(BROWSER_CLIENT, browserClient);
}


if (additionalProperties.containsKey(CodegenConstants.MODEL_PROPERTY_NAMING)) {
setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING));
}



if (additionalProperties.containsKey(PUB_NAME)) {
this.setPubName((String) additionalProperties.get(PUB_NAME));
} else {
Expand Down Expand Up @@ -215,6 +228,17 @@ public String modelDocFileFolder() {
return (outputFolder + "/" + modelDocPath).replace('/', File.separatorChar);
}


public String formatPropertyOrVarName(String name) {
switch (CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.valueOf(getModelPropertyNaming())) {
case original: return name;
case camelCase: return camelize(name, true);
case PascalCase: return camelize(name);
case snake_case: return underscore(name);
default: return camelize(name, true); // default=camelCase
}
}

@Override
public String toVarName(String name) {
// replace - with _ e.g. created-at => created_at
Expand All @@ -227,7 +251,7 @@ public String toVarName(String name) {

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

if (name.matches("^\\d.*")) {
name = "n" + name;
Expand All @@ -250,13 +274,13 @@ public String toParamName(String name) {
public String toModelName(String name) {
// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
LOGGER.warn(name + " (reserved word) cannot be used as model filename. Renamed to " + camelize("model_" + name));
LOGGER.warn(name + " (reserved word) cannot be used as model filename. Renamed to " + formatPropertyOrVarName("model_" + name));
name = "model_" + name; // e.g. return => ModelReturn (after camelize)
}

// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
return formatPropertyOrVarName(name);
}

@Override
Expand Down Expand Up @@ -381,7 +405,7 @@ private boolean buildEnumFromVendorExtension(CodegenModel cm) {
new ArrayList<Map<String, String>>();
for (Map<String, Object> value : values) {
Map<String, String> enumVar = new HashMap<String, String>();
String name = camelize((String) value.get("identifier"), true);
String name = formatPropertyOrVarName((String) value.get("identifier"));
if (isReservedWord(name)) {
name = escapeReservedWord(name);
}
Expand All @@ -407,7 +431,7 @@ public String toEnumVarName(String value, String datatype) {
"int".equalsIgnoreCase(datatype)) {
var = "Number" + var;
}
return escapeReservedWord(camelize(var, true));
return escapeReservedWord(formatPropertyOrVarName(var));
}

@Override
Expand All @@ -429,7 +453,7 @@ public String toOperationId(String operationId) {
return newOperationId;
}

return camelize(operationId, true);
return formatPropertyOrVarName(operationId);
}

public void setBrowserClient(boolean browserClient) {
Expand All @@ -456,6 +480,22 @@ public void setSourceFolder(String sourceFolder) {
this.sourceFolder = sourceFolder;
}


public void setModelPropertyNaming(String naming) {
if ("original".equals(naming) || "camelCase".equals(naming) ||
"PascalCase".equals(naming) || "snake_case".equals(naming)) {
this.modelPropertyNaming = naming;
} else {
throw new IllegalArgumentException("Invalid model property naming '" +
naming + "'. Must be 'original', 'camelCase', " +
"'PascalCase' or 'snake_case'");
}
}

public String getModelPropertyNaming() {
return this.modelPropertyNaming;
}

@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ class ApiClient {
throw new ApiException(500, 'Could not find a suitable class for deserialization');
}

dynamic deserialize(String jsonVal, String targetType) {
dynamic deserialize(String jsonstr, String targetType) {
// Remove all spaces. Necessary for reg expressions as well.
targetType = targetType.replaceAll(' ', '');

if (targetType == 'String') return jsonVal;

var decodedJson = json.decode(jsonVal);
var decodedJson = json.decode(jsonstr);
return _deserialize(decodedJson, targetType);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: {{pubName}}
version: {{pubVersion}}
description: {{pubDescription}}
environment:
# The pub client defaults to an <2.0.0 sdk constraint which we need to explicitly overwrite.
sdk: ">=1.0.0 <3.0.0"
dependencies:
http: '>=0.11.1 <0.12.0'
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ protected void setExpectations() {
times = 1;
clientCodegen.setUseEnumExtension(Boolean.valueOf(DartClientOptionsProvider.USE_ENUM_EXTENSION));
times = 1;
clientCodegen.setModelPropertyNaming(DartClientOptionsProvider.MODEL_PROPERTY_NAMING);
times = 1;
}};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DartClientOptionsProvider implements OptionsProvider {
public static final String SOURCE_FOLDER_VALUE = "src";
public static final String USE_ENUM_EXTENSION = "true";
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
public static final String MODEL_PROPERTY_NAMING = "modelPropertyNaming";


@Override
Expand All @@ -36,6 +37,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE)
.put(DartClientCodegen.USE_ENUM_EXTENSION, USE_ENUM_EXTENSION)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING)
.build();
}

Expand Down