mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-13 05:00:50 +00:00
Fix package declaration for play-framework (#332)
This commit is contained in:
parent
aab99bc530
commit
2103fadab8
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import play.mvc.Action;
|
import play.mvc.Action;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
import play.*;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import play.mvc.With;
|
import play.mvc.With;
|
||||||
|
|
||||||
|
@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -44,7 +44,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -72,7 +72,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -83,7 +83,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByStatus(status);
|
List<Pet> obj = imp.findPetsByStatus(status);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -96,7 +96,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -107,7 +107,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByTags(tags);
|
List<Pet> obj = imp.findPetsByTags(tags);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -118,7 +118,7 @@ public class PetApiController extends Controller {
|
|||||||
public Result getPetById(Long petId) throws Exception {
|
public Result getPetById(Long petId) throws Exception {
|
||||||
Pet obj = imp.getPetById(petId);
|
Pet obj = imp.getPetById(petId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -131,7 +131,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -172,7 +172,7 @@ public class PetApiController extends Controller {
|
|||||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
@ -53,7 +53,7 @@ public class StoreApiController extends Controller {
|
|||||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||||
Order obj = imp.getOrderById(orderId);
|
Order obj = imp.getOrderById(orderId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -66,14 +66,14 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
}
|
}
|
||||||
Order obj = imp.placeOrder(order);
|
Order obj = imp.placeOrder(order);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
@ -43,7 +43,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -60,7 +60,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -78,7 +78,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -98,7 +98,7 @@ public class UserApiController extends Controller {
|
|||||||
public Result getUserByName(String username) throws Exception {
|
public Result getUserByName(String username) throws Exception {
|
||||||
User obj = imp.getUserByName(username);
|
User obj = imp.getUserByName(username);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -138,7 +138,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import play.mvc.Action;
|
import play.mvc.Action;
|
||||||
@ -8,7 +8,7 @@ import play.mvc.Result;
|
|||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ApiCall() {}
|
private ApiCall() {}
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
import play.*;
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import play.mvc.With;
|
import play.mvc.With;
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ import javax.validation.Validation;
|
|||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
import javax.validation.ValidatorFactory;
|
import javax.validation.ValidatorFactory;
|
||||||
|
|
||||||
public class SwaggerUtils {
|
public class OpenAPIUtils {
|
||||||
|
|
||||||
@With(ApiCall.class)
|
@With(ApiCall.class)
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
# When using bean validation with the OpenAPI 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
|
# 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.
|
# 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
|
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||||
@ -26,7 +26,7 @@ play.filters.headers.contentSecurityPolicy=null
|
|||||||
useInputBeanValidation=true
|
useInputBeanValidation=true
|
||||||
useOutputBeanValidation=true
|
useOutputBeanValidation=true
|
||||||
|
|
||||||
play.http.errorHandler="swagger.ErrorHandler"
|
play.http.errorHandler="openapitools.ErrorHandler"
|
||||||
|
|
||||||
## Akka
|
## Akka
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||||
|
@ -1017,7 +1017,12 @@
|
|||||||
"type" : "string"
|
"type" : "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"description" : "Describes the result of uploading an image resource"
|
"description" : "Describes the result of uploading an image resource",
|
||||||
|
"example" : {
|
||||||
|
"code" : 0,
|
||||||
|
"type" : "type",
|
||||||
|
"message" : "message"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"securitySchemes" : {
|
"securitySchemes" : {
|
@ -14,7 +14,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
@ -23,7 +23,7 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -47,7 +47,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -79,7 +79,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -90,7 +90,7 @@ public class PetApiController extends Controller {
|
|||||||
CompletionStage<List<Pet>> stage = imp.findPetsByStatus(status).thenApply(obj -> {
|
CompletionStage<List<Pet>> stage = imp.findPetsByStatus(status).thenApply(obj -> {
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
@ -107,7 +107,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -118,7 +118,7 @@ public class PetApiController extends Controller {
|
|||||||
CompletionStage<List<Pet>> stage = imp.findPetsByTags(tags).thenApply(obj -> {
|
CompletionStage<List<Pet>> stage = imp.findPetsByTags(tags).thenApply(obj -> {
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
@ -133,7 +133,7 @@ public class PetApiController extends Controller {
|
|||||||
public CompletionStage<Result> getPetById(Long petId) throws Exception {
|
public CompletionStage<Result> getPetById(Long petId) throws Exception {
|
||||||
CompletionStage<Pet> stage = imp.getPetById(petId).thenApply(obj -> {
|
CompletionStage<Pet> stage = imp.getPetById(petId).thenApply(obj -> {
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
@ -150,7 +150,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -195,7 +195,7 @@ public class PetApiController extends Controller {
|
|||||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||||
CompletionStage<ModelApiResponse> stage = imp.uploadFile(petId, additionalMetadata, file).thenApply(obj -> {
|
CompletionStage<ModelApiResponse> stage = imp.uploadFile(petId, additionalMetadata, file).thenApply(obj -> {
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
|
@ -13,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
@ -22,7 +22,7 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
@ -62,7 +62,7 @@ public class StoreApiController extends Controller {
|
|||||||
public CompletionStage<Result> getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
public CompletionStage<Result> getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||||
CompletionStage<Order> stage = imp.getOrderById(orderId).thenApply(obj -> {
|
CompletionStage<Order> stage = imp.getOrderById(orderId).thenApply(obj -> {
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
@ -79,14 +79,14 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
}
|
}
|
||||||
CompletionStage<Order> stage = imp.placeOrder(order).thenApply(obj -> {
|
CompletionStage<Order> stage = imp.placeOrder(order).thenApply(obj -> {
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
|
@ -13,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
@ -22,7 +22,7 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
@ -46,7 +46,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -65,7 +65,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -85,7 +85,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -109,7 +109,7 @@ public class UserApiController extends Controller {
|
|||||||
public CompletionStage<Result> getUserByName(String username) throws Exception {
|
public CompletionStage<Result> getUserByName(String username) throws Exception {
|
||||||
CompletionStage<User> stage = imp.getUserByName(username).thenApply(obj -> {
|
CompletionStage<User> stage = imp.getUserByName(username).thenApply(obj -> {
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
@ -159,7 +159,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import play.mvc.Action;
|
import play.mvc.Action;
|
||||||
@ -8,7 +8,7 @@ import play.mvc.Result;
|
|||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ApiCall() {}
|
private ApiCall() {}
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
import play.*;
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import play.mvc.With;
|
import play.mvc.With;
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ import javax.validation.Validation;
|
|||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
import javax.validation.ValidatorFactory;
|
import javax.validation.ValidatorFactory;
|
||||||
|
|
||||||
public class SwaggerUtils {
|
public class OpenAPIUtils {
|
||||||
|
|
||||||
@With(ApiCall.class)
|
@With(ApiCall.class)
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
# When using bean validation with the OpenAPI 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
|
# 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.
|
# 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
|
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||||
@ -26,7 +26,7 @@ play.filters.headers.contentSecurityPolicy=null
|
|||||||
useInputBeanValidation=true
|
useInputBeanValidation=true
|
||||||
useOutputBeanValidation=true
|
useOutputBeanValidation=true
|
||||||
|
|
||||||
play.http.errorHandler="swagger.ErrorHandler"
|
play.http.errorHandler="openapitools.ErrorHandler"
|
||||||
|
|
||||||
## Akka
|
## Akka
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||||
|
@ -1017,7 +1017,12 @@
|
|||||||
"type" : "string"
|
"type" : "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"description" : "Describes the result of uploading an image resource"
|
"description" : "Describes the result of uploading an image resource",
|
||||||
|
"example" : {
|
||||||
|
"code" : 0,
|
||||||
|
"type" : "type",
|
||||||
|
"message" : "message"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"securitySchemes" : {
|
"securitySchemes" : {
|
@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -42,7 +42,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -68,7 +68,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -85,7 +85,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -108,7 +108,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
@ -56,7 +56,7 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
@ -41,7 +41,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -57,7 +57,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -74,7 +74,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -124,7 +124,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import play.mvc.Action;
|
import play.mvc.Action;
|
||||||
@ -8,7 +8,7 @@ import play.mvc.Result;
|
|||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ApiCall() {}
|
private ApiCall() {}
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
import play.*;
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import play.mvc.With;
|
import play.mvc.With;
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ import javax.validation.Validation;
|
|||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
import javax.validation.ValidatorFactory;
|
import javax.validation.ValidatorFactory;
|
||||||
|
|
||||||
public class SwaggerUtils {
|
public class OpenAPIUtils {
|
||||||
|
|
||||||
@With(ApiCall.class)
|
@With(ApiCall.class)
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
# When using bean validation with the OpenAPI 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
|
# 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.
|
# 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
|
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||||
@ -26,7 +26,7 @@ play.filters.headers.contentSecurityPolicy=null
|
|||||||
useInputBeanValidation=true
|
useInputBeanValidation=true
|
||||||
useOutputBeanValidation=true
|
useOutputBeanValidation=true
|
||||||
|
|
||||||
play.http.errorHandler="swagger.ErrorHandler"
|
play.http.errorHandler="openapitools.ErrorHandler"
|
||||||
|
|
||||||
## Akka
|
## Akka
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||||
|
@ -1017,7 +1017,12 @@
|
|||||||
"type" : "string"
|
"type" : "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"description" : "Describes the result of uploading an image resource"
|
"description" : "Describes the result of uploading an image resource",
|
||||||
|
"example" : {
|
||||||
|
"code" : 0,
|
||||||
|
"type" : "type",
|
||||||
|
"message" : "message"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"securitySchemes" : {
|
"securitySchemes" : {
|
@ -12,13 +12,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class AnotherFakeApiController extends Controller {
|
public class AnotherFakeApiController extends Controller {
|
||||||
@ -42,14 +42,14 @@ public class AnotherFakeApiController extends Controller {
|
|||||||
if (nodeclient != null) {
|
if (nodeclient != null) {
|
||||||
client = mapper.readValue(nodeclient.toString(), Client.class);
|
client = mapper.readValue(nodeclient.toString(), Client.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(client);
|
OpenAPIUtils.validate(client);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Client' parameter is required");
|
throw new IllegalArgumentException("'Client' parameter is required");
|
||||||
}
|
}
|
||||||
Client obj = imp.testSpecialTags(client);
|
Client obj = imp.testSpecialTags(client);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -19,13 +19,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class FakeApiController extends Controller {
|
public class FakeApiController extends Controller {
|
||||||
@ -49,7 +49,7 @@ public class FakeApiController extends Controller {
|
|||||||
if (nodebody != null) {
|
if (nodebody != null) {
|
||||||
body = mapper.readValue(nodebody.toString(), Boolean.class);
|
body = mapper.readValue(nodebody.toString(), Boolean.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(body);
|
OpenAPIUtils.validate(body);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body = null;
|
body = null;
|
||||||
@ -66,14 +66,14 @@ public class FakeApiController extends Controller {
|
|||||||
if (nodeouterComposite != null) {
|
if (nodeouterComposite != null) {
|
||||||
outerComposite = mapper.readValue(nodeouterComposite.toString(), OuterComposite.class);
|
outerComposite = mapper.readValue(nodeouterComposite.toString(), OuterComposite.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(outerComposite);
|
OpenAPIUtils.validate(outerComposite);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
outerComposite = null;
|
outerComposite = null;
|
||||||
}
|
}
|
||||||
OuterComposite obj = imp.fakeOuterCompositeSerialize(outerComposite);
|
OuterComposite obj = imp.fakeOuterCompositeSerialize(outerComposite);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -86,14 +86,14 @@ public class FakeApiController extends Controller {
|
|||||||
if (nodebody != null) {
|
if (nodebody != null) {
|
||||||
body = mapper.readValue(nodebody.toString(), BigDecimal.class);
|
body = mapper.readValue(nodebody.toString(), BigDecimal.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(body);
|
OpenAPIUtils.validate(body);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body = null;
|
body = null;
|
||||||
}
|
}
|
||||||
BigDecimal obj = imp.fakeOuterNumberSerialize(body);
|
BigDecimal obj = imp.fakeOuterNumberSerialize(body);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -106,7 +106,7 @@ public class FakeApiController extends Controller {
|
|||||||
if (nodebody != null) {
|
if (nodebody != null) {
|
||||||
body = mapper.readValue(nodebody.toString(), String.class);
|
body = mapper.readValue(nodebody.toString(), String.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(body);
|
OpenAPIUtils.validate(body);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body = null;
|
body = null;
|
||||||
@ -123,7 +123,7 @@ public class FakeApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -146,14 +146,14 @@ public class FakeApiController extends Controller {
|
|||||||
if (nodeclient != null) {
|
if (nodeclient != null) {
|
||||||
client = mapper.readValue(nodeclient.toString(), Client.class);
|
client = mapper.readValue(nodeclient.toString(), Client.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(client);
|
OpenAPIUtils.validate(client);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Client' parameter is required");
|
throw new IllegalArgumentException("'Client' parameter is required");
|
||||||
}
|
}
|
||||||
Client obj = imp.testClientModel(client);
|
Client obj = imp.testClientModel(client);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -260,7 +260,7 @@ public class FakeApiController extends Controller {
|
|||||||
@ApiAction
|
@ApiAction
|
||||||
public Result testEnumParameters() throws Exception {
|
public Result testEnumParameters() throws Exception {
|
||||||
String[] enumQueryStringArrayArray = request().queryString().get("enum_query_string_array");
|
String[] enumQueryStringArrayArray = request().queryString().get("enum_query_string_array");
|
||||||
List<String> enumQueryStringArrayList = SwaggerUtils.parametersToList("csv", enumQueryStringArrayArray);
|
List<String> enumQueryStringArrayList = OpenAPIUtils.parametersToList("csv", enumQueryStringArrayArray);
|
||||||
List<String> enumQueryStringArray = new ArrayList<String>();
|
List<String> enumQueryStringArray = new ArrayList<String>();
|
||||||
for (String curParam : enumQueryStringArrayList) {
|
for (String curParam : enumQueryStringArrayList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -290,11 +290,11 @@ public class FakeApiController extends Controller {
|
|||||||
enumQueryDouble = null;
|
enumQueryDouble = null;
|
||||||
}
|
}
|
||||||
String valueenumFormStringArray = (request().body().asMultipartFormData().asFormUrlEncoded().get("enum_form_string_array"))[0];
|
String valueenumFormStringArray = (request().body().asMultipartFormData().asFormUrlEncoded().get("enum_form_string_array"))[0];
|
||||||
List<String> enumFormStringArray;
|
String enumFormStringArray;
|
||||||
if (valueenumFormStringArray != null) {
|
if (valueenumFormStringArray != null) {
|
||||||
enumFormStringArray = valueenumFormStringArray;
|
enumFormStringArray = valueenumFormStringArray;
|
||||||
} else {
|
} else {
|
||||||
enumFormStringArray = new ArrayList<>();
|
enumFormStringArray = "$";
|
||||||
}
|
}
|
||||||
String valueenumFormString = (request().body().asMultipartFormData().asFormUrlEncoded().get("enum_form_string"))[0];
|
String valueenumFormString = (request().body().asMultipartFormData().asFormUrlEncoded().get("enum_form_string"))[0];
|
||||||
String enumFormString;
|
String enumFormString;
|
||||||
@ -304,7 +304,7 @@ public class FakeApiController extends Controller {
|
|||||||
enumFormString = "-efg";
|
enumFormString = "-efg";
|
||||||
}
|
}
|
||||||
String[] enumHeaderStringArrayArray = request().headers().get("enum_header_string_array");
|
String[] enumHeaderStringArrayArray = request().headers().get("enum_header_string_array");
|
||||||
List<String> enumHeaderStringArrayList = SwaggerUtils.parametersToList("csv", enumHeaderStringArrayArray);
|
List<String> enumHeaderStringArrayList = OpenAPIUtils.parametersToList("csv", enumHeaderStringArrayArray);
|
||||||
List<String> enumHeaderStringArray = new ArrayList<String>();
|
List<String> enumHeaderStringArray = new ArrayList<String>();
|
||||||
for (String curParam : enumHeaderStringArrayList) {
|
for (String curParam : enumHeaderStringArrayList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -331,7 +331,7 @@ public class FakeApiController extends Controller {
|
|||||||
requestBody = mapper.readValue(noderequestBody.toString(), new TypeReference<String>(){});
|
requestBody = mapper.readValue(noderequestBody.toString(), new TypeReference<String>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (Map.Entry<String, String> entry : requestBody.entrySet()) {
|
for (Map.Entry<String, String> entry : requestBody.entrySet()) {
|
||||||
SwaggerUtils.validate(entry.getValue());
|
OpenAPIUtils.validate(entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -58,7 +58,7 @@ public class FakeApiControllerImp implements FakeApiControllerImpInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void testEnumParameters(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List<String> enumFormStringArray, String enumFormString) throws Exception {
|
public void testEnumParameters(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, String enumFormStringArray, String enumFormString) throws Exception {
|
||||||
//Do your magic!!!
|
//Do your magic!!!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ public interface FakeApiControllerImpInterface {
|
|||||||
|
|
||||||
void testEndpointParameters(BigDecimal number, Double _double, String patternWithoutDelimiter, byte[] _byte, Integer integer, Integer int32, Long int64, Float _float, String string, Http.MultipartFormData.FilePart binary, LocalDate date, OffsetDateTime dateTime, String password, String paramCallback) throws Exception;
|
void testEndpointParameters(BigDecimal number, Double _double, String patternWithoutDelimiter, byte[] _byte, Integer integer, Integer int32, Long int64, Float _float, String string, Http.MultipartFormData.FilePart binary, LocalDate date, OffsetDateTime dateTime, String password, String paramCallback) throws Exception;
|
||||||
|
|
||||||
void testEnumParameters(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List<String> enumFormStringArray, String enumFormString) throws Exception;
|
void testEnumParameters(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, String enumFormStringArray, String enumFormString) throws Exception;
|
||||||
|
|
||||||
void testInlineAdditionalProperties(String requestBody) throws Exception;
|
void testInlineAdditionalProperties(String requestBody) throws Exception;
|
||||||
|
|
||||||
|
@ -12,13 +12,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class FakeClassnameTags123ApiController extends Controller {
|
public class FakeClassnameTags123ApiController extends Controller {
|
||||||
@ -42,14 +42,14 @@ public class FakeClassnameTags123ApiController extends Controller {
|
|||||||
if (nodeclient != null) {
|
if (nodeclient != null) {
|
||||||
client = mapper.readValue(nodeclient.toString(), Client.class);
|
client = mapper.readValue(nodeclient.toString(), Client.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(client);
|
OpenAPIUtils.validate(client);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Client' parameter is required");
|
throw new IllegalArgumentException("'Client' parameter is required");
|
||||||
}
|
}
|
||||||
Client obj = imp.testClassname(client);
|
Client obj = imp.testClassname(client);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -44,7 +44,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -72,7 +72,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -83,7 +83,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByStatus(status);
|
List<Pet> obj = imp.findPetsByStatus(status);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -96,7 +96,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -107,7 +107,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByTags(tags);
|
List<Pet> obj = imp.findPetsByTags(tags);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -118,7 +118,7 @@ public class PetApiController extends Controller {
|
|||||||
public Result getPetById(Long petId) throws Exception {
|
public Result getPetById(Long petId) throws Exception {
|
||||||
Pet obj = imp.getPetById(petId);
|
Pet obj = imp.getPetById(petId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -131,7 +131,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -172,7 +172,7 @@ public class PetApiController extends Controller {
|
|||||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
@ -53,7 +53,7 @@ public class StoreApiController extends Controller {
|
|||||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||||
Order obj = imp.getOrderById(orderId);
|
Order obj = imp.getOrderById(orderId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -66,14 +66,14 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
}
|
}
|
||||||
Order obj = imp.placeOrder(order);
|
Order obj = imp.placeOrder(order);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
@ -43,7 +43,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -60,7 +60,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -78,7 +78,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -98,7 +98,7 @@ public class UserApiController extends Controller {
|
|||||||
public Result getUserByName(String username) throws Exception {
|
public Result getUserByName(String username) throws Exception {
|
||||||
User obj = imp.getUserByName(username);
|
User obj = imp.getUserByName(username);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -138,7 +138,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import play.mvc.Action;
|
import play.mvc.Action;
|
||||||
@ -8,7 +8,7 @@ import play.mvc.Result;
|
|||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ApiCall() {}
|
private ApiCall() {}
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
import play.*;
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import play.mvc.With;
|
import play.mvc.With;
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ import javax.validation.Validation;
|
|||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
import javax.validation.ValidatorFactory;
|
import javax.validation.ValidatorFactory;
|
||||||
|
|
||||||
public class SwaggerUtils {
|
public class OpenAPIUtils {
|
||||||
|
|
||||||
@With(ApiCall.class)
|
@With(ApiCall.class)
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
# When using bean validation with the OpenAPI 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
|
# 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.
|
# 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
|
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||||
@ -26,7 +26,7 @@ play.filters.headers.contentSecurityPolicy=null
|
|||||||
useInputBeanValidation=true
|
useInputBeanValidation=true
|
||||||
useOutputBeanValidation=true
|
useOutputBeanValidation=true
|
||||||
|
|
||||||
play.http.errorHandler="swagger.ErrorHandler"
|
play.http.errorHandler="openapitools.ErrorHandler"
|
||||||
|
|
||||||
## Akka
|
## Akka
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||||
|
@ -1483,6 +1483,11 @@
|
|||||||
"message" : {
|
"message" : {
|
||||||
"type" : "string"
|
"type" : "string"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"example" : {
|
||||||
|
"code" : 0,
|
||||||
|
"type" : "type",
|
||||||
|
"message" : "message"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Name" : {
|
"Name" : {
|
@ -14,11 +14,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -65,7 +65,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -84,7 +84,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
|
@ -13,11 +13,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
|
@ -13,11 +13,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import play.mvc.Action;
|
||||||
|
import play.mvc.Http;
|
||||||
|
import play.mvc.Result;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ApiCall() {}
|
||||||
|
|
||||||
|
public CompletionStage<Result> call(Http.Context ctx) {
|
||||||
|
try {
|
||||||
|
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
||||||
|
return delegate.call(ctx);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
//TODO: log the error in your metric
|
||||||
|
|
||||||
|
//We rethrow this error so it will be caught in the ErrorHandler
|
||||||
|
throw t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
|
import play.*;
|
||||||
|
import play.api.OptionalSourceMapper;
|
||||||
|
import play.api.UsefulException;
|
||||||
|
import play.api.routing.Router;
|
||||||
|
import play.http.DefaultHttpErrorHandler;
|
||||||
|
import play.mvc.Http.*;
|
||||||
|
import play.mvc.*;
|
||||||
|
|
||||||
|
import javax.inject.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
import static play.mvc.Results.*;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
public class ErrorHandler extends DefaultHttpErrorHandler {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
||||||
|
super(configuration, environment, sourceMapper, routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
||||||
|
//Since the error is already handled, we don't want to print anything on the console
|
||||||
|
//But if you want to have the error printed in the console, just delete this override
|
||||||
|
}
|
||||||
|
|
||||||
|
private Result handleExceptions(Throwable t) {
|
||||||
|
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
||||||
|
return ok();
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import play.mvc.With;
|
import play.mvc.With;
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ import java.text.SimpleDateFormat;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
public class SwaggerUtils {
|
public class OpenAPIUtils {
|
||||||
|
|
||||||
@With(ApiCall.class)
|
@With(ApiCall.class)
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
@ -1,27 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
|
||||||
import play.mvc.Action;
|
|
||||||
import play.mvc.Http;
|
|
||||||
import play.mvc.Result;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
private ApiCall() {}
|
|
||||||
|
|
||||||
public CompletionStage<Result> call(Http.Context ctx) {
|
|
||||||
try {
|
|
||||||
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
|
||||||
return delegate.call(ctx);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
//TODO: log the error in your metric
|
|
||||||
|
|
||||||
//We rethrow this error so it will be caught in the ErrorHandler
|
|
||||||
throw t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
|
||||||
import play.api.OptionalSourceMapper;
|
|
||||||
import play.api.UsefulException;
|
|
||||||
import play.api.routing.Router;
|
|
||||||
import play.http.DefaultHttpErrorHandler;
|
|
||||||
import play.mvc.Http.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
import static play.mvc.Results.*;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class ErrorHandler extends DefaultHttpErrorHandler {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
|
||||||
super(configuration, environment, sourceMapper, routes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
|
||||||
//Since the error is already handled, we don't want to print anything on the console
|
|
||||||
//But if you want to have the error printed in the console, just delete this override
|
|
||||||
}
|
|
||||||
|
|
||||||
private Result handleExceptions(Throwable t) {
|
|
||||||
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
|
||||||
return ok();
|
|
||||||
}
|
|
||||||
}
|
|
@ -18,7 +18,7 @@
|
|||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
|
|
||||||
play.http.errorHandler="swagger.ErrorHandler"
|
play.http.errorHandler="openapitools.ErrorHandler"
|
||||||
|
|
||||||
## Akka
|
## Akka
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||||
|
@ -1017,7 +1017,12 @@
|
|||||||
"type" : "string"
|
"type" : "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"description" : "Describes the result of uploading an image resource"
|
"description" : "Describes the result of uploading an image resource",
|
||||||
|
"example" : {
|
||||||
|
"code" : 0,
|
||||||
|
"type" : "type",
|
||||||
|
"message" : "message"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"securitySchemes" : {
|
"securitySchemes" : {
|
@ -15,13 +15,13 @@ import com.fasterxml.jackson.databind.JsonNode;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -45,7 +45,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -73,7 +73,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -84,7 +84,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByStatus(status);
|
List<Pet> obj = imp.findPetsByStatus(status);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -97,7 +97,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -108,7 +108,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByTags(tags);
|
List<Pet> obj = imp.findPetsByTags(tags);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -119,7 +119,7 @@ public class PetApiController extends Controller {
|
|||||||
public Result getPetById(Long petId) {
|
public Result getPetById(Long petId) {
|
||||||
Pet obj = imp.getPetById(petId);
|
Pet obj = imp.getPetById(petId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -132,7 +132,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -173,7 +173,7 @@ public class PetApiController extends Controller {
|
|||||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.JsonNode;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
@ -54,7 +54,7 @@ public class StoreApiController extends Controller {
|
|||||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) {
|
public Result getOrderById( @Min(1) @Max(5)Long orderId) {
|
||||||
Order obj = imp.getOrderById(orderId);
|
Order obj = imp.getOrderById(orderId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -67,14 +67,14 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
}
|
}
|
||||||
Order obj = imp.placeOrder(order);
|
Order obj = imp.placeOrder(order);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.JsonNode;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
@ -44,7 +44,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -61,7 +61,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -79,7 +79,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -99,7 +99,7 @@ public class UserApiController extends Controller {
|
|||||||
public Result getUserByName(String username) {
|
public Result getUserByName(String username) {
|
||||||
User obj = imp.getUserByName(username);
|
User obj = imp.getUserByName(username);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -139,7 +139,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import play.mvc.Action;
|
||||||
|
import play.mvc.Http;
|
||||||
|
import play.mvc.Result;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ApiCall() {}
|
||||||
|
|
||||||
|
public CompletionStage<Result> call(Http.Context ctx) {
|
||||||
|
try {
|
||||||
|
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
||||||
|
return delegate.call(ctx);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
//TODO: log the error in your metric
|
||||||
|
|
||||||
|
//We rethrow this error so it will be caught in the ErrorHandler
|
||||||
|
throw t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import play.mvc.With;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
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 OpenAPIUtils {
|
||||||
|
|
||||||
|
@With(ApiCall.class)
|
||||||
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
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<>();
|
||||||
|
|
||||||
|
if (values == null) {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
||||||
|
params.addAll(Arrays.asList(values));
|
||||||
|
} else {
|
||||||
|
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
||||||
|
|
||||||
|
String delimiter = ",";
|
||||||
|
|
||||||
|
switch(collectionFormat) {
|
||||||
|
case "csv": {
|
||||||
|
delimiter = ",";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "ssv": {
|
||||||
|
delimiter = " ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "tsv": {
|
||||||
|
delimiter = "\t";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "pipes": {
|
||||||
|
delimiter = "|";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
params = Arrays.asList(values[0].split(delimiter));
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parameterToString(Object param) {
|
||||||
|
if (param == null) {
|
||||||
|
return "";
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return formatDatetime((Date) param);
|
||||||
|
} else if (param instanceof Collection) {
|
||||||
|
StringBuilder b = new StringBuilder();
|
||||||
|
for (Object o : (Collection)param) {
|
||||||
|
if (b.length() > 0) {
|
||||||
|
b.append(",");
|
||||||
|
}
|
||||||
|
b.append(String.valueOf(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.toString();
|
||||||
|
} else {
|
||||||
|
return String.valueOf(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatDatetime(Date date) {
|
||||||
|
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
|
||||||
import play.mvc.Action;
|
|
||||||
import play.mvc.Http;
|
|
||||||
import play.mvc.Result;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
private ApiCall() {}
|
|
||||||
|
|
||||||
public CompletionStage<Result> call(Http.Context ctx) {
|
|
||||||
try {
|
|
||||||
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
|
||||||
return delegate.call(ctx);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
//TODO: log the error in your metric
|
|
||||||
|
|
||||||
//We rethrow this error so it will be caught in the ErrorHandler
|
|
||||||
throw t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,103 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import play.mvc.With;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
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)
|
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
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<>();
|
|
||||||
|
|
||||||
if (values == null) {
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
|
||||||
params.addAll(Arrays.asList(values));
|
|
||||||
} else {
|
|
||||||
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
|
||||||
|
|
||||||
String delimiter = ",";
|
|
||||||
|
|
||||||
switch(collectionFormat) {
|
|
||||||
case "csv": {
|
|
||||||
delimiter = ",";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "ssv": {
|
|
||||||
delimiter = " ";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "tsv": {
|
|
||||||
delimiter = "\t";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "pipes": {
|
|
||||||
delimiter = "|";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
params = Arrays.asList(values[0].split(delimiter));
|
|
||||||
}
|
|
||||||
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parameterToString(Object param) {
|
|
||||||
if (param == null) {
|
|
||||||
return "";
|
|
||||||
} else if (param instanceof Date) {
|
|
||||||
return formatDatetime((Date) param);
|
|
||||||
} else if (param instanceof Collection) {
|
|
||||||
StringBuilder b = new StringBuilder();
|
|
||||||
for (Object o : (Collection)param) {
|
|
||||||
if (b.length() > 0) {
|
|
||||||
b.append(",");
|
|
||||||
}
|
|
||||||
b.append(String.valueOf(o));
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.toString();
|
|
||||||
} else {
|
|
||||||
return String.valueOf(param);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String formatDatetime(Date date) {
|
|
||||||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
# When using bean validation with the OpenAPI 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
|
# 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.
|
# 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
|
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -44,7 +44,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -72,7 +72,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -83,7 +83,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByStatus(status);
|
List<Pet> obj = imp.findPetsByStatus(status);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -96,7 +96,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -107,7 +107,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByTags(tags);
|
List<Pet> obj = imp.findPetsByTags(tags);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -118,7 +118,7 @@ public class PetApiController extends Controller {
|
|||||||
public Result getPetById(Long petId) throws Exception {
|
public Result getPetById(Long petId) throws Exception {
|
||||||
Pet obj = imp.getPetById(petId);
|
Pet obj = imp.getPetById(petId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -131,7 +131,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -172,7 +172,7 @@ public class PetApiController extends Controller {
|
|||||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
@ -53,7 +53,7 @@ public class StoreApiController extends Controller {
|
|||||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||||
Order obj = imp.getOrderById(orderId);
|
Order obj = imp.getOrderById(orderId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -66,14 +66,14 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
}
|
}
|
||||||
Order obj = imp.placeOrder(order);
|
Order obj = imp.placeOrder(order);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
@ -43,7 +43,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -60,7 +60,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -78,7 +78,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -98,7 +98,7 @@ public class UserApiController extends Controller {
|
|||||||
public Result getUserByName(String username) throws Exception {
|
public Result getUserByName(String username) throws Exception {
|
||||||
User obj = imp.getUserByName(username);
|
User obj = imp.getUserByName(username);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -138,7 +138,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import play.mvc.Action;
|
||||||
|
import play.mvc.Http;
|
||||||
|
import play.mvc.Result;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ApiCall() {}
|
||||||
|
|
||||||
|
public CompletionStage<Result> call(Http.Context ctx) {
|
||||||
|
try {
|
||||||
|
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
||||||
|
return delegate.call(ctx);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
//TODO: log the error in your metric
|
||||||
|
|
||||||
|
//We rethrow this error so it will be caught in the ErrorHandler
|
||||||
|
throw t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
|
import play.*;
|
||||||
|
import play.api.OptionalSourceMapper;
|
||||||
|
import play.api.UsefulException;
|
||||||
|
import play.api.routing.Router;
|
||||||
|
import play.http.DefaultHttpErrorHandler;
|
||||||
|
import play.mvc.Http.*;
|
||||||
|
import play.mvc.*;
|
||||||
|
|
||||||
|
import javax.inject.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
import static play.mvc.Results.*;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
public class ErrorHandler extends DefaultHttpErrorHandler {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
||||||
|
super(configuration, environment, sourceMapper, routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
||||||
|
//Since the error is already handled, we don't want to print anything on the console
|
||||||
|
//But if you want to have the error printed in the console, just delete this override
|
||||||
|
}
|
||||||
|
|
||||||
|
private Result handleExceptions(Throwable t) {
|
||||||
|
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
||||||
|
return ok();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import play.mvc.With;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
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 OpenAPIUtils {
|
||||||
|
|
||||||
|
@With(ApiCall.class)
|
||||||
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
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<>();
|
||||||
|
|
||||||
|
if (values == null) {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
||||||
|
params.addAll(Arrays.asList(values));
|
||||||
|
} else {
|
||||||
|
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
||||||
|
|
||||||
|
String delimiter = ",";
|
||||||
|
|
||||||
|
switch(collectionFormat) {
|
||||||
|
case "csv": {
|
||||||
|
delimiter = ",";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "ssv": {
|
||||||
|
delimiter = " ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "tsv": {
|
||||||
|
delimiter = "\t";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "pipes": {
|
||||||
|
delimiter = "|";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
params = Arrays.asList(values[0].split(delimiter));
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parameterToString(Object param) {
|
||||||
|
if (param == null) {
|
||||||
|
return "";
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return formatDatetime((Date) param);
|
||||||
|
} else if (param instanceof Collection) {
|
||||||
|
StringBuilder b = new StringBuilder();
|
||||||
|
for (Object o : (Collection)param) {
|
||||||
|
if (b.length() > 0) {
|
||||||
|
b.append(",");
|
||||||
|
}
|
||||||
|
b.append(String.valueOf(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.toString();
|
||||||
|
} else {
|
||||||
|
return String.valueOf(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatDatetime(Date date) {
|
||||||
|
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
|
||||||
import play.mvc.Action;
|
|
||||||
import play.mvc.Http;
|
|
||||||
import play.mvc.Result;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
private ApiCall() {}
|
|
||||||
|
|
||||||
public CompletionStage<Result> call(Http.Context ctx) {
|
|
||||||
try {
|
|
||||||
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
|
||||||
return delegate.call(ctx);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
//TODO: log the error in your metric
|
|
||||||
|
|
||||||
//We rethrow this error so it will be caught in the ErrorHandler
|
|
||||||
throw t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
|
||||||
import play.api.OptionalSourceMapper;
|
|
||||||
import play.api.UsefulException;
|
|
||||||
import play.api.routing.Router;
|
|
||||||
import play.http.DefaultHttpErrorHandler;
|
|
||||||
import play.mvc.Http.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
import static play.mvc.Results.*;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class ErrorHandler extends DefaultHttpErrorHandler {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
|
||||||
super(configuration, environment, sourceMapper, routes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
|
||||||
//Since the error is already handled, we don't want to print anything on the console
|
|
||||||
//But if you want to have the error printed in the console, just delete this override
|
|
||||||
}
|
|
||||||
|
|
||||||
private Result handleExceptions(Throwable t) {
|
|
||||||
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
|
||||||
return ok();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,103 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import play.mvc.With;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
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)
|
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
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<>();
|
|
||||||
|
|
||||||
if (values == null) {
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
|
||||||
params.addAll(Arrays.asList(values));
|
|
||||||
} else {
|
|
||||||
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
|
||||||
|
|
||||||
String delimiter = ",";
|
|
||||||
|
|
||||||
switch(collectionFormat) {
|
|
||||||
case "csv": {
|
|
||||||
delimiter = ",";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "ssv": {
|
|
||||||
delimiter = " ";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "tsv": {
|
|
||||||
delimiter = "\t";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "pipes": {
|
|
||||||
delimiter = "|";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
params = Arrays.asList(values[0].split(delimiter));
|
|
||||||
}
|
|
||||||
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parameterToString(Object param) {
|
|
||||||
if (param == null) {
|
|
||||||
return "";
|
|
||||||
} else if (param instanceof Date) {
|
|
||||||
return formatDatetime((Date) param);
|
|
||||||
} else if (param instanceof Collection) {
|
|
||||||
StringBuilder b = new StringBuilder();
|
|
||||||
for (Object o : (Collection)param) {
|
|
||||||
if (b.length() > 0) {
|
|
||||||
b.append(",");
|
|
||||||
}
|
|
||||||
b.append(String.valueOf(o));
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.toString();
|
|
||||||
} else {
|
|
||||||
return String.valueOf(param);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String formatDatetime(Date date) {
|
|
||||||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
# When using bean validation with the OpenAPI 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
|
# 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.
|
# 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
|
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||||
@ -26,7 +26,7 @@ play.filters.headers.contentSecurityPolicy=null
|
|||||||
useInputBeanValidation=true
|
useInputBeanValidation=true
|
||||||
useOutputBeanValidation=true
|
useOutputBeanValidation=true
|
||||||
|
|
||||||
play.http.errorHandler="swagger.ErrorHandler"
|
play.http.errorHandler="openapitools.ErrorHandler"
|
||||||
|
|
||||||
## Akka
|
## Akka
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -44,7 +44,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -72,7 +72,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -83,7 +83,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByStatus(status);
|
List<Pet> obj = imp.findPetsByStatus(status);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -96,7 +96,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -107,7 +107,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByTags(tags);
|
List<Pet> obj = imp.findPetsByTags(tags);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -118,7 +118,7 @@ public class PetApiController extends Controller {
|
|||||||
public Result getPetById(Long petId) throws Exception {
|
public Result getPetById(Long petId) throws Exception {
|
||||||
Pet obj = imp.getPetById(petId);
|
Pet obj = imp.getPetById(petId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -131,7 +131,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -172,7 +172,7 @@ public class PetApiController extends Controller {
|
|||||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
@ -53,7 +53,7 @@ public class StoreApiController extends Controller {
|
|||||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||||
Order obj = imp.getOrderById(orderId);
|
Order obj = imp.getOrderById(orderId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -66,14 +66,14 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
}
|
}
|
||||||
Order obj = imp.placeOrder(order);
|
Order obj = imp.placeOrder(order);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
@ -43,7 +43,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -60,7 +60,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -78,7 +78,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -98,7 +98,7 @@ public class UserApiController extends Controller {
|
|||||||
public Result getUserByName(String username) throws Exception {
|
public Result getUserByName(String username) throws Exception {
|
||||||
User obj = imp.getUserByName(username);
|
User obj = imp.getUserByName(username);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -138,7 +138,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import play.mvc.Action;
|
||||||
|
import play.mvc.Http;
|
||||||
|
import play.mvc.Result;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ApiCall() {}
|
||||||
|
|
||||||
|
public CompletionStage<Result> call(Http.Context ctx) {
|
||||||
|
try {
|
||||||
|
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
||||||
|
return delegate.call(ctx);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
//TODO: log the error in your metric
|
||||||
|
|
||||||
|
//We rethrow this error so it will be caught in the ErrorHandler
|
||||||
|
throw t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
|
import play.*;
|
||||||
|
import play.api.OptionalSourceMapper;
|
||||||
|
import play.api.UsefulException;
|
||||||
|
import play.api.routing.Router;
|
||||||
|
import play.http.DefaultHttpErrorHandler;
|
||||||
|
import play.mvc.Http.*;
|
||||||
|
import play.mvc.*;
|
||||||
|
|
||||||
|
import javax.inject.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
import static play.mvc.Results.*;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
public class ErrorHandler extends DefaultHttpErrorHandler {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
||||||
|
super(configuration, environment, sourceMapper, routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
||||||
|
//Since the error is already handled, we don't want to print anything on the console
|
||||||
|
//But if you want to have the error printed in the console, just delete this override
|
||||||
|
}
|
||||||
|
|
||||||
|
private Result handleExceptions(Throwable t) {
|
||||||
|
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
||||||
|
return ok();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import play.mvc.With;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
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 OpenAPIUtils {
|
||||||
|
|
||||||
|
@With(ApiCall.class)
|
||||||
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
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<>();
|
||||||
|
|
||||||
|
if (values == null) {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
||||||
|
params.addAll(Arrays.asList(values));
|
||||||
|
} else {
|
||||||
|
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
||||||
|
|
||||||
|
String delimiter = ",";
|
||||||
|
|
||||||
|
switch(collectionFormat) {
|
||||||
|
case "csv": {
|
||||||
|
delimiter = ",";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "ssv": {
|
||||||
|
delimiter = " ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "tsv": {
|
||||||
|
delimiter = "\t";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "pipes": {
|
||||||
|
delimiter = "|";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
params = Arrays.asList(values[0].split(delimiter));
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parameterToString(Object param) {
|
||||||
|
if (param == null) {
|
||||||
|
return "";
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return formatDatetime((Date) param);
|
||||||
|
} else if (param instanceof Collection) {
|
||||||
|
StringBuilder b = new StringBuilder();
|
||||||
|
for (Object o : (Collection)param) {
|
||||||
|
if (b.length() > 0) {
|
||||||
|
b.append(",");
|
||||||
|
}
|
||||||
|
b.append(String.valueOf(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.toString();
|
||||||
|
} else {
|
||||||
|
return String.valueOf(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatDatetime(Date date) {
|
||||||
|
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
|
||||||
import play.mvc.Action;
|
|
||||||
import play.mvc.Http;
|
|
||||||
import play.mvc.Result;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
private ApiCall() {}
|
|
||||||
|
|
||||||
public CompletionStage<Result> call(Http.Context ctx) {
|
|
||||||
try {
|
|
||||||
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
|
||||||
return delegate.call(ctx);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
//TODO: log the error in your metric
|
|
||||||
|
|
||||||
//We rethrow this error so it will be caught in the ErrorHandler
|
|
||||||
throw t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
|
||||||
import play.api.OptionalSourceMapper;
|
|
||||||
import play.api.UsefulException;
|
|
||||||
import play.api.routing.Router;
|
|
||||||
import play.http.DefaultHttpErrorHandler;
|
|
||||||
import play.mvc.Http.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
import static play.mvc.Results.*;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class ErrorHandler extends DefaultHttpErrorHandler {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
|
||||||
super(configuration, environment, sourceMapper, routes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
|
||||||
//Since the error is already handled, we don't want to print anything on the console
|
|
||||||
//But if you want to have the error printed in the console, just delete this override
|
|
||||||
}
|
|
||||||
|
|
||||||
private Result handleExceptions(Throwable t) {
|
|
||||||
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
|
||||||
return ok();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,103 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import play.mvc.With;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
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)
|
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
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<>();
|
|
||||||
|
|
||||||
if (values == null) {
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
|
||||||
params.addAll(Arrays.asList(values));
|
|
||||||
} else {
|
|
||||||
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
|
||||||
|
|
||||||
String delimiter = ",";
|
|
||||||
|
|
||||||
switch(collectionFormat) {
|
|
||||||
case "csv": {
|
|
||||||
delimiter = ",";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "ssv": {
|
|
||||||
delimiter = " ";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "tsv": {
|
|
||||||
delimiter = "\t";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "pipes": {
|
|
||||||
delimiter = "|";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
params = Arrays.asList(values[0].split(delimiter));
|
|
||||||
}
|
|
||||||
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parameterToString(Object param) {
|
|
||||||
if (param == null) {
|
|
||||||
return "";
|
|
||||||
} else if (param instanceof Date) {
|
|
||||||
return formatDatetime((Date) param);
|
|
||||||
} else if (param instanceof Collection) {
|
|
||||||
StringBuilder b = new StringBuilder();
|
|
||||||
for (Object o : (Collection)param) {
|
|
||||||
if (b.length() > 0) {
|
|
||||||
b.append(",");
|
|
||||||
}
|
|
||||||
b.append(String.valueOf(o));
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.toString();
|
|
||||||
} else {
|
|
||||||
return String.valueOf(param);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String formatDatetime(Date date) {
|
|
||||||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
# When using bean validation with the OpenAPI 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
|
# 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.
|
# 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
|
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||||
@ -26,7 +26,7 @@ play.filters.headers.contentSecurityPolicy=null
|
|||||||
useInputBeanValidation=true
|
useInputBeanValidation=true
|
||||||
useOutputBeanValidation=true
|
useOutputBeanValidation=true
|
||||||
|
|
||||||
play.http.errorHandler="swagger.ErrorHandler"
|
play.http.errorHandler="openapitools.ErrorHandler"
|
||||||
|
|
||||||
## Akka
|
## Akka
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||||
|
@ -14,7 +14,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
@ -43,7 +43,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -71,7 +71,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -82,7 +82,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByStatus(status);
|
List<Pet> obj = imp.findPetsByStatus(status);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -95,7 +95,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -106,7 +106,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByTags(tags);
|
List<Pet> obj = imp.findPetsByTags(tags);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -117,7 +117,7 @@ public class PetApiController extends Controller {
|
|||||||
public Result getPetById(Long petId) throws Exception {
|
public Result getPetById(Long petId) throws Exception {
|
||||||
Pet obj = imp.getPetById(petId);
|
Pet obj = imp.getPetById(petId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -130,7 +130,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -171,7 +171,7 @@ public class PetApiController extends Controller {
|
|||||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
@ -52,7 +52,7 @@ public class StoreApiController extends Controller {
|
|||||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||||
Order obj = imp.getOrderById(orderId);
|
Order obj = imp.getOrderById(orderId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -65,14 +65,14 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
}
|
}
|
||||||
Order obj = imp.placeOrder(order);
|
Order obj = imp.placeOrder(order);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
@ -42,7 +42,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -59,7 +59,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -77,7 +77,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -97,7 +97,7 @@ public class UserApiController extends Controller {
|
|||||||
public Result getUserByName(String username) throws Exception {
|
public Result getUserByName(String username) throws Exception {
|
||||||
User obj = imp.getUserByName(username);
|
User obj = imp.getUserByName(username);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -137,7 +137,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
|
import play.*;
|
||||||
|
import play.api.OptionalSourceMapper;
|
||||||
|
import play.api.UsefulException;
|
||||||
|
import play.api.routing.Router;
|
||||||
|
import play.http.DefaultHttpErrorHandler;
|
||||||
|
import play.mvc.Http.*;
|
||||||
|
import play.mvc.*;
|
||||||
|
|
||||||
|
import javax.inject.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
import static play.mvc.Results.*;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
public class ErrorHandler extends DefaultHttpErrorHandler {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
||||||
|
super(configuration, environment, sourceMapper, routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
||||||
|
//Since the error is already handled, we don't want to print anything on the console
|
||||||
|
//But if you want to have the error printed in the console, just delete this override
|
||||||
|
}
|
||||||
|
|
||||||
|
private Result handleExceptions(Throwable t) {
|
||||||
|
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
||||||
|
return ok();
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package swagger;
|
package openapitools;
|
||||||
|
|
||||||
import play.mvc.With;
|
import play.mvc.With;
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ import javax.validation.Validation;
|
|||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
import javax.validation.ValidatorFactory;
|
import javax.validation.ValidatorFactory;
|
||||||
|
|
||||||
public class SwaggerUtils {
|
public class OpenAPIUtils {
|
||||||
|
|
||||||
|
|
||||||
public static <T> void validate(T obj) {
|
public static <T> void validate(T obj) {
|
@ -1,49 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
|
||||||
import play.api.OptionalSourceMapper;
|
|
||||||
import play.api.UsefulException;
|
|
||||||
import play.api.routing.Router;
|
|
||||||
import play.http.DefaultHttpErrorHandler;
|
|
||||||
import play.mvc.Http.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
import static play.mvc.Results.*;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class ErrorHandler extends DefaultHttpErrorHandler {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
|
||||||
super(configuration, environment, sourceMapper, routes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
|
||||||
//Since the error is already handled, we don't want to print anything on the console
|
|
||||||
//But if you want to have the error printed in the console, just delete this override
|
|
||||||
}
|
|
||||||
|
|
||||||
private Result handleExceptions(Throwable t) {
|
|
||||||
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
|
||||||
return ok();
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
play.filters.headers.contentSecurityPolicy=null
|
play.filters.headers.contentSecurityPolicy=null
|
||||||
|
|
||||||
# When using bean validation with the swagger api, the validator will check that every constraint is respected
|
# When using bean validation with the OpenAPI 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
|
# 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.
|
# 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
|
# If this is a concern in your application, or if you don't want to validate the output coming from your API for
|
||||||
@ -26,7 +26,7 @@ play.filters.headers.contentSecurityPolicy=null
|
|||||||
useInputBeanValidation=true
|
useInputBeanValidation=true
|
||||||
useOutputBeanValidation=true
|
useOutputBeanValidation=true
|
||||||
|
|
||||||
play.http.errorHandler="swagger.ErrorHandler"
|
play.http.errorHandler="openapitools.ErrorHandler"
|
||||||
|
|
||||||
## Akka
|
## Akka
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class PetApiController extends Controller {
|
public class PetApiController extends Controller {
|
||||||
@ -44,7 +44,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -72,7 +72,7 @@ public class PetApiController extends Controller {
|
|||||||
if (statusArray == null) {
|
if (statusArray == null) {
|
||||||
throw new IllegalArgumentException("'status' parameter is required");
|
throw new IllegalArgumentException("'status' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
|
List<String> statusList = OpenAPIUtils.parametersToList("csv", statusArray);
|
||||||
List<String> status = new ArrayList<String>();
|
List<String> status = new ArrayList<String>();
|
||||||
for (String curParam : statusList) {
|
for (String curParam : statusList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -83,7 +83,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByStatus(status);
|
List<Pet> obj = imp.findPetsByStatus(status);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -96,7 +96,7 @@ public class PetApiController extends Controller {
|
|||||||
if (tagsArray == null) {
|
if (tagsArray == null) {
|
||||||
throw new IllegalArgumentException("'tags' parameter is required");
|
throw new IllegalArgumentException("'tags' parameter is required");
|
||||||
}
|
}
|
||||||
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
|
List<String> tagsList = OpenAPIUtils.parametersToList("csv", tagsArray);
|
||||||
List<String> tags = new ArrayList<String>();
|
List<String> tags = new ArrayList<String>();
|
||||||
for (String curParam : tagsList) {
|
for (String curParam : tagsList) {
|
||||||
if (!curParam.isEmpty()) {
|
if (!curParam.isEmpty()) {
|
||||||
@ -107,7 +107,7 @@ public class PetApiController extends Controller {
|
|||||||
List<Pet> obj = imp.findPetsByTags(tags);
|
List<Pet> obj = imp.findPetsByTags(tags);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
for (Pet curItem : obj) {
|
for (Pet curItem : obj) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
@ -118,7 +118,7 @@ public class PetApiController extends Controller {
|
|||||||
public Result getPetById(Long petId) throws Exception {
|
public Result getPetById(Long petId) throws Exception {
|
||||||
Pet obj = imp.getPetById(petId);
|
Pet obj = imp.getPetById(petId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -131,7 +131,7 @@ public class PetApiController extends Controller {
|
|||||||
if (nodepet != null) {
|
if (nodepet != null) {
|
||||||
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
pet = mapper.readValue(nodepet.toString(), Pet.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(pet);
|
OpenAPIUtils.validate(pet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Pet' parameter is required");
|
throw new IllegalArgumentException("'Pet' parameter is required");
|
||||||
@ -172,7 +172,7 @@ public class PetApiController extends Controller {
|
|||||||
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
Http.MultipartFormData.FilePart file = request().body().asMultipartFormData().getFile("file");
|
||||||
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
ModelApiResponse obj = imp.uploadFile(petId, additionalMetadata, file);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class StoreApiController extends Controller {
|
public class StoreApiController extends Controller {
|
||||||
@ -53,7 +53,7 @@ public class StoreApiController extends Controller {
|
|||||||
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
|
||||||
Order obj = imp.getOrderById(orderId);
|
Order obj = imp.getOrderById(orderId);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -66,14 +66,14 @@ public class StoreApiController extends Controller {
|
|||||||
if (nodeorder != null) {
|
if (nodeorder != null) {
|
||||||
order = mapper.readValue(nodeorder.toString(), Order.class);
|
order = mapper.readValue(nodeorder.toString(), Order.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(order);
|
OpenAPIUtils.validate(order);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'Order' parameter is required");
|
throw new IllegalArgumentException("'Order' parameter is required");
|
||||||
}
|
}
|
||||||
Order obj = imp.placeOrder(order);
|
Order obj = imp.placeOrder(order);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
|
@ -13,13 +13,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import swagger.SwaggerUtils;
|
import openapitools.OpenAPIUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import play.Configuration;
|
import play.Configuration;
|
||||||
|
|
||||||
import swagger.SwaggerUtils.ApiAction;
|
import openapitools.OpenAPIUtils.ApiAction;
|
||||||
|
|
||||||
|
|
||||||
public class UserApiController extends Controller {
|
public class UserApiController extends Controller {
|
||||||
@ -43,7 +43,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
@ -60,7 +60,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -78,7 +78,7 @@ public class UserApiController extends Controller {
|
|||||||
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
user = mapper.readValue(nodeuser.toString(), new TypeReference<List<User>>(){});
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
for (User curItem : user) {
|
for (User curItem : user) {
|
||||||
SwaggerUtils.validate(curItem);
|
OpenAPIUtils.validate(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -98,7 +98,7 @@ public class UserApiController extends Controller {
|
|||||||
public Result getUserByName(String username) throws Exception {
|
public Result getUserByName(String username) throws Exception {
|
||||||
User obj = imp.getUserByName(username);
|
User obj = imp.getUserByName(username);
|
||||||
if (configuration.getBoolean("useOutputBeanValidation")) {
|
if (configuration.getBoolean("useOutputBeanValidation")) {
|
||||||
SwaggerUtils.validate(obj);
|
OpenAPIUtils.validate(obj);
|
||||||
}
|
}
|
||||||
JsonNode result = mapper.valueToTree(obj);
|
JsonNode result = mapper.valueToTree(obj);
|
||||||
return ok(result);
|
return ok(result);
|
||||||
@ -138,7 +138,7 @@ public class UserApiController extends Controller {
|
|||||||
if (nodeuser != null) {
|
if (nodeuser != null) {
|
||||||
user = mapper.readValue(nodeuser.toString(), User.class);
|
user = mapper.readValue(nodeuser.toString(), User.class);
|
||||||
if (configuration.getBoolean("useInputBeanValidation")) {
|
if (configuration.getBoolean("useInputBeanValidation")) {
|
||||||
SwaggerUtils.validate(user);
|
OpenAPIUtils.validate(user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("'User' parameter is required");
|
throw new IllegalArgumentException("'User' parameter is required");
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import play.mvc.Action;
|
||||||
|
import play.mvc.Http;
|
||||||
|
import play.mvc.Result;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
|
public class ApiCall extends Action<OpenAPIUtils.ApiAction> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ApiCall() {}
|
||||||
|
|
||||||
|
public CompletionStage<Result> call(Http.Context ctx) {
|
||||||
|
try {
|
||||||
|
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
||||||
|
return delegate.call(ctx);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
//TODO: log the error in your metric
|
||||||
|
|
||||||
|
//We rethrow this error so it will be caught in the ErrorHandler
|
||||||
|
throw t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
|
||||||
|
import play.*;
|
||||||
|
import play.api.OptionalSourceMapper;
|
||||||
|
import play.api.UsefulException;
|
||||||
|
import play.api.routing.Router;
|
||||||
|
import play.http.DefaultHttpErrorHandler;
|
||||||
|
import play.mvc.Http.*;
|
||||||
|
import play.mvc.*;
|
||||||
|
|
||||||
|
import javax.inject.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
import static play.mvc.Results.*;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
public class ErrorHandler extends DefaultHttpErrorHandler {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
||||||
|
super(configuration, environment, sourceMapper, routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
||||||
|
return CompletableFuture.completedFuture(
|
||||||
|
handleExceptions(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
||||||
|
//Since the error is already handled, we don't want to print anything on the console
|
||||||
|
//But if you want to have the error printed in the console, just delete this override
|
||||||
|
}
|
||||||
|
|
||||||
|
private Result handleExceptions(Throwable t) {
|
||||||
|
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
||||||
|
return ok();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
package openapitools;
|
||||||
|
|
||||||
|
import play.mvc.With;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
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 OpenAPIUtils {
|
||||||
|
|
||||||
|
@With(ApiCall.class)
|
||||||
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
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<>();
|
||||||
|
|
||||||
|
if (values == null) {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
||||||
|
params.addAll(Arrays.asList(values));
|
||||||
|
} else {
|
||||||
|
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
||||||
|
|
||||||
|
String delimiter = ",";
|
||||||
|
|
||||||
|
switch(collectionFormat) {
|
||||||
|
case "csv": {
|
||||||
|
delimiter = ",";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "ssv": {
|
||||||
|
delimiter = " ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "tsv": {
|
||||||
|
delimiter = "\t";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "pipes": {
|
||||||
|
delimiter = "|";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
params = Arrays.asList(values[0].split(delimiter));
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parameterToString(Object param) {
|
||||||
|
if (param == null) {
|
||||||
|
return "";
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return formatDatetime((Date) param);
|
||||||
|
} else if (param instanceof Collection) {
|
||||||
|
StringBuilder b = new StringBuilder();
|
||||||
|
for (Object o : (Collection)param) {
|
||||||
|
if (b.length() > 0) {
|
||||||
|
b.append(",");
|
||||||
|
}
|
||||||
|
b.append(String.valueOf(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.toString();
|
||||||
|
} else {
|
||||||
|
return String.valueOf(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatDatetime(Date date) {
|
||||||
|
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
|
||||||
import play.mvc.Action;
|
|
||||||
import play.mvc.Http;
|
|
||||||
import play.mvc.Result;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
|
|
||||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
private ApiCall() {}
|
|
||||||
|
|
||||||
public CompletionStage<Result> call(Http.Context ctx) {
|
|
||||||
try {
|
|
||||||
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
|
||||||
return delegate.call(ctx);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
//TODO: log the error in your metric
|
|
||||||
|
|
||||||
//We rethrow this error so it will be caught in the ErrorHandler
|
|
||||||
throw t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
|
|
||||||
import play.*;
|
|
||||||
import play.api.OptionalSourceMapper;
|
|
||||||
import play.api.UsefulException;
|
|
||||||
import play.api.routing.Router;
|
|
||||||
import play.http.DefaultHttpErrorHandler;
|
|
||||||
import play.mvc.Http.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
import static play.mvc.Results.*;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class ErrorHandler extends DefaultHttpErrorHandler {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
|
||||||
super(configuration, environment, sourceMapper, routes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
|
||||||
return CompletableFuture.completedFuture(
|
|
||||||
handleExceptions(exception)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
|
||||||
//Since the error is already handled, we don't want to print anything on the console
|
|
||||||
//But if you want to have the error printed in the console, just delete this override
|
|
||||||
}
|
|
||||||
|
|
||||||
private Result handleExceptions(Throwable t) {
|
|
||||||
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
|
||||||
return ok();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,103 +0,0 @@
|
|||||||
package swagger;
|
|
||||||
|
|
||||||
import play.mvc.With;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
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)
|
|
||||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
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<>();
|
|
||||||
|
|
||||||
if (values == null) {
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
|
||||||
params.addAll(Arrays.asList(values));
|
|
||||||
} else {
|
|
||||||
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
|
||||||
|
|
||||||
String delimiter = ",";
|
|
||||||
|
|
||||||
switch(collectionFormat) {
|
|
||||||
case "csv": {
|
|
||||||
delimiter = ",";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "ssv": {
|
|
||||||
delimiter = " ";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "tsv": {
|
|
||||||
delimiter = "\t";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "pipes": {
|
|
||||||
delimiter = "|";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
params = Arrays.asList(values[0].split(delimiter));
|
|
||||||
}
|
|
||||||
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parameterToString(Object param) {
|
|
||||||
if (param == null) {
|
|
||||||
return "";
|
|
||||||
} else if (param instanceof Date) {
|
|
||||||
return formatDatetime((Date) param);
|
|
||||||
} else if (param instanceof Collection) {
|
|
||||||
StringBuilder b = new StringBuilder();
|
|
||||||
for (Object o : (Collection)param) {
|
|
||||||
if (b.length() > 0) {
|
|
||||||
b.append(",");
|
|
||||||
}
|
|
||||||
b.append(String.valueOf(o));
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.toString();
|
|
||||||
} else {
|
|
||||||
return String.valueOf(param);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String formatDatetime(Date date) {
|
|
||||||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
|
||||||
}
|
|
||||||
}
|
|
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