Skip to content
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

[Java] BeanValidation + JAXRS CXF server generator #4068

Merged
merged 85 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
730d41d
add first implementation of @NotNull, @Size, @Pattern, @Min, @Max #2549
wing328 Jul 2, 2016
26f4189
Merge remote-tracking branch 'upstream/master' into HEAD
jfiala Jul 2, 2016
ac328cc
add pom-file to cxf including cxf-client #2017
jfiala Jul 2, 2016
4766b9b
adapt pom sourceFolder in pom.mustache to gen/java #2017
jfiala Jul 2, 2016
ed5039b
add test templates for CXF #2017
jfiala Jul 2, 2016
7b375d3
optimize mustache template structure, remove tabs #3280 #2549
jfiala Jul 2, 2016
956432a
refined dependency to swagger-jaxrs #2017
jfiala Jul 2, 2016
f420da0
refined annotations and line breaks #2549
jfiala Jul 2, 2016
ee34013
fix tabs instead of spaces...
jfiala Jul 3, 2016
d47eaa9
move annotations from getter to field for server model
jfiala Jul 3, 2016
8c8f587
cleanup tabs
jfiala Jul 3, 2016
e44bb1e
add example for gzipping #2017
jfiala Jul 3, 2016
bd8dac6
Merge branch 'jaxrs_cxf_2017' of https://github.com/jfiala/swagger-co…
jfiala Jul 3, 2016
6b1b883
add complete WAR to cxf-server #2017
jfiala Jul 3, 2016
bfc597c
add readme + rework pom #2017
jfiala Jul 3, 2016
06144f0
add cli switch for beanvalidation #2549
jfiala Sep 10, 2016
2dfc291
move beanvalidation into separate subtemplate and reuse for cxf
jfiala Sep 11, 2016
4b7c322
add beanValidation java template
jfiala Sep 11, 2016
245b63a
corrected path to mustache file
jfiala Sep 11, 2016
2d8b8a9
fix AbstractOptionsTest, add default false for USE_BEANVALIDATION
jfiala Oct 10, 2016
cc0e942
add pom-file to cxf including cxf-client #2017
jfiala Jul 2, 2016
008c558
adapt pom sourceFolder in pom.mustache to gen/java #2017
jfiala Jul 2, 2016
8dab093
add test templates for CXF #2017
jfiala Jul 2, 2016
b34db72
optimize mustache template structure, remove tabs #3280 #2549
jfiala Jul 2, 2016
fa02f47
refined dependency to swagger-jaxrs #2017
jfiala Jul 2, 2016
4786f25
refined annotations and line breaks #2549
jfiala Jul 2, 2016
b26aaa8
fix tabs instead of spaces...
jfiala Jul 3, 2016
52ca86f
move annotations from getter to field for server model
jfiala Jul 3, 2016
681ac8d
cleanup tabs
jfiala Jul 3, 2016
320f7b6
add example for gzipping #2017
jfiala Jul 3, 2016
a8811c3
add complete WAR to cxf-server #2017
jfiala Jul 3, 2016
4da6712
add readme + rework pom #2017
jfiala Jul 3, 2016
4df86dc
add cli switch for beanvalidation #2549
jfiala Sep 10, 2016
fb6fed9
move beanvalidation into separate subtemplate and reuse for cxf
jfiala Sep 11, 2016
6aa5cd6
add beanValidation java template
jfiala Sep 11, 2016
94cacdc
corrected path to mustache file
jfiala Sep 11, 2016
d99929c
fix AbstractOptionsTest, add default false for USE_BEANVALIDATION
jfiala Oct 10, 2016
a960003
Merge branch 'jaxrs_cxf_2017' of https://github.com/jfiala/swagger-co…
jfiala Oct 14, 2016
fc2b3ee
add beanvalidation to okhttp-gson library #2549
jfiala Oct 14, 2016
e62a357
cleanup api.mustache for cxf #3281
jfiala Oct 14, 2016
a57947f
temporary fix for invalid BeanValidation-pattern with / #2549
jfiala Oct 14, 2016
65e87f2
temporary fix for invalid BeanValidation-pattern with / #2549
jfiala Oct 14, 2016
82df5e6
temporary solution for pattern - move to AbstractJavaCodegen #2549
jfiala Oct 14, 2016
c0e56fe
remove jaxrs-annotations from implementation classes #3281
jfiala Oct 14, 2016
a9619dd
add configuration to generate spring web application #4003
jfiala Oct 14, 2016
e5b23bb
clean up unnecessary @Path annotation (part of method level!)
jfiala Oct 15, 2016
d35e3d2
add support for minItems/maxItems #2549
jfiala Oct 15, 2016
cc0d084
add support for BeanValidation for query params #2549
jfiala Oct 15, 2016
a608e56
add imports for bean validation #2549
jfiala Oct 15, 2016
8d8c66f
add switches for various cxf server features #2017
jfiala Oct 15, 2016
cbf7c62
fix tests #2549 #2017
jfiala Oct 15, 2016
8a6d413
re-create bin folder
jfiala Oct 15, 2016
ac44cc2
cleanup
jfiala Oct 15, 2016
c752ba8
cleanup
jfiala Oct 15, 2016
94f78e6
Revert "temporary solution for pattern - move to AbstractJavaCodegen …
jfiala Oct 15, 2016
e17e385
Revert "cleanup"
jfiala Oct 15, 2016
4369b35
Revert "re-create bin folder"
jfiala Oct 15, 2016
29e2948
Revert "re-create bin folder"
jfiala Oct 15, 2016
d4b5c9a
re-create bin from master
jfiala Oct 15, 2016
3fdc1ec
Revert "re-create bin from master"
jfiala Oct 15, 2016
2ebc0df
Revert "Revert "re-create bin folder""
jfiala Oct 15, 2016
6815c62
Revert "re-create bin folder"
jfiala Oct 15, 2016
8ab1949
Revert "temporary solution for pattern - move to AbstractJavaCodegen …
jfiala Oct 15, 2016
a90a139
correct @Path to re-activate Swagger support #
jfiala Oct 15, 2016
e3212b5
change sourceFolder to default
jfiala Oct 15, 2016
5bcef56
re-activate unittest generator + support gzip switch #2017
jfiala Oct 15, 2016
2fb1b16
add switch to support Spring Boot + integration tests
jfiala Oct 16, 2016
cf3056f
add cxf client generator, rename cxf server generator #2017
jfiala Oct 16, 2016
5afdf6b
add interfaces for language feature support #4003
jfiala Oct 16, 2016
ce865bd
add comments #2017
jfiala Oct 16, 2016
7fd3f71
correct reference to context.xml.mustache #4003
jfiala Oct 16, 2016
f473fa8
use dynamic sourceFolder path #2017
jfiala Oct 16, 2016
51e1eee
replace tabs with spaces #4003
jfiala Oct 17, 2016
a8d25ad
use hard-coded sourceFolder #4003
jfiala Oct 17, 2016
437b230
rename language for backwards compatibility
jfiala Oct 17, 2016
1306e56
cleanup todo comment #4003
jfiala Oct 17, 2016
a6d53f2
move convertPropertyToBooleanAndWriteBack to DefaultCodegen #4003
jfiala Oct 17, 2016
f92741a
change reference to beanValidation.mustache to local copy for cxf
jfiala Oct 23, 2016
bf7e3d8
cleanup client-side beanvalidation (currently not working in CXF)
jfiala Oct 23, 2016
6f0d529
add support for tags #4003
jfiala Oct 23, 2016
38eb916
fix instantiiation of method parameters
jfiala Oct 24, 2016
6f2a93d
Merge branch 'jaxrs_cxf_2017' of https://github.com/jfiala/swagger-co…
wing328 Oct 24, 2016
0ddb35f
Merge branch 'jaxrs_cxf_2017' of https://github.com/jfiala/swagger-co…
wing328 Oct 24, 2016
1267e0f
move commonValidationFeature from bus to endpoint #4003
jfiala Oct 24, 2016
b85af0d
Merge branch 'jaxrs_cxf_2017' of https://github.com/jfiala/swagger-co…
wing328 Oct 24, 2016
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 @@ -3330,4 +3330,23 @@ public String addRegularExpressionDelimiter(String pattern) {

return pattern;
}

