[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:
Jean-François Côté
2017-10-27 10:48:27 -04:00
committed by wing328
parent cab4fc0fed
commit 18ba90f5ac
124 changed files with 784 additions and 1251 deletions

View File

@@ -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);
for (Pet curItem : obj) {
curItem.validate();
if (configuration.getBoolean("useOutputBeanValidation")) {
for (Pet curItem : obj) {
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);
for (Pet curItem : obj) {
curItem.validate();
if (configuration.getBoolean("useOutputBeanValidation")) {
for (Pet curItem : obj) {
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);
}

View File

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

View File

@@ -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>>(){});
for (User curItem : body) {
curItem.validate();
if (configuration.getBoolean("useInputBeanValidation")) {
for (User curItem : body) {
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>>(){});
for (User curItem : body) {
curItem.validate();
if (configuration.getBoolean("useInputBeanValidation")) {
for (User curItem : body) {
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");
}