forked from loafle/openapi-generator-original
[JavaPlayFramework] Add automatic bean validation method when activated (#6125)
* Add automatic validation of return type when beanValidation is activated * Small fixes for the automatic validation * Add validation of entering body object + fix problem when receiving list + do not validate when the return value is a file
This commit is contained in:
committed by
wing328
parent
570f0a9bee
commit
8597b3f843
@@ -2,6 +2,8 @@ package apimodels;
|
||||
|
||||
import java.util.Objects;
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import java.util.Set;
|
||||
import javax.validation.*;
|
||||
import javax.validation.constraints.*;
|
||||
/**
|
||||
* A category for a pet
|
||||
@@ -88,5 +90,21 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package apimodels;
|
||||
|
||||
import java.util.Objects;
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import java.util.Set;
|
||||
import javax.validation.*;
|
||||
import javax.validation.constraints.*;
|
||||
/**
|
||||
* Describes the result of uploading an image resource
|
||||
@@ -110,5 +112,21 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package apimodels;
|
||||
import java.util.Objects;
|
||||
import java.time.OffsetDateTime;
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import java.util.Set;
|
||||
import javax.validation.*;
|
||||
import javax.validation.constraints.*;
|
||||
/**
|
||||
* An order for a pets from the pet store
|
||||
@@ -120,7 +122,8 @@ public class Order {
|
||||
* Get shipDate
|
||||
* @return shipDate
|
||||
**/
|
||||
public OffsetDateTime getShipDate() {
|
||||
@Valid
|
||||
public OffsetDateTime getShipDate() {
|
||||
return shipDate;
|
||||
}
|
||||
|
||||
@@ -210,5 +213,21 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import apimodels.Tag;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import java.util.Set;
|
||||
import javax.validation.*;
|
||||
import javax.validation.constraints.*;
|
||||
/**
|
||||
* A pet for sale in the pet store
|
||||
@@ -89,7 +91,8 @@ public class Pet {
|
||||
* Get category
|
||||
* @return category
|
||||
**/
|
||||
public Category getCategory() {
|
||||
@Valid
|
||||
public Category getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
@@ -106,7 +109,7 @@ public class Pet {
|
||||
* Get name
|
||||
* @return name
|
||||
**/
|
||||
@NotNull
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@@ -129,7 +132,7 @@ public class Pet {
|
||||
* Get photoUrls
|
||||
* @return photoUrls
|
||||
**/
|
||||
@NotNull
|
||||
@NotNull
|
||||
public List<String> getPhotoUrls() {
|
||||
return photoUrls;
|
||||
}
|
||||
@@ -155,7 +158,8 @@ public class Pet {
|
||||
* Get tags
|
||||
* @return tags
|
||||
**/
|
||||
public List<Tag> getTags() {
|
||||
@Valid
|
||||
public List<Tag> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
@@ -228,5 +232,21 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package apimodels;
|
||||
|
||||
import java.util.Objects;
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import java.util.Set;
|
||||
import javax.validation.*;
|
||||
import javax.validation.constraints.*;
|
||||
/**
|
||||
* A tag for a pet
|
||||
@@ -88,5 +90,21 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package apimodels;
|
||||
|
||||
import java.util.Objects;
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import java.util.Set;
|
||||
import javax.validation.*;
|
||||
import javax.validation.constraints.*;
|
||||
/**
|
||||
* A User who is purchasing from the pet store
|
||||
@@ -220,5 +222,21 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import play.mvc.Controller;
|
||||
import play.mvc.Result;
|
||||
import play.mvc.Http;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -39,6 +40,7 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
|
||||
imp.addPet(body);
|
||||
|
||||
@@ -71,6 +73,9 @@ public class PetApiController extends Controller {
|
||||
status.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByStatus(status);
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
|
||||
@@ -86,6 +91,9 @@ public class PetApiController extends Controller {
|
||||
tags.add(curParam);
|
||||
}
|
||||
List<Pet> obj = imp.findPetsByTags(tags);
|
||||
for (Pet curItem : obj) {
|
||||
curItem.validate();
|
||||
}
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
|
||||
@@ -95,6 +103,7 @@ public class PetApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getPetById(Long petId) {
|
||||
Pet obj = imp.getPetById(petId);
|
||||
obj.validate();
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
|
||||
@@ -107,6 +116,7 @@ public class PetApiController extends Controller {
|
||||
Pet body;
|
||||
|
||||
body = mapper.readValue(nodebody.toString(), Pet.class);
|
||||
body.validate();
|
||||
|
||||
imp.updatePet(body);
|
||||
|
||||
@@ -150,6 +160,7 @@ public class PetApiController extends Controller {
|
||||
}
|
||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||
obj.validate();
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import play.mvc.Controller;
|
||||
import play.mvc.Result;
|
||||
import play.mvc.Http;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -52,6 +53,7 @@ public class StoreApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) {
|
||||
Order obj = imp.getOrderById(orderId);
|
||||
obj.validate();
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
|
||||
@@ -64,8 +66,10 @@ public class StoreApiController extends Controller {
|
||||
Order body;
|
||||
|
||||
body = mapper.readValue(nodebody.toString(), Order.class);
|
||||
body.validate();
|
||||
|
||||
Order obj = imp.placeOrder(body);
|
||||
obj.validate();
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import play.mvc.Controller;
|
||||
import play.mvc.Result;
|
||||
import play.mvc.Http;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -38,6 +39,7 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
|
||||
imp.createUser(body);
|
||||
|
||||
@@ -50,7 +52,10 @@ public class UserApiController extends Controller {
|
||||
JsonNode nodebody = request().body().asJson();
|
||||
List<User> body;
|
||||
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<List<User>>>(){});
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
}
|
||||
|
||||
imp.createUsersWithArrayInput(body);
|
||||
|
||||
@@ -63,7 +68,10 @@ public class UserApiController extends Controller {
|
||||
JsonNode nodebody = request().body().asJson();
|
||||
List<User> body;
|
||||
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<List<User>>>(){});
|
||||
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
|
||||
for (User curItem : body) {
|
||||
curItem.validate();
|
||||
}
|
||||
|
||||
imp.createUsersWithListInput(body);
|
||||
|
||||
@@ -82,6 +90,7 @@ public class UserApiController extends Controller {
|
||||
@ApiAction
|
||||
public Result getUserByName(String username) {
|
||||
User obj = imp.getUserByName(username);
|
||||
obj.validate();
|
||||
JsonNode result = mapper.valueToTree(obj);
|
||||
return ok(result);
|
||||
|
||||
@@ -121,6 +130,7 @@ public class UserApiController extends Controller {
|
||||
User body;
|
||||
|
||||
body = mapper.readValue(nodebody.toString(), User.class);
|
||||
body.validate();
|
||||
|
||||
imp.updateUser(username, body);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user