/**
* reads propertyKey from additionalProperties, converts it to a boolean and
* writes it back to additionalProperties to be usable as a boolean in
* mustache files.
*
* @param propertyKey
* @return property value as boolean
*/
public boolean convertPropertyToBooleanAndWriteBack(String propertyKey) {
boolean booleanValue = false;
if (additionalProperties.containsKey(propertyKey)) {
booleanValue = Boolean.valueOf(additionalProperties.get(propertyKey).toString());
// write back as boolean
additionalProperties.put(propertyKey, booleanValue);
}

return booleanValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -935,4 +935,8 @@ private String deriveInvokerPackageName(String input) {
return sb.toString();
}

public String toRegularExpression(String pattern) {
return escapeText(pattern);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@

package io.swagger.codegen.languages;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.languages.features.CXFFeatures;
import io.swagger.codegen.languages.features.LoggingFeatures;
import io.swagger.models.Operation;

public class JavaCXFClientCodegen extends AbstractJavaCodegen implements CXFFeatures
{
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCXFClientCodegen.class);

/**
* Name of the sub-directory in "src/main/resource" where to find the
* Mustache template for the JAX-RS Codegen.
*/
protected static final String JAXRS_TEMPLATE_DIRECTORY_NAME = "JavaJaxRS";

protected boolean useBeanValidation = false;

protected boolean useGzipFeature = false;

protected boolean useLoggingFeature = false;

protected boolean useBeanValidationFeature = false;


public JavaCXFClientCodegen()
{
super();

supportsInheritance = true;

sourceFolder = "src/gen/java";
invokerPackage = "io.swagger.api";
artifactId = "swagger-jaxrs-client";
dateLibrary = "legacy"; //TODO: add joda support to all jax-rs

apiPackage = "io.swagger.api";
modelPackage = "io.swagger.model";

outputFolder = "generated-code/JavaJaxRS-CXF";

// clear model and api doc template as this codegen
// does not support auto-generated markdown doc at the moment
//TODO: add doc templates
modelDocTemplateFiles.remove("model_doc.mustache");
apiDocTemplateFiles.remove("api_doc.mustache");


typeMapping.put("date", "LocalDate");
typeMapping.put("DateTime", "javax.xml.datatype.XMLGregorianCalendar"); // Map DateTime fields to Java standart class 'XMLGregorianCalendar'

importMapping.put("LocalDate", "org.joda.time.LocalDate");

embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "cxf";

cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));

cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE, "Use Gzip Feature"));
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION_FEATURE, "Use BeanValidation Feature"));
cliOptions.add(CliOption.newBoolean(USE_LOGGING_FEATURE, "Use Logging Feature"));


}


