forked from loafle/openapi-generator-original
[Jaxrs-Resteasy] Add beanvalidation annotations (#4506)
* add beanvalidation to jaxrs-resteasy #4091 * replace tabs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.*;
|
||||
import io.swagger.codegen.languages.features.BeanValidationFeatures;
|
||||
import io.swagger.codegen.languages.features.JbossFeature;
|
||||
import io.swagger.models.Operation;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
@@ -9,10 +10,11 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
public class JavaResteasyServerCodegen extends AbstractJavaJAXRSServerCodegen implements JbossFeature {
|
||||
public class JavaResteasyServerCodegen extends AbstractJavaJAXRSServerCodegen implements JbossFeature, BeanValidationFeatures {
|
||||
|
||||
protected boolean useBeanValidation = true;
|
||||
protected boolean generateJbossDeploymentDescriptor = true;
|
||||
|
||||
|
||||
public JavaResteasyServerCodegen() {
|
||||
|
||||
super();
|
||||
@@ -35,6 +37,7 @@ public class JavaResteasyServerCodegen extends AbstractJavaJAXRSServerCodegen im
|
||||
|
||||
embeddedTemplateDir = templateDir = "JavaJaxRS" + File.separator + "resteasy";
|
||||
|
||||
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
|
||||
cliOptions.add(
|
||||
CliOption.newBoolean(GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR, "Generate Jboss Deployment Descriptor"));
|
||||
}
|
||||
@@ -58,6 +61,14 @@ public class JavaResteasyServerCodegen extends AbstractJavaJAXRSServerCodegen im
|
||||
GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR);
|
||||
this.setGenerateJbossDeploymentDescriptor(generateJbossDeploymentDescriptorProp);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
|
||||
this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION));
|
||||
}
|
||||
|
||||
if (useBeanValidation) {
|
||||
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
|
||||
}
|
||||
|
||||
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
|
||||
writeOptional(outputFolder, new SupportingFile("gradle.mustache", "", "build.gradle"));
|
||||
@@ -214,6 +225,10 @@ public class JavaResteasyServerCodegen extends AbstractJavaJAXRSServerCodegen im
|
||||
|
||||
return objs;
|
||||
}
|
||||
|
||||
public void setUseBeanValidation(boolean useBeanValidation) {
|
||||
this.useBeanValidation = useBeanValidation;
|
||||
}
|
||||
|
||||
public void setGenerateJbossDeploymentDescriptor(boolean generateJbossDeploymentDescriptor) {
|
||||
this.generateJbossDeploymentDescriptor = generateJbossDeploymentDescriptor;
|
||||
|
||||
@@ -16,6 +16,9 @@ import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.SecurityContext;
|
||||
import javax.ws.rs.*;
|
||||
{{#useBeanValidation}}
|
||||
import javax.validation.constraints.*;
|
||||
{{/useBeanValidation}}
|
||||
{{#operations}}{{#operation}}{{#isMultipart}}import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
|
||||
{{/isMultipart}}{{/operation}}{{/operations}}
|
||||
@Path("/{{baseName}}")
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
{{#required}}
|
||||
@NotNull
|
||||
{{/required}}
|
||||
{{#pattern}}
|
||||
@Pattern(regexp="{{pattern}}")
|
||||
{{/pattern}}
|
||||
{{#minLength}}
|
||||
{{#maxLength}}
|
||||
@Size(min={{minLength}},max={{maxLength}})
|
||||
{{/maxLength}}
|
||||
{{/minLength}}
|
||||
{{#minLength}}
|
||||
{{^maxLength}}
|
||||
@Size(min={{minLength}})
|
||||
{{/maxLength}}
|
||||
{{/minLength}}
|
||||
{{^minLength}}
|
||||
{{#maxLength}}
|
||||
@Size(max={{maxLength}})
|
||||
{{/maxLength}}
|
||||
{{/minLength}}
|
||||
{{#minItems}}
|
||||
{{#maxItems}}
|
||||
@Size(min={{minItems}},max={{maxItems}})
|
||||
{{/maxItems}}
|
||||
{{/minItems}}
|
||||
{{#minItems}}
|
||||
{{^maxItems}}
|
||||
@Size(min={{minItems}})
|
||||
{{/maxItems}}
|
||||
{{/minItems}}
|
||||
{{^minItems}}
|
||||
{{#maxItems}}
|
||||
@Size(max={{maxItems}})
|
||||
{{/maxItems}}
|
||||
{{/minItems}}
|
||||
{{! check for integer / number=decimal type}}
|
||||
{{#isInteger}}
|
||||
{{#minimum}}
|
||||
@Min({{minimum}})
|
||||
{{/minimum}}
|
||||
{{#maximum}}
|
||||
@Max({{maximum}})
|
||||
{{/maximum}}
|
||||
{{/isInteger}}
|
||||
{{^isInteger}}
|
||||
{{#minimum}}
|
||||
@DecimalMin("{{minimum}}")
|
||||
{{/minimum}}
|
||||
{{#maximum}}
|
||||
@DecimalMax("{{maximum}}")
|
||||
{{/maximum}}
|
||||
{{/isInteger}}
|
||||
@@ -0,0 +1 @@
|
||||
{{! PathParam is always required, no @NotNull necessary }}{{#pattern}} @Pattern(regexp="{{pattern}}"){{/pattern}}{{#minLength}}{{#maxLength}} @Size(min={{minLength}},max={{maxLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} @Size(min={{minLength}}){{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} @Size(max={{maxLength}}){{/maxLength}}{{/minLength}}{{#minItems}}{{#maxItems}} @Size(min={{minItems}},max={{maxItems}}){{/maxItems}}{{/minItems}}{{#minItems}}{{^maxItems}} @Size(min={{minItems}}){{/maxItems}}{{/minItems}}{{^minItems}}{{#maxItems}} @Size(max={{maxItems}}){{/maxItems}}{{/minItems}}{{#minimum}} @Min({{minimum}}){{/minimum}}{{#maximum}} @Max({{maximum}}){{/maximum}}
|
||||
@@ -0,0 +1 @@
|
||||
{{#required}} @NotNull{{/required}}{{#pattern}} @Pattern(regexp="{{pattern}}"){{/pattern}}{{#minLength}}{{#maxLength}} @Size(min={{minLength}},max={{maxLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} @Size(min={{minLength}}){{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} @Size(max={{maxLength}}){{/maxLength}}{{/minLength}}{{#minItems}}{{#maxItems}} @Size(min={{minItems}},max={{maxItems}}){{/maxItems}}{{/minItems}}{{#minItems}}{{^maxItems}} @Size(min={{minItems}}){{/maxItems}}{{/minItems}}{{^minItems}}{{#maxItems}} @Size(max={{maxItems}}){{/maxItems}}{{/minItems}}{{#minimum}} @Min({{minimum}}){{/minimum}}{{#maximum}} @Max({{maximum}}){{/maximum}}
|
||||
@@ -7,7 +7,9 @@ import java.util.ArrayList;
|
||||
{{#serializableModel}}
|
||||
import java.io.Serializable;
|
||||
{{/serializableModel}}
|
||||
|
||||
{{#useBeanValidation}}
|
||||
import javax.validation.constraints.*;
|
||||
{{/useBeanValidation}}
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{#isEnum}}
|
||||
|
||||
@@ -1 +1 @@
|
||||
{{#isPathParam}} @PathParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isPathParam}}
|
||||
{{#isPathParam}}{{#useBeanValidation}}{{>beanValidationPathParams}}{{/useBeanValidation}} @PathParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isPathParam}}
|
||||
@@ -15,7 +15,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
|
||||
**/
|
||||
{{#vendorExtensions.extraAnnotation}}{{{vendorExtensions.extraAnnotation}}}{{/vendorExtensions.extraAnnotation}}
|
||||
@JsonProperty("{{baseName}}")
|
||||
public {{{datatypeWithEnum}}} {{getter}}() {
|
||||
{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}} public {{{datatypeWithEnum}}} {{getter}}() {
|
||||
return {{name}};
|
||||
}
|
||||
public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
|
||||
|
||||
@@ -138,6 +138,16 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
{{#useBeanValidation}}
|
||||
<!-- Bean Validation API support -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>1.1.0.Final</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
{{/useBeanValidation}}
|
||||
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
|
||||
@@ -1 +1 @@
|
||||
{{#isQueryParam}} @QueryParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}}
|
||||
{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}} @QueryParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}}
|
||||
@@ -59,6 +59,8 @@ public class JavaResteasyServerOptionsTest extends AbstractOptionsTest {
|
||||
|
||||
clientCodegen.setGenerateJbossDeploymentDescriptor(
|
||||
Boolean.valueOf(JavaResteasyServerOptionsProvider.GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR));
|
||||
|
||||
clientCodegen.setUseBeanValidation(Boolean.valueOf(JavaResteasyServerOptionsProvider.USE_BEANVALIDATION));
|
||||
times = 1;
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.languages.JavaCXFServerCodegen;
|
||||
import io.swagger.codegen.languages.JavaResteasyServerCodegen;
|
||||
|
||||
public class JavaResteasyServerOptionsProvider extends JavaOptionsProvider {
|
||||
|
||||
@@ -13,6 +14,8 @@ public class JavaResteasyServerOptionsProvider extends JavaOptionsProvider {
|
||||
|
||||
public static final String IMPL_FOLDER_VALUE = "src/main/java";
|
||||
|
||||
public static final String USE_BEANVALIDATION = "true";
|
||||
|
||||
@Override
|
||||
public boolean isServer() {
|
||||
return true;
|
||||
@@ -35,6 +38,7 @@ public class JavaResteasyServerOptionsProvider extends JavaOptionsProvider {
|
||||
builder.put("title", "Test title");
|
||||
|
||||
builder.put(JavaCXFServerCodegen.GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR, GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR);
|
||||
builder.put(JavaResteasyServerCodegen.USE_BEANVALIDATION, USE_BEANVALIDATION);
|
||||
|
||||
return builder.build();
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.SecurityContext;
|
||||
import javax.ws.rs.*;
|
||||
import javax.validation.constraints.*;
|
||||
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
|
||||
|
||||
@Path("/pet")
|
||||
@@ -46,7 +47,7 @@ public class PetApi {
|
||||
@Path("/findByStatus")
|
||||
|
||||
@Produces({ "application/xml", "application/json" })
|
||||
public Response findPetsByStatus( @QueryParam("status") List<String> status,@Context SecurityContext securityContext)
|
||||
public Response findPetsByStatus( @NotNull @QueryParam("status") List<String> status,@Context SecurityContext securityContext)
|
||||
throws NotFoundException {
|
||||
return delegate.findPetsByStatus(status,securityContext);
|
||||
}
|
||||
@@ -54,7 +55,7 @@ public class PetApi {
|
||||
@Path("/findByTags")
|
||||
|
||||
@Produces({ "application/xml", "application/json" })
|
||||
public Response findPetsByTags( @QueryParam("tags") List<String> tags,@Context SecurityContext securityContext)
|
||||
public Response findPetsByTags( @NotNull @QueryParam("tags") List<String> tags,@Context SecurityContext securityContext)
|
||||
throws NotFoundException {
|
||||
return delegate.findPetsByTags(tags,securityContext);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.SecurityContext;
|
||||
import javax.ws.rs.*;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
@Path("/store")
|
||||
|
||||
@@ -28,7 +29,7 @@ public class StoreApi {
|
||||
@Path("/order/{orderId}")
|
||||
|
||||
@Produces({ "application/xml", "application/json" })
|
||||
public Response deleteOrder( @PathParam("orderId") String orderId,@Context SecurityContext securityContext)
|
||||
public Response deleteOrder( @Min(1) @PathParam("orderId") String orderId,@Context SecurityContext securityContext)
|
||||
throws NotFoundException {
|
||||
return delegate.deleteOrder(orderId,securityContext);
|
||||
}
|
||||
@@ -44,7 +45,7 @@ public class StoreApi {
|
||||
@Path("/order/{orderId}")
|
||||
|
||||
@Produces({ "application/xml", "application/json" })
|
||||
public Response getOrderById( @PathParam("orderId") Long orderId,@Context SecurityContext securityContext)
|
||||
public Response getOrderById( @Min(1) @Max(5) @PathParam("orderId") Long orderId,@Context SecurityContext securityContext)
|
||||
throws NotFoundException {
|
||||
return delegate.getOrderById(orderId,securityContext);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.SecurityContext;
|
||||
import javax.ws.rs.*;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
@Path("/user")
|
||||
|
||||
@@ -68,7 +69,7 @@ public class UserApi {
|
||||
@Path("/login")
|
||||
|
||||
@Produces({ "application/xml", "application/json" })
|
||||
public Response loginUser( @QueryParam("username") String username, @QueryParam("password") String password,@Context SecurityContext securityContext)
|
||||
public Response loginUser( @NotNull @QueryParam("username") String username, @NotNull @QueryParam("password") String password,@Context SecurityContext securityContext)
|
||||
throws NotFoundException {
|
||||
return delegate.loginUser(username,password,securityContext);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.ArrayList;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
public class Category {
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.ArrayList;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
public class ModelApiResponse {
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
public class Order {
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.model.Category;
|
||||
import io.swagger.model.Tag;
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
public class Pet {
|
||||
|
||||
@@ -69,6 +69,7 @@ public class Pet {
|
||||
**/
|
||||
|
||||
@JsonProperty("name")
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@@ -80,6 +81,7 @@ public class Pet {
|
||||
**/
|
||||
|
||||
@JsonProperty("photoUrls")
|
||||
@NotNull
|
||||
public List<String> getPhotoUrls() {
|
||||
return photoUrls;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.ArrayList;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
public class Tag {
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.ArrayList;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
public class User {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user