CaseFormatLambda has been added, params for Rest-assured client has been refactored (#91)

This commit is contained in:
Victor Orlovsky
2018-05-18 13:49:34 +03:00
committed by Jérémie Bresson
parent e55ba567a9
commit 3b9a2a7c36
7 changed files with 189 additions and 47 deletions

View File

@@ -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;
@@ -32,6 +30,7 @@ import org.openapitools.codegen.SupportingFile;
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;
@@ -46,6 +45,10 @@ import java.util.List;
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 {
@@ -283,6 +286,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
} 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"));

View File

@@ -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);
}
}