@Override
public void processOpts()
{
super.processOpts();

if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
boolean useBeanValidationProp = convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION);
this.setUseBeanValidation(useBeanValidationProp);
}

this.setUseGzipFeature(convertPropertyToBooleanAndWriteBack(USE_GZIP_FEATURE));
this.setUseLoggingFeature(convertPropertyToBooleanAndWriteBack(USE_LOGGING_FEATURE));

boolean useBeanValidationFeature = convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION_FEATURE);
this.setUseBeanValidationFeature(useBeanValidationFeature);
if (useBeanValidationFeature) {
LOGGER.info("make sure your client supports Bean Validation 1.1");
}

supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen

writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));

}

@Override
public String getName()
{
return "jaxrs-cxf-client";
}


@Override
public CodegenType getTag()
{
return CodegenType.CLIENT;
}

@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
co.subresourceOperation = !co.path.isEmpty();
}

@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);
model.imports.remove("ApiModelProperty");
model.imports.remove("ApiModel");
model.imports.remove("JsonSerialize");
model.imports.remove("ToStringSerializer");
}

@Override
public String getHelp()
{
return "Generates a Java JAXRS Client based on Apache CXF framework.";
}

public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
}


public void setUseGzipFeature(boolean useGzipFeature) {
this.useGzipFeature = useGzipFeature;
}


public void setUseLoggingFeature(boolean useLoggingFeature) {
this.useLoggingFeature = useLoggingFeature;
}


public void setUseBeanValidationFeature(boolean useBeanValidationFeature) {
this.useBeanValidationFeature = useBeanValidationFeature;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,51 @@
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.languages.features.CXFServerFeatures;
import io.swagger.models.Operation;

public class JavaCXFServerCodegen extends AbstractJavaJAXRSServerCodegen
{
public JavaCXFServerCodegen()
{
public class JavaCXFServerCodegen extends AbstractJavaJAXRSServerCodegen implements CXFServerFeatures
{
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCXFServerCodegen.class);

protected boolean useBeanValidation = false;

protected boolean generateSpringApplication = false;

protected boolean useSwaggerFeature = false;

protected boolean useWadlFeature = false;

protected boolean useMultipartFeature = false;

protected boolean useGzipFeature = false;

protected boolean useLoggingFeature = false;

protected boolean useBeanValidationFeature = false;

protected boolean generateSpringBootApplication= false;

public JavaCXFServerCodegen()
{
super();
sourceFolder = "gen" + File.separator + "java";
outputFolder = "generated-code/JavaJaxRS-CXF";
apiTestTemplateFiles.clear(); // TODO: add test template

//TODO add auto-generated pom.xml for maven
//apiTemplateFiles.put("pom.mustache", "pom.xml");

supportsInheritance = true;

artifactId = "swagger-cxf-server";

outputFolder = "generated-code/JavaJaxRS-CXF";

apiTemplateFiles.put("apiServiceImpl.mustache", ".java");

// clear model and api doc template as this codegen
// does not support auto-generated markdown doc at the moment
//TODO: add doc templates
Expand All @@ -36,14 +64,86 @@ public JavaCXFServerCodegen()

embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "cxf";

}
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
cliOptions.add(CliOption.newBoolean(GENERATE_SPRING_APPLICATION, "Generate Spring application"));

cliOptions.add(CliOption.newBoolean(USE_SWAGGER_FEATURE, "Use Swagger Feature"));
cliOptions.add(CliOption.newBoolean(USE_WADL_FEATURE, "Use WADL Feature"));
cliOptions.add(CliOption.newBoolean(USE_MULTIPART_FEATURE, "Use Multipart Feature"));
cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE, "Use Gzip Feature"));
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION_FEATURE, "Use BeanValidation Feature"));
cliOptions.add(CliOption.newBoolean(USE_LOGGING_FEATURE, "Use Logging Feature"));

