Skip to content
Merged
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 @@ -17,8 +17,6 @@

package org.openapitools.codegen.languages;

import static java.util.Collections.sort;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
Expand All @@ -32,6 +30,7 @@
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
import org.openapitools.codegen.languages.features.GzipFeatures;
import org.openapitools.codegen.languages.features.PerformBeanValidationFeatures;
import org.openapitools.codegen.mustache.CaseFormatLambda;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -46,6 +45,10 @@
import java.util.Map;
import java.util.regex.Pattern;

import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
import static java.util.Collections.sort;

public class JavaClientCodegen extends AbstractJavaCodegen
implements BeanValidationFeatures, PerformBeanValidationFeatures,
GzipFeatures {
Expand Down Expand Up @@ -283,6 +286,7 @@ public void processOpts() {

} else if (REST_ASSURED.equals(getLibrary())) {
additionalProperties.put("gson", "true");
additionalProperties.put("convert", new CaseFormatLambda(LOWER_CAMEL, UPPER_UNDERSCORE));
apiTemplateFiles.put("api.mustache", ".java");
supportingFiles.add(new SupportingFile("ResponseSpecBuilders.mustache", invokerFolder, "ResponseSpecBuilders.java"));
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openapitools.codegen.mustache;

import com.google.common.base.CaseFormat;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import org.openapitools.codegen.CodegenConfig;

import java.io.IOException;
import java.io.Writer;

/**
* Converts text from CaseFormat to another CaseFormat
*
* Register:
* <pre>
* additionalProperties.put("convert", new CaseFormatLambda(LOWER_CAMEL, UPPER_UNDERSCORE));
* </pre>
*
* Use:
* <pre>
* {{#convert}}{{name}}{{/convert}}
* </pre>
*/
public class CaseFormatLambda implements Mustache.Lambda {
private CodegenConfig generator = null;

private CaseFormat initialFormat;
private CaseFormat targetFormat;

public CaseFormatLambda(CaseFormat target, CaseFormat targetFormat) {
this.initialFormat = target;
this.targetFormat = targetFormat;
}

public CaseFormatLambda generator(final CodegenConfig generator) {
this.generator = generator;
return this;
}

@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String text = initialFormat.converterTo(targetFormat).convert(fragment.execute());
if (generator != null && generator.reservedWords().contains(text)) {
text = generator.escapeReservedWord(text);
}
writer.write(text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,46 +151,54 @@ public class {{classname}} {
{{/bodyParams}}
{{#headerParams}}

public static final String {{#convert}}{{paramName}}{{/convert}}_HEADER = "{{baseName}}";

/**
* @param {{paramName}} ({{dataType}}) {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @return operation
*/
public {{operationIdCamelCase}}Oper {{paramName}}Header(String {{paramName}}) {
reqSpec.addHeader("{{baseName}}", {{paramName}});
reqSpec.addHeader({{#convert}}{{paramName}}{{/convert}}_HEADER, {{paramName}});
return this;
}
{{/headerParams}}
{{#pathParams}}

public static final String {{#convert}}{{paramName}}{{/convert}}_PATH = "{{baseName}}";

/**
* @param {{paramName}} ({{dataType}}) {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @return operation
*/
public {{operationIdCamelCase}}Oper {{paramName}}Path(Object {{paramName}}) {
reqSpec.addPathParam("{{baseName}}", {{paramName}});
reqSpec.addPathParam({{#convert}}{{paramName}}{{/convert}}_PATH, {{paramName}});
return this;
}
{{/pathParams}}
{{#queryParams}}

public static final String {{#convert}}{{paramName}}{{/convert}}_QUERY = "{{baseName}}";

/**
* @param {{paramName}} ({{dataType}}) {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @return operation
*/
public {{operationIdCamelCase}}Oper {{paramName}}Query(Object... {{paramName}}) {
reqSpec.addQueryParam("{{baseName}}", {{paramName}});
reqSpec.addQueryParam({{#convert}}{{paramName}}{{/convert}}_QUERY, {{paramName}});
return this;
}
{{/queryParams}}
{{#formParams}}
{{^isFile}}

public static final String {{#convert}}{{paramName}}{{/convert}}_FORM = "{{baseName}}";

/**
* @param {{paramName}} ({{dataType}}) {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @return operation
*/
public {{operationIdCamelCase}}Oper {{paramName}}Form(Object... {{paramName}}) {
reqSpec.addFormParam("{{baseName}}", {{paramName}});
reqSpec.addFormParam({{#convert}}{{paramName}}{{/convert}}_FORM, {{paramName}});
return this;
}
{{/isFile}}
Expand Down
Loading