mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 22:20:56 +00:00
[JavaPlayFramework] Add parameters for beanValidation in the application.conf + fix bugs (#6794)
* Add configuration to split input and output bean validations. When useBeanValidation is used, the variable are created in the application.conf file and can be tweaked by environment. For example, dev and stage can have true to both but only have input in prod. * Refactor of mustache tags for more clarity * sample generation with refactor * Fix a couple of bugs with the fake-endpoint yaml but there is still 2 cases where it doesn't work.
This commit is contained in:
parent
cab4fc0fed
commit
18ba90f5ac
@ -17,6 +17,17 @@
|
||||
|
||||
play.filters.headers.contentSecurityPolicy=null
|
||||
|
||||
{{#useBeanValidation}}
|
||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
||||
# This is very useful when testing but could add a lot of overhead if you return a lot of data. Benchmark have
|
||||
# shown that the time it takes to validate is exponential.
|
||||
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||
# respecting its contract, set the "output" property below to "false". Since there is not a lot of data as input for
|
||||
# an endpoint, I highly suggest you let the "input" property set to true.
|
||||
useInputBeanValidation=true
|
||||
useOutputBeanValidation=true
|
||||
{{/useBeanValidation}}
|
||||
|
||||
{{#handleExceptions}}
|
||||
play.http.errorHandler="swagger.ErrorHandler"
|
||||
{{/handleExceptions}}
|
||||
|
@ -1 +1 @@
|
||||
{{#isBoolean}}Boolean.valueOf({{/isBoolean}}{{#isInteger}}Integer.parseInt({{/isInteger}}{{#isDouble}}Double.parseDouble({{/isDouble}}{{#isLong}}Long.parseLong({{/isLong}}{{#isFloat}}Float.parseFloat({{/isFloat}}{{#isUuid}}UUID.fromString({{/isUuid}}{{#isDateTime}}OffsetDateTime.parse({{/isDateTime}}
|
||||
{{#isBoolean}}Boolean.valueOf({{/isBoolean}}{{#isInteger}}Integer.parseInt({{/isInteger}}{{#isDouble}}Double.parseDouble({{/isDouble}}{{#isLong}}Long.parseLong({{/isLong}}{{#isFloat}}Float.parseFloat({{/isFloat}}{{#isUuid}}UUID.fromString({{/isUuid}}{{#isDateTime}}OffsetDateTime.parse({{/isDateTime}}{{#isDate}}LocalDate.parse({{/isDate}}{{#isByteArray}}{{/isByteArray}}
|
@ -1 +1 @@
|
||||
{{#isBoolean}}){{/isBoolean}}{{#isInteger}}){{/isInteger}}{{#isDouble}}){{/isDouble}}{{#isLong}}){{/isLong}}{{#isFloat}}){{/isFloat}}{{#isUuid}}){{/isUuid}}{{#isDateTime}}){{/isDateTime}}
|
||||
{{#isBoolean}}){{/isBoolean}}{{#isInteger}}){{/isInteger}}{{#isDouble}}){{/isDouble}}{{#isLong}}){{/isLong}}{{#isFloat}}){{/isFloat}}{{#isUuid}}){{/isUuid}}{{#isDateTime}}){{/isDateTime}}{{#isDate}}){{/isDate}}{{#isByteArray}}.getBytes(){{/isByteArray}}
|
@ -1 +1 @@
|
||||
{{#items.isBoolean}}Boolean.valueOf({{/items.isBoolean}}{{#items.isInteger}}Integer.parseInt({{/items.isInteger}}{{#items.isDouble}}Double.parseDouble({{/items.isDouble}}{{#items.isLong}}Long.parseLong({{/items.isLong}}{{#items.isFloat}}Float.parseFloat({{/items.isFloat}}{{#items.isUuid}}UUID.fromString({{/items.isUuid}}{{#items.isDateTime}}OffsetDateTime.parse({{/items.isDateTime}}
|
||||
{{#items.isBoolean}}Boolean.valueOf({{/items.isBoolean}}{{#items.isInteger}}Integer.parseInt({{/items.isInteger}}{{#items.isDouble}}Double.parseDouble({{/items.isDouble}}{{#items.isLong}}Long.parseLong({{/items.isLong}}{{#items.isFloat}}Float.parseFloat({{/items.isFloat}}{{#items.isUuid}}UUID.fromString({{/items.isUuid}}{{#items.isDateTime}}OffsetDateTime.parse({{/items.isDateTime}}{{#items.isDate}}LocalDate.parse({{/items.isDate}}{{#isByteArray}}{{/isByteArray}}
|
@ -1 +1 @@
|
||||
{{#items.isBoolean}}){{/items.isBoolean}}{{#items.isInteger}}){{/items.isInteger}}{{#items.isDouble}}){{/items.isDouble}}{{#items.isLong}}){{/items.isLong}}{{#items.isFloat}}){{/items.isFloat}}{{#items.isUuid}}){{/items.isUuid}}{{#items.isDateTime}}){{/items.isDateTime}}
|
||||
{{#items.isBoolean}}){{/items.isBoolean}}{{#items.isInteger}}){{/items.isInteger}}{{#items.isDouble}}){{/items.isDouble}}{{#items.isLong}}){{/items.isLong}}{{#items.isFloat}}){{/items.isFloat}}{{#items.isUuid}}){{/items.isUuid}}{{#items.isDateTime}}){{/items.isDateTime}}{{#items.isDate}}){{/items.isDate}}{{#isByteArray}}.getBytes(){{/isByteArray}}
|
@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
{{#useBeanValidation}}
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
{{/useBeanValidation}}
|
||||
|
||||
{{#wrapCalls}}
|
||||
@ -35,13 +36,19 @@ public class {{classname}}Controller extends Controller {
|
||||
private final {{classname}}ControllerImp{{#useInterfaces}}Interface{{/useInterfaces}} imp;
|
||||
{{/controllerOnly}}
|
||||
private final ObjectMapper mapper;
|
||||
{{#useBeanValidation}}
|
||||
private final Configuration configuration;
|
||||
{{/useBeanValidation}}
|
||||
|
||||
@Inject
|
||||
private {{classname}}Controller({{^controllerOnly}}{{classname}}ControllerImp{{#useInterfaces}}Interface{{/useInterfaces}} imp{{/controllerOnly}}) {
|
||||
private {{classname}}Controller({{#useBeanValidation}}Configuration configuration{{^controllerOnly}}, {{/controllerOnly}}{{/useBeanValidation}}{{^controllerOnly}}{{classname}}ControllerImp{{#useInterfaces}}Interface{{/useInterfaces}} imp{{/controllerOnly}}) {
|
||||
{{^controllerOnly}}
|
||||
this.imp = imp;
|
||||
{{/controllerOnly}}
|
||||
mapper = new ObjectMapper();
|
||||
{{#useBeanValidation}}
|
||||
this.configuration = configuration;
|
||||
{{/useBeanValidation}}
|
||||
}
|
||||
|
||||
{{#operation}}
|
||||
@ -55,12 +62,21 @@ public class {{classname}}Controller extends Controller {
|
||||
if (node{{paramName}} != null) {
|
||||
{{paramName}} = mapper.readValue(node{{paramName}}.toString(), {{#isContainer}}new TypeReference<{{{dataType}}}>(){}{{/isContainer}}{{^isContainer}}{{{dataType}}}.class{{/isContainer}});
|
||||
{{#useBeanValidation}}
|
||||
{{#isListContainer}}for ({{{baseType}}} curItem : {{paramName}}) {
|
||||
curItem.validate();
|
||||
}{{/isListContainer}}{{#isMapContainer}}for (Map.Entry<String, {{{baseType}}}> entry : {{paramName}}.entrySet()) {
|
||||
entry.getValue().validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
{{#isListContainer}}
|
||||
for ({{{baseType}}} curItem : {{paramName}}) {
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
{{/isListContainer}}
|
||||
{{#isMapContainer}}
|
||||
for (Map.Entry<String, {{{baseType}}}> entry : {{paramName}}.entrySet()) {
|
||||
SwaggerUtils.validate(entry.getValue());
|
||||
}
|
||||
{{/isMapContainer}}
|
||||
{{^isContainer}}
|
||||
SwaggerUtils.validate({{paramName}});
|
||||
{{/isContainer}}
|
||||
}
|
||||
{{/isMapContainer}}{{^isContainer}}{{paramName}}.validate();{{/isContainer}}
|
||||
{{/useBeanValidation}}
|
||||
} else {
|
||||
{{#required}}
|
||||
@ -173,13 +189,30 @@ public class {{classname}}Controller extends Controller {
|
||||
{{/collectionFormat}}
|
||||
{{/headerParams}}
|
||||
{{^controllerOnly}}
|
||||
{{#returnType}}{{>returnTypesNoVoid}} obj = {{/returnType}}imp.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}{{^isResponseFile}}{{^returnTypeIsPrimitive}}{{#useBeanValidation}}
|
||||
{{#isListContainer}}for ({{{returnType}}} curItem : obj) {
|
||||
curItem.validate();
|
||||
}{{/isListContainer}}{{#isMapContainer}}for (Map.Entry<String, {{{returnType}}}> entry : obj.entrySet()) {
|
||||
entry.getValue().validate();
|
||||
{{#returnType}}{{>returnTypesNoVoid}} obj = {{/returnType}}imp.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
|
||||
{{#returnType}}
|
||||
{{^isResponseFile}}
|
||||
{{^returnTypeIsPrimitive}}
|
||||
{{#useBeanValidation}}
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
{{#isListContainer}}
|
||||
for ({{{returnType}}} curItem : obj) {
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
{{/isMapContainer}}{{^returnContainer}}obj.validate();{{/returnContainer}}{{/useBeanValidation}}{{/returnTypeIsPrimitive}}{{/isResponseFile}}{{/returnType}}
|
||||
{{/isListContainer}}
|
||||
{{#isMapContainer}}
|
||||
for (Map.Entry<String, {{{returnType}}}> entry : obj.entrySet()) {
|
||||
SwaggerUtils.validate(entry.getValue());
|
||||
}
|
||||
{{/isMapContainer}}
|
||||
{{^returnContainer}}
|
||||
SwaggerUtils.validate(obj);
|
||||
{{/returnContainer}}
|
||||
}
|
||||
{{/useBeanValidation}}
|
||||
{{/returnTypeIsPrimitive}}
|
||||
{{/isResponseFile}}
|
||||
{{/returnType}}
|
||||
{{#returnType}}
|
||||
{{^isResponseFile}}JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
|
@ -134,22 +134,4 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
{{#useBeanValidation}}
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<{{classname}}>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<{{classname}}> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
{{/useBeanValidation}}
|
||||
}
|
||||
|
@ -9,6 +9,13 @@ import java.lang.annotation.Target;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
{{#useBeanValidation}}
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
{{/useBeanValidation}}
|
||||
|
||||
public class SwaggerUtils {
|
||||
|
||||
{{#wrapCalls}}
|
||||
@ -19,6 +26,24 @@ public class SwaggerUtils {
|
||||
}
|
||||
{{/wrapCalls}}
|
||||
|
||||
{{#useBeanValidation}}
|
||||
public static <T> void validate(T obj) {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<T> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
{{/useBeanValidation}}
|
||||
|
||||
public static List<String> parametersToList(String collectionFormat, String[] values){
|
||||
List<String> params = new ArrayList<>();
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class Category {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Category>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Category> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,21 +114,5 @@ public class ModelApiResponse {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ModelApiResponse>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ModelApiResponse> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,21 +215,5 @@ public class Order {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Order>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Order> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,21 +234,5 @@ public class Pet {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Pet>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Pet> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class Tag {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Tag>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Tag> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,21 +224,5 @@ public class User {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<User> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -25,10 +26,12 @@ import swagger.SwaggerUtils.ApiAction;
|
||||
public class PetApiController extends Controller {
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private PetApiController() {
|
||||
private PetApiController(Configuration configuration) {
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -38,7 +41,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -98,7 +103,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -24,10 +25,12 @@ import swagger.SwaggerUtils.ApiAction;
|
||||
public class StoreApiController extends Controller {
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private StoreApiController() {
|
||||
private StoreApiController(Configuration configuration) {
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -52,7 +55,9 @@ public class StoreApiController extends Controller {
|
||||
Order body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Order.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -24,10 +25,12 @@ import swagger.SwaggerUtils.ApiAction;
|
||||
public class UserApiController extends Controller {
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private UserApiController() {
|
||||
private UserApiController(Configuration configuration) {
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -37,7 +40,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -50,8 +55,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -65,8 +72,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -114,7 +123,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
|
@ -9,6 +9,11 @@ import java.lang.annotation.Target;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
public class SwaggerUtils {
|
||||
|
||||
@With(ApiCall.class)
|
||||
@ -17,6 +22,22 @@ public class SwaggerUtils {
|
||||
public @interface ApiAction {
|
||||
}
|
||||
|
||||
public static <T> void validate(T obj) {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<T> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> parametersToList(String collectionFormat, String[] values){
|
||||
List<String> params = new ArrayList<>();
|
||||
|
||||
|
@ -17,6 +17,15 @@
|
||||
|
||||
play.filters.headers.contentSecurityPolicy=null
|
||||
|
||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
||||
# This is very useful when testing but could add a lot of overhead if you return a lot of data. Benchmark have
|
||||
# shown that the time it takes to validate is exponential.
|
||||
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||
# respecting its contract, set the "output" property below to "false". Since there is not a lot of data as input for
|
||||
# an endpoint, I highly suggest you let the "input" property set to true.
|
||||
useInputBeanValidation=true
|
||||
useOutputBeanValidation=true
|
||||
|
||||
play.http.errorHandler="swagger.ErrorHandler"
|
||||
|
||||
## Akka
|
||||
|
@ -112,21 +112,5 @@ public class AdditionalPropertiesClass {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<AdditionalPropertiesClass>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<AdditionalPropertiesClass> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,21 +95,5 @@ public class Animal {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Animal>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Animal> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,21 +51,5 @@ public class AnimalFarm extends ArrayList<Animal> {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<AnimalFarm>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<AnimalFarm> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,21 +82,5 @@ public class ArrayOfArrayOfNumberOnly {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ArrayOfArrayOfNumberOnly>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ArrayOfArrayOfNumberOnly> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,21 +82,5 @@ public class ArrayOfNumberOnly {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ArrayOfNumberOnly>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ArrayOfNumberOnly> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,21 +143,5 @@ public class ArrayTest {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ArrayTest>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ArrayTest> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -180,21 +180,5 @@ public class Capitalization {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Capitalization>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Capitalization> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,21 +72,5 @@ public class Cat extends Animal {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Cat>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Cat> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class Category {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Category>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Category> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,21 +70,5 @@ public class ClassModel {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ClassModel>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ClassModel> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,21 +70,5 @@ public class Client {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Client>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Client> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,21 +72,5 @@ public class Dog extends Animal {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Dog>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Dog> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,21 +164,5 @@ public class EnumArrays {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<EnumArrays>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<EnumArrays> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -233,21 +233,5 @@ public class EnumTest {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<EnumTest>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<EnumTest> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -369,21 +369,5 @@ public class FormatTest {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<FormatTest>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<FormatTest> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class HasOnlyReadOnly {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<HasOnlyReadOnly>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<HasOnlyReadOnly> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,21 +143,5 @@ public class MapTest {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<MapTest>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<MapTest> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,21 +131,5 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<MixedPropertiesAndAdditionalPropertiesClass>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<MixedPropertiesAndAdditionalPropertiesClass> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class Model200Response {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Model200Response>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Model200Response> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,21 +114,5 @@ public class ModelApiResponse {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ModelApiResponse>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ModelApiResponse> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,21 +70,5 @@ public class ModelReturn {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ModelReturn>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ModelReturn> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,21 +137,5 @@ public class Name {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Name>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Name> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,21 +72,5 @@ public class NumberOnly {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<NumberOnly>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<NumberOnly> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,21 +215,5 @@ public class Order {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Order>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Order> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,21 +116,5 @@ public class OuterComposite {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<OuterComposite>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<OuterComposite> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,21 +234,5 @@ public class Pet {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Pet>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Pet> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class ReadOnlyFirst {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ReadOnlyFirst>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ReadOnlyFirst> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,21 +70,5 @@ public class SpecialModelName {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<SpecialModelName>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<SpecialModelName> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class Tag {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Tag>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Tag> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,21 +224,5 @@ public class User {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<User> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -24,11 +25,13 @@ public class AnotherFakeApiController extends Controller {
|
||||
|
||||
private final AnotherFakeApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private AnotherFakeApiController(AnotherFakeApiControllerImpInterface imp) {
|
||||
private AnotherFakeApiController(Configuration configuration, AnotherFakeApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -38,12 +41,16 @@ public class AnotherFakeApiController extends Controller {
|
||||
Client body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Client.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
Client obj = imp.testSpecialTags(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -28,11 +29,13 @@ public class FakeApiController extends Controller {
|
||||
|
||||
private final FakeApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private FakeApiController(FakeApiControllerImpInterface imp) {
|
||||
private FakeApiController(Configuration configuration, FakeApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -42,7 +45,9 @@ public class FakeApiController extends Controller {
|
||||
Boolean body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Boolean.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
body = null;
|
||||
}
|
||||
@ -57,12 +62,16 @@ public class FakeApiController extends Controller {
|
||||
OuterComposite body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), OuterComposite.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
body = null;
|
||||
}
|
||||
OuterComposite obj = imp.fakeOuterCompositeSerialize(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -73,12 +82,16 @@ public class FakeApiController extends Controller {
|
||||
BigDecimal body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), BigDecimal.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
body = null;
|
||||
}
|
||||
BigDecimal obj = imp.fakeOuterNumberSerialize(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -89,7 +102,9 @@ public class FakeApiController extends Controller {
|
||||
String body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), String.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
body = null;
|
||||
}
|
||||
@ -104,12 +119,16 @@ public class FakeApiController extends Controller {
|
||||
Client body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Client.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
Client obj = imp.testClientModel(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -182,14 +201,14 @@ public class FakeApiController extends Controller {
|
||||
String valuebinary = (request().body().asMultipartFormData().asFormUrlEncoded().get("binary"))[0];
|
||||
byte[] binary;
|
||||
if (valuebinary != null) {
|
||||
binary = valuebinary;
|
||||
binary = valuebinary.getBytes();
|
||||
} else {
|
||||
binary = null;
|
||||
}
|
||||
String valuedate = (request().body().asMultipartFormData().asFormUrlEncoded().get("date"))[0];
|
||||
LocalDate date;
|
||||
if (valuedate != null) {
|
||||
date = valuedate;
|
||||
date = LocalDate.parse(valuedate);
|
||||
} else {
|
||||
date = null;
|
||||
}
|
||||
@ -280,6 +299,22 @@ public class FakeApiController extends Controller {
|
||||
return ok();
|
||||
}
|
||||
|
||||
@ApiAction
|
||||
public Result testInlineAdditionalProperties() throws Exception {
|
||||
JsonNode nodeparam = request().body().asJson();
|
||||
Object param;
|
||||
if (nodeparam != null) {
|
||||
param = mapper.readValue(nodeparam.toString(), Object.class);
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(param);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'param' parameter is required");
|
||||
}
|
||||
imp.testInlineAdditionalProperties(param);
|
||||
return ok();
|
||||
}
|
||||
|
||||
@ApiAction
|
||||
public Result testJsonFormData() throws Exception {
|
||||
String valueparam = (request().body().asMultipartFormData().asFormUrlEncoded().get("param"))[0];
|
||||
|
@ -56,6 +56,12 @@ public class FakeApiControllerImp implements FakeApiControllerImpInterface {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testInlineAdditionalProperties(Object param) throws Exception {
|
||||
//Do your magic!!!
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testJsonFormData(String param, String param2) throws Exception {
|
||||
//Do your magic!!!
|
||||
|
@ -29,6 +29,8 @@ public interface FakeApiControllerImpInterface {
|
||||
|
||||
void testEnumParameters(List<String> enumFormStringArray, String enumFormString, List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble) throws Exception;
|
||||
|
||||
void testInlineAdditionalProperties(Object param) throws Exception;
|
||||
|
||||
void testJsonFormData(String param, String param2) throws Exception;
|
||||
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -24,11 +25,13 @@ public class FakeClassnameTags123ApiController extends Controller {
|
||||
|
||||
private final FakeClassnameTags123ApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private FakeClassnameTags123ApiController(FakeClassnameTags123ApiControllerImpInterface imp) {
|
||||
private FakeClassnameTags123ApiController(Configuration configuration, FakeClassnameTags123ApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -38,12 +41,16 @@ public class FakeClassnameTags123ApiController extends Controller {
|
||||
Client body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Client.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
Client obj = imp.testClassname(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -26,11 +27,13 @@ public class PetApiController extends Controller {
|
||||
|
||||
private final PetApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private PetApiController(PetApiControllerImpInterface imp) {
|
||||
private PetApiController(Configuration configuration, PetApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -40,7 +43,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -74,8 +79,10 @@ public class PetApiController extends Controller {
|
||||
status.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByStatus(status);
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
@ -94,8 +101,10 @@ public class PetApiController extends Controller {
|
||||
tags.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByTags(tags);
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
@ -104,7 +113,9 @@ public class PetApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getPetById(Long petId) throws Exception {
|
||||
Pet obj = imp.getPetById(petId);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -115,7 +126,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -154,7 +167,9 @@ public class PetApiController extends Controller {
|
||||
}
|
||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -25,11 +26,13 @@ public class StoreApiController extends Controller {
|
||||
|
||||
private final StoreApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private StoreApiController(StoreApiControllerImpInterface imp) {
|
||||
private StoreApiController(Configuration configuration, StoreApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -49,7 +52,9 @@ public class StoreApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||
Order obj = imp.getOrderById(orderId);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -60,12 +65,16 @@ public class StoreApiController extends Controller {
|
||||
Order body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Order.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
Order obj = imp.placeOrder(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -25,11 +26,13 @@ public class UserApiController extends Controller {
|
||||
|
||||
private final UserApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private UserApiController(UserApiControllerImpInterface imp) {
|
||||
private UserApiController(Configuration configuration, UserApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -39,7 +42,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -53,8 +58,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -69,8 +76,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -88,7 +97,9 @@ public class UserApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getUserByName(String username) throws Exception {
|
||||
User obj = imp.getUserByName(username);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -126,7 +137,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
|
@ -9,6 +9,11 @@ import java.lang.annotation.Target;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
public class SwaggerUtils {
|
||||
|
||||
@With(ApiCall.class)
|
||||
@ -17,6 +22,22 @@ public class SwaggerUtils {
|
||||
public @interface ApiAction {
|
||||
}
|
||||
|
||||
public static <T> void validate(T obj) {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<T> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> parametersToList(String collectionFormat, String[] values){
|
||||
List<String> params = new ArrayList<>();
|
||||
|
||||
|
@ -17,6 +17,15 @@
|
||||
|
||||
play.filters.headers.contentSecurityPolicy=null
|
||||
|
||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
||||
# This is very useful when testing but could add a lot of overhead if you return a lot of data. Benchmark have
|
||||
# shown that the time it takes to validate is exponential.
|
||||
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||
# respecting its contract, set the "output" property below to "false". Since there is not a lot of data as input for
|
||||
# an endpoint, I highly suggest you let the "input" property set to true.
|
||||
useInputBeanValidation=true
|
||||
useOutputBeanValidation=true
|
||||
|
||||
play.http.errorHandler="swagger.ErrorHandler"
|
||||
|
||||
## Akka
|
||||
|
@ -16,6 +16,7 @@ POST /v2/fake/outer/string controllers.FakeApiController
|
||||
PATCH /v2/fake controllers.FakeApiController.testClientModel()
|
||||
POST /v2/fake controllers.FakeApiController.testEndpointParameters()
|
||||
GET /v2/fake controllers.FakeApiController.testEnumParameters()
|
||||
POST /v2/fake/inline-additionalProperties controllers.FakeApiController.testInlineAdditionalProperties()
|
||||
GET /v2/fake/jsonFormData controllers.FakeApiController.testJsonFormData()
|
||||
|
||||
#Functions for FakeClassnameTags123 API
|
||||
|
@ -1088,6 +1088,34 @@
|
||||
"x-accepts" : "application/json"
|
||||
}
|
||||
},
|
||||
"/fake/inline-additionalProperties" : {
|
||||
"post" : {
|
||||
"tags" : [ "fake" ],
|
||||
"summary" : "test inline additionalProperties",
|
||||
"description" : "",
|
||||
"operationId" : "testInlineAdditionalProperties",
|
||||
"consumes" : [ "application/json" ],
|
||||
"parameters" : [ {
|
||||
"in" : "body",
|
||||
"name" : "param",
|
||||
"description" : "request body",
|
||||
"required" : true,
|
||||
"schema" : {
|
||||
"type" : "object",
|
||||
"additionalProperties" : {
|
||||
"type" : "string"
|
||||
}
|
||||
}
|
||||
} ],
|
||||
"responses" : {
|
||||
"200" : {
|
||||
"description" : "successful operation"
|
||||
}
|
||||
},
|
||||
"x-contentType" : "application/json",
|
||||
"x-accepts" : "application/json"
|
||||
}
|
||||
},
|
||||
"/another-fake/dummy" : {
|
||||
"patch" : {
|
||||
"tags" : [ "$another-fake?" ],
|
||||
|
@ -91,6 +91,5 @@ public class Category {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -113,6 +113,5 @@ public class ModelApiResponse {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -213,6 +213,5 @@ public class Order {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -229,6 +229,5 @@ public class Pet {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -91,6 +91,5 @@ public class Tag {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -223,6 +223,5 @@ public class User {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import java.lang.annotation.Target;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class SwaggerUtils {
|
||||
|
||||
@With(ApiCall.class)
|
||||
@ -17,6 +18,7 @@ public class SwaggerUtils {
|
||||
public @interface ApiAction {
|
||||
}
|
||||
|
||||
|
||||
public static List<String> parametersToList(String collectionFormat, String[] values){
|
||||
List<String> params = new ArrayList<>();
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
|
||||
play.filters.headers.contentSecurityPolicy=null
|
||||
|
||||
|
||||
play.http.errorHandler="swagger.ErrorHandler"
|
||||
|
||||
## Akka
|
||||
|
@ -92,21 +92,5 @@ public class Category {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Category>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Category> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,21 +114,5 @@ public class ModelApiResponse {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ModelApiResponse>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ModelApiResponse> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,21 +215,5 @@ public class Order {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Order>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Order> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,21 +234,5 @@ public class Pet {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Pet>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Pet> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class Tag {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Tag>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Tag> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,21 +224,5 @@ public class User {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<User> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -27,11 +28,13 @@ public class PetApiController extends Controller {
|
||||
|
||||
private final PetApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private PetApiController(PetApiControllerImpInterface imp) {
|
||||
private PetApiController(Configuration configuration, PetApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -41,7 +44,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -75,8 +80,10 @@ public class PetApiController extends Controller {
|
||||
status.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByStatus(status);
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
@ -95,8 +102,10 @@ public class PetApiController extends Controller {
|
||||
tags.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByTags(tags);
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
@ -105,7 +114,9 @@ public class PetApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getPetById(Long petId) {
|
||||
Pet obj = imp.getPetById(petId);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -116,7 +127,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -155,7 +168,9 @@ public class PetApiController extends Controller {
|
||||
}
|
||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -26,11 +27,13 @@ public class StoreApiController extends Controller {
|
||||
|
||||
private final StoreApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private StoreApiController(StoreApiControllerImpInterface imp) {
|
||||
private StoreApiController(Configuration configuration, StoreApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -50,7 +53,9 @@ public class StoreApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) {
|
||||
Order obj = imp.getOrderById(orderId);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -61,12 +66,16 @@ public class StoreApiController extends Controller {
|
||||
Order body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Order.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
Order obj = imp.placeOrder(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -26,11 +27,13 @@ public class UserApiController extends Controller {
|
||||
|
||||
private final UserApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private UserApiController(UserApiControllerImpInterface imp) {
|
||||
private UserApiController(Configuration configuration, UserApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -40,7 +43,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -54,8 +59,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -70,8 +77,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -89,7 +98,9 @@ public class UserApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getUserByName(String username) {
|
||||
User obj = imp.getUserByName(username);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -127,7 +138,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
|
@ -9,6 +9,11 @@ import java.lang.annotation.Target;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
public class SwaggerUtils {
|
||||
|
||||
@With(ApiCall.class)
|
||||
@ -17,6 +22,22 @@ public class SwaggerUtils {
|
||||
public @interface ApiAction {
|
||||
}
|
||||
|
||||
public static <T> void validate(T obj) {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<T> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> parametersToList(String collectionFormat, String[] values){
|
||||
List<String> params = new ArrayList<>();
|
||||
|
||||
|
@ -17,6 +17,15 @@
|
||||
|
||||
play.filters.headers.contentSecurityPolicy=null
|
||||
|
||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
||||
# This is very useful when testing but could add a lot of overhead if you return a lot of data. Benchmark have
|
||||
# shown that the time it takes to validate is exponential.
|
||||
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||
# respecting its contract, set the "output" property below to "false". Since there is not a lot of data as input for
|
||||
# an endpoint, I highly suggest you let the "input" property set to true.
|
||||
useInputBeanValidation=true
|
||||
useOutputBeanValidation=true
|
||||
|
||||
|
||||
## Akka
|
||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||
|
@ -92,21 +92,5 @@ public class Category {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Category>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Category> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,21 +114,5 @@ public class ModelApiResponse {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ModelApiResponse>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ModelApiResponse> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,21 +215,5 @@ public class Order {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Order>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Order> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,21 +234,5 @@ public class Pet {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Pet>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Pet> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class Tag {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Tag>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Tag> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,21 +224,5 @@ public class User {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<User> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -26,11 +27,13 @@ public class PetApiController extends Controller {
|
||||
|
||||
private final PetApiControllerImp imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private PetApiController(PetApiControllerImp imp) {
|
||||
private PetApiController(Configuration configuration, PetApiControllerImp imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -40,7 +43,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -74,8 +79,10 @@ public class PetApiController extends Controller {
|
||||
status.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByStatus(status);
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
@ -94,8 +101,10 @@ public class PetApiController extends Controller {
|
||||
tags.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByTags(tags);
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
@ -104,7 +113,9 @@ public class PetApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getPetById(Long petId) throws Exception {
|
||||
Pet obj = imp.getPetById(petId);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -115,7 +126,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -154,7 +167,9 @@ public class PetApiController extends Controller {
|
||||
}
|
||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -25,11 +26,13 @@ public class StoreApiController extends Controller {
|
||||
|
||||
private final StoreApiControllerImp imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private StoreApiController(StoreApiControllerImp imp) {
|
||||
private StoreApiController(Configuration configuration, StoreApiControllerImp imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -49,7 +52,9 @@ public class StoreApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||
Order obj = imp.getOrderById(orderId);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -60,12 +65,16 @@ public class StoreApiController extends Controller {
|
||||
Order body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Order.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
Order obj = imp.placeOrder(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -25,11 +26,13 @@ public class UserApiController extends Controller {
|
||||
|
||||
private final UserApiControllerImp imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private UserApiController(UserApiControllerImp imp) {
|
||||
private UserApiController(Configuration configuration, UserApiControllerImp imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -39,7 +42,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -53,8 +58,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -69,8 +76,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -88,7 +97,9 @@ public class UserApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getUserByName(String username) throws Exception {
|
||||
User obj = imp.getUserByName(username);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -126,7 +137,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
|
@ -9,6 +9,11 @@ import java.lang.annotation.Target;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
public class SwaggerUtils {
|
||||
|
||||
@With(ApiCall.class)
|
||||
@ -17,6 +22,22 @@ public class SwaggerUtils {
|
||||
public @interface ApiAction {
|
||||
}
|
||||
|
||||
public static <T> void validate(T obj) {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<T> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> parametersToList(String collectionFormat, String[] values){
|
||||
List<String> params = new ArrayList<>();
|
||||
|
||||
|
@ -17,6 +17,15 @@
|
||||
|
||||
play.filters.headers.contentSecurityPolicy=null
|
||||
|
||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
||||
# This is very useful when testing but could add a lot of overhead if you return a lot of data. Benchmark have
|
||||
# shown that the time it takes to validate is exponential.
|
||||
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||
# respecting its contract, set the "output" property below to "false". Since there is not a lot of data as input for
|
||||
# an endpoint, I highly suggest you let the "input" property set to true.
|
||||
useInputBeanValidation=true
|
||||
useOutputBeanValidation=true
|
||||
|
||||
play.http.errorHandler="swagger.ErrorHandler"
|
||||
|
||||
## Akka
|
||||
|
@ -92,21 +92,5 @@ public class Category {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Category>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Category> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,21 +114,5 @@ public class ModelApiResponse {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<ModelApiResponse>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<ModelApiResponse> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,21 +215,5 @@ public class Order {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Order>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Order> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,21 +234,5 @@ public class Pet {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Pet>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Pet> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,21 +92,5 @@ public class Tag {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<Tag>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<Tag> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,21 +224,5 @@ public class User {
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> constraintViolations = validator.validate(this);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errors = new StringBuilder();
|
||||
for (ConstraintViolation<User> contraintes : constraintViolations) {
|
||||
errors.append(String.format("%s.%s %s\n",
|
||||
contraintes.getRootBeanClass().getSimpleName(),
|
||||
contraintes.getPropertyPath(),
|
||||
contraintes.getMessage()));
|
||||
}
|
||||
throw new RuntimeException("Bean validation : " + errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -26,11 +27,13 @@ public class PetApiController extends Controller {
|
||||
|
||||
private final PetApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private PetApiController(PetApiControllerImpInterface imp) {
|
||||
private PetApiController(Configuration configuration, PetApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -40,7 +43,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -74,8 +79,10 @@ public class PetApiController extends Controller {
|
||||
status.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByStatus(status);
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
@ -94,8 +101,10 @@ public class PetApiController extends Controller {
|
||||
tags.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByTags(tags);
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
@ -104,7 +113,9 @@ public class PetApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getPetById(Long petId) throws Exception {
|
||||
Pet obj = imp.getPetById(petId);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -115,7 +126,9 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -154,7 +167,9 @@ public class PetApiController extends Controller {
|
||||
}
|
||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -25,11 +26,13 @@ public class StoreApiController extends Controller {
|
||||
|
||||
private final StoreApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private StoreApiController(StoreApiControllerImpInterface imp) {
|
||||
private StoreApiController(Configuration configuration, StoreApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -49,7 +52,9 @@ public class StoreApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||
Order obj = imp.getOrderById(orderId);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -60,12 +65,16 @@ public class StoreApiController extends Controller {
|
||||
Order body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), Order.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
Order obj = imp.placeOrder(body);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import swagger.SwaggerUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import play.Configuration;
|
||||
|
||||
import swagger.SwaggerUtils.ApiAction;
|
||||
|
||||
@ -25,11 +26,13 @@ public class UserApiController extends Controller {
|
||||
|
||||
private final UserApiControllerImpInterface imp;
|
||||
private final ObjectMapper mapper;
|
||||
private final Configuration configuration;
|
||||
|
||||
@Inject
|
||||
private UserApiController(UserApiControllerImpInterface imp) {
|
||||
private UserApiController(Configuration configuration, UserApiControllerImpInterface imp) {
|
||||
this.imp = imp;
|
||||
mapper = new ObjectMapper();
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
@ -39,7 +42,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
@ -53,8 +58,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -69,8 +76,10 @@ public class UserApiController extends Controller {
|
||||
List<User> body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
SwaggerUtils.validate(curItem);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
@ -88,7 +97,9 @@ public class UserApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getUserByName(String username) throws Exception {
|
||||
User obj = imp.getUserByName(username);
|
||||
obj.validate();
|
||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||
SwaggerUtils.validate(obj);
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
}
|
||||
@ -126,7 +137,9 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
if (nodebody != null) {
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||
SwaggerUtils.validate(body);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("'body' parameter is required");
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user