cliOptions.add(CliOption.newBoolean(GENERATE_SPRING_BOOT_APPLICATION, "Generate Spring Boot application"));


}


@Override
public String getName()
{
return "jaxrs-cxf";
}
@Override
public void processOpts()
{
super.processOpts();

if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
boolean useBeanValidationProp = convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION);
this.setUseBeanValidation(useBeanValidationProp);
}

if (additionalProperties.containsKey(GENERATE_SPRING_APPLICATION)) {
this.setGenerateSpringApplication(convertPropertyToBooleanAndWriteBack(GENERATE_SPRING_APPLICATION));

this.setUseSwaggerFeature(convertPropertyToBooleanAndWriteBack(USE_SWAGGER_FEATURE));
this.setUseWadlFeature(convertPropertyToBooleanAndWriteBack(USE_WADL_FEATURE));
this.setUseMultipartFeature(convertPropertyToBooleanAndWriteBack(USE_MULTIPART_FEATURE));
this.setUseGzipFeature(convertPropertyToBooleanAndWriteBack(USE_GZIP_FEATURE));
this.setUseLoggingFeature(convertPropertyToBooleanAndWriteBack(USE_LOGGING_FEATURE));

boolean useBeanValidationFeature = convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION_FEATURE);
this.setUseBeanValidationFeature(useBeanValidationFeature);
if (useBeanValidationFeature) {
LOGGER.info("make sure your target server supports Bean Validation 1.1");
}

this.setGenerateSpringBootApplication(convertPropertyToBooleanAndWriteBack(GENERATE_SPRING_BOOT_APPLICATION));
}


supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen

writeOptional(outputFolder, new SupportingFile("server/pom.mustache", "", "pom.xml"));

if (this.generateSpringApplication) {
writeOptional(outputFolder, new SupportingFile("server/readme.md", "", "readme.md"));

writeOptional(outputFolder, new SupportingFile("server/ApplicationContext.xml.mustache",
("src/main/resources"), "ApplicationContext.xml"));
writeOptional(outputFolder, new SupportingFile("server/web.mustache",
("src/main/webapp/WEB-INF"), "web.xml"));
writeOptional(outputFolder, new SupportingFile("server/context.xml.mustache",
("src/main/webapp/WEB-INF"), "context.xml"));

// Jboss
writeOptional(outputFolder, new SupportingFile("server/jboss-web.xml.mustache",
("src/main/webapp/WEB-INF"), "jboss-web.xml"));

// Spring Boot
if (this.generateSpringBootApplication) {
writeOptional(outputFolder, new SupportingFile("server/SpringBootApplication.mustache",
(testFolder + '/' + apiPackage).replace(".", "/"), "SpringBootApplication.java"));

}

}


}

@Override
public String getName()
{
return "jaxrs-cxf";
}

@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
Expand All @@ -65,4 +165,46 @@ public String getHelp()
{
return "Generates a Java JAXRS Server application based on Apache CXF framework.";
}

public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
}

public void setGenerateSpringApplication(boolean generateSpringApplication) {
this.generateSpringApplication = generateSpringApplication;
}


public void setUseSwaggerFeature(boolean useSwaggerFeature) {
this.useSwaggerFeature = useSwaggerFeature;
}


public void setUseWadlFeature(boolean useWadlFeature) {
this.useWadlFeature = useWadlFeature;
}


public void setUseMultipartFeature(boolean useMultipartFeature) {
this.useMultipartFeature = useMultipartFeature;
}


public void setUseGzipFeature(boolean useGzipFeature) {
this.useGzipFeature = useGzipFeature;
}


public void setUseLoggingFeature(boolean useLoggingFeature) {
this.useLoggingFeature = useLoggingFeature;
}


public void setUseBeanValidationFeature(boolean useBeanValidationFeature) {
this.useBeanValidationFeature = useBeanValidationFeature;
}

public void setGenerateSpringBootApplication(boolean generateSpringBootApplication) {
this.generateSpringBootApplication = generateSpringBootApplication;
}
}
Loading