Skip to content

Commit

Permalink
[Groovy] improve code parsing body/form params (#2879)
Browse files Browse the repository at this point in the history
* [Groovy] improve code parsing body/form params

* remove leading space

* remove leading space
  • Loading branch information
karismann authored and wing328 committed May 20, 2019
1 parent fdd08dc commit 6bffe4d
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 196 deletions.
2 changes: 0 additions & 2 deletions docs/generators/groovy.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ sidebar_label: groovy
|groupId|groupId in generated pom.xml| |org.openapitools|
|artifactId|artifactId in generated pom.xml. This also becomes part of the generated library's filename| |openapi-groovy|
|artifactVersion|artifact version in generated pom.xml. This also becomes part of the generated library's filename| |1.0.0|
|artifactUrl|artifact URL in generated pom.xml| |https://github.com/openapitools/openapi-generator|
|artifactDescription|artifact description in generated pom.xml| |OpenAPI Java|
|scmConnection|SCM connection in generated pom.xml| |scm:git:git@github.com:openapitools/openapi-generator.git|
|scmDeveloperConnection|SCM developer connection in generated pom.xml| |scm:git:git@github.com:openapitools/openapi-generator.git|
|scmUrl|SCM URL in generated pom.xml| |https://github.com/openapitools/openapi-generator|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4862,7 +4862,7 @@ protected void addSwitch(String key, String description, Boolean defaultValue) {
*
* @param objs map of object
*/
public void generateJSONSpecFile(Map<String, Object> objs) {
protected void generateJSONSpecFile(Map<String, Object> objs) {
OpenAPI openAPI = (OpenAPI) objs.get("openAPI");
if (openAPI != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,19 +652,6 @@ public String toRegularExpression(String pattern) {
return escapeText(pattern);
}

public boolean convertPropertyToBoolean(String propertyKey) {
boolean booleanValue = false;
if (additionalProperties.containsKey(propertyKey)) {
booleanValue = Boolean.valueOf(additionalProperties.get(propertyKey).toString());
}

return booleanValue;
}

public void writePropertyBack(String propertyKey, boolean value) {
additionalProperties.put(propertyKey, value);
}

@Override
public String sanitizeTag(String tag) {
return camelize(sanitizeName(tag));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ private static String getAccept(OpenAPI openAPI, Operation operation) {
String defaultContentType = "application/json";
Set<String> producesInfo = getProducesInfo(openAPI, operation);
if (producesInfo != null && !producesInfo.isEmpty()) {
ArrayList<String> produces = new ArrayList<String>(producesInfo);
ArrayList<String> produces = new ArrayList<>(producesInfo);
StringBuilder sb = new StringBuilder();
for (String produce : produces) {
if (defaultContentType.equalsIgnoreCase(produce)) {
Expand All @@ -1085,7 +1085,7 @@ private static String getAccept(OpenAPI openAPI, Operation operation) {

@Override
protected boolean needToImport(String type) {
return super.needToImport(type) && type.indexOf(".") < 0;
return super.needToImport(type) && !type.contains(".");
}

@Override
Expand Down Expand Up @@ -1183,10 +1183,11 @@ private static CodegenModel reconcileInlineEnums(CodegenModel codegenModel, Code

if (removedChildEnum) {
// If we removed an entry from this model's vars, we need to ensure hasMore is updated
int count = 0, numVars = codegenProperties.size();
int count = 0;
int numVars = codegenProperties.size();
for (CodegenProperty codegenProperty : codegenProperties) {
count += 1;
codegenProperty.hasMore = (count < numVars) ? true : false;
codegenProperty.hasMore = count < numVars;
}
codegenModel.vars = codegenProperties;
}
Expand Down Expand Up @@ -1444,27 +1445,13 @@ public String toRegularExpression(String pattern) {
return escapeText(pattern);
}

@Override
public boolean convertPropertyToBoolean(String propertyKey) {
boolean booleanValue = false;
if (additionalProperties.containsKey(propertyKey)) {
booleanValue = Boolean.valueOf(additionalProperties.get(propertyKey).toString());
}

return booleanValue;
}

@Override
public void writePropertyBack(String propertyKey, boolean value) {
additionalProperties.put(propertyKey, value);
}

/**
* Output the Getter name for boolean property, e.g. isActive
*
* @param name the name of the property
* @return getter name based on naming convention
*/
@Override
public String toBooleanGetter(String name) {
return booleanGetterPrefix + getterAndSetterCapitalize(name);
}
Expand Down Expand Up @@ -1505,7 +1492,6 @@ public String getterAndSetterCapitalize(String name) {
return camelize(name, lowercaseFirstLetter);
}


@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ public GroovyClientCodegen() {
artifactId = "openapi-groovy";
dateLibrary = "legacy"; //TODO: add joda support to groovy

// clioOptions default redefinition need to be updated
// cliOptions default redefinition need to be updated
updateOption(CodegenConstants.SOURCE_FOLDER, this.getSourceFolder());
updateOption(CodegenConstants.INVOKER_PACKAGE, this.getInvokerPackage());
updateOption(CodegenConstants.ARTIFACT_ID, this.getArtifactId());
updateOption(CodegenConstants.API_PACKAGE, apiPackage);
updateOption(CodegenConstants.MODEL_PACKAGE, modelPackage);
updateOption(DATE_LIBRARY, this.getDateLibrary());
removeOption(CodegenConstants.ARTIFACT_URL);
removeOption(CodegenConstants.ARTIFACT_DESCRIPTION);

}

Expand Down Expand Up @@ -95,7 +97,6 @@ public void processOpts() {
@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> operations, List<Object> allModels) {
Map<String, Object> objs = (Map<String, Object>) operations.get("operations");

List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
for (CodegenOperation op : ops) {
// Overwrite path to map variable with path parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public JavaJAXRSSpecServerCodegen() {
apiPackage = "org.openapitools.api";
modelPackage = "org.openapitools.model";

// clioOptions default redifinition need to be updated
// cliOptions default redefinition need to be updated
updateOption(CodegenConstants.INVOKER_PACKAGE, this.getInvokerPackage());
updateOption(CodegenConstants.ARTIFACT_ID, this.getArtifactId());
updateOption(CodegenConstants.API_PACKAGE, apiPackage);
Expand All @@ -80,13 +80,7 @@ public JavaJAXRSSpecServerCodegen() {

super.embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "spec";

for (int i = 0; i < cliOptions.size(); i++) {
if (CodegenConstants.LIBRARY.equals(cliOptions.get(i).getOpt())) {
cliOptions.remove(i);
break;
}
}

removeOption(CodegenConstants.LIBRARY);
CliOption library = new CliOption(CodegenConstants.LIBRARY, CodegenConstants.LIBRARY_DESC).defaultValue(DEFAULT_LIBRARY);
Map<String, String> supportedLibraries = new LinkedHashMap<>();
supportedLibraries.put(DEFAULT_LIBRARY, "JAXRS");
Expand Down
48 changes: 26 additions & 22 deletions modules/openapi-generator/src/main/resources/Groovy/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class {{classname}} {
def bodyParams
def contentType
{{#allParams}}{{#required}}
{{#allParams}}
{{#required}}
// verify required params are set
if ({{paramName}} == null) {
throw new RuntimeException("missing required params {{paramName}}")
}
{{/required}}{{/allParams}}
{{/required}}
{{/allParams}}

{{#queryParams}}
if ({{paramName}} != null) {
Expand All @@ -47,36 +49,38 @@ class {{classname}} {
contentType = '{{{mediaType}}}';
{{/consumes.0}}
{{/bodyParam}}
{{#bodyParams.0}}
{{^hasMore}}
bodyParams = {{paramName}}
{{/hasMore}}
{{#hasMore}}
bodyParams = [:]
bodyParams.put("{{baseName}}", {{paramName}})
{{/hasMore}}
{{/bodyParams.0}}
{{#bodyParams}}
// only one body parameter
if (1 == {{bodyParams.size}}) {
bodyParams = {{paramName}}
}
// array of body parameters
else {
bodyParams.put("{{baseName}}", {{paramName}})
}
{{#secondaryParam}}
bodyParams.put("{{baseName}}", {{paramName}})
{{/secondaryParam}}
{{/bodyParams}}

{{#hasFormParams}}
{{#consumes.0}}
contentType = '{{{mediaType}}}';
{{/consumes.0}}
{{#formParams.0}}
// only one form parameter
if (1 == {{formParams.size}}) {
bodyParams = {{paramName}}
}
// array of form parameters
else {
bodyParams = [:]
}
{{^hasMore}}
bodyParams = {{paramName}}
{{/hasMore}}
{{#hasMore}}
bodyParams = [:]
bodyParams.put("{{baseName}}", {{paramName}})
{{/hasMore}}
{{/formParams.0}}
{{#formParams}}
// array of form parameters
if (1 < {{formParams.size}}) {
bodyParams.put("{{baseName}}", {{paramName}})
}
{{#secondaryParam}}
bodyParams.put("{{baseName}}", {{paramName}})
{{/secondaryParam}}
{{/formParams}}
{{/hasFormParams}}

Expand Down
Loading

0 comments on commit 6bffe4d

Please sign in to comment.