use of io.swagger.v3.oas.annotations.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testJsonBody.
@Test
public void testJsonBody() throws Exception {
Operation op = testSpec.getPaths().get("/jsonBodyTest/sampleTest").getPost();
if (op.getParameters() == null)
op.setParameters(new ArrayList<>());
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/jsonBodyTest/sampleTest", HttpMethod.POST, false, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
});
JsonObject object = new JsonObject();
object.put("id", "anId");
List<Integer> valuesArray = new ArrayList<>();
for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.INT).getInteger());
object.put("values", valuesArray);
testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest", object, 200, "OK", object);
testRequestWithJSONAndCustomContentType(HttpMethod.POST, "/jsonBodyTest/sampleTest", "application/json; charset=utf-8", object, 200, "OK", object);
testRequestWithJSONAndCustomContentType(HttpMethod.POST, "/jsonBodyTest/sampleTest", "application/superapplication+json", object, 200, "OK", object);
}
use of io.swagger.v3.oas.annotations.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testAdditionalPropertiesJson.
@Test
public void testAdditionalPropertiesJson() throws Exception {
Operation op = testSpec.getPaths().get("/additionalProperties").getPost();
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/additionalProperties", HttpMethod.POST, true, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
});
JsonObject pet = new JsonObject();
pet.put("id", 14612);
pet.put("name", "Willy");
pet.put("lazyness", "Highest");
testRequestWithJSON(HttpMethod.POST, "/additionalProperties", pet, 400, errorMessage(ValidationException.ErrorType.JSON_INVALID));
}
use of io.swagger.v3.oas.annotations.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testJsonBodyFailure.
@Test
public void testJsonBodyFailure() throws Exception {
Operation op = testSpec.getPaths().get("/jsonBodyTest/sampleTest").getPost();
if (op.getParameters() == null)
op.setParameters(new ArrayList<>());
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/jsonBodyTest/sampleTest", HttpMethod.POST, true, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
});
JsonObject object = new JsonObject();
object.put("id", "anId");
List<String> valuesArray = new ArrayList<>();
for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.INT).getInteger().toString());
valuesArray.add(2, getFailureSample(ParameterType.INT));
object.put("values", valuesArray);
testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest", object, 400, errorMessage(ValidationException.ErrorType.JSON_INVALID), null);
}
use of io.swagger.v3.oas.annotations.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testCircularReferences.
@Test
@Ignore
public void testCircularReferences() throws Exception {
Operation op = testSpec.getPaths().get("/circularReferences").getPost();
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/circularReferences", HttpMethod.POST, false, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
});
JsonObject obj = new JsonObject("{\n" + " \"a\": {\n" + " \"a\": [\n" + " {\n" + " \"a\": {\n" + " \"a\": []\n" + " },\n" + " \"b\": \"hi\",\n" + " \"c\": 10\n" + " }\n" + " ]\n" + " },\n" + " \"b\": \"hello\",\n" + " \"c\": 6\n" + "}");
testRequestWithJSON(HttpMethod.POST, "/circularReferences", obj, 200, "OK", obj);
}
use of io.swagger.v3.oas.annotations.media.Content in project ballerina by ballerina-lang.
the class SwaggerConverterUtils method getTopLevelNodeFromBallerinaFile.
/**
* Generate ballerina fine from the String definition.
*
* @param bFile ballerina string definition
* @return ballerina file created from ballerina string definition
* @throws IOException IO exception
*/
public static BLangCompilationUnit getTopLevelNodeFromBallerinaFile(BFile bFile) throws IOException {
String filePath = bFile.getFilePath();
String fileName = bFile.getFileName();
String content = bFile.getContent();
Path fileRoot = Paths.get(filePath);
org.wso2.ballerinalang.compiler.tree.BLangPackage model;
// Sometimes we are getting Ballerina content without a file in the file-system.
if (!Files.exists(Paths.get(filePath, fileName))) {
BallerinaFile ballerinaFile = ParserUtils.getBallerinaFileForContent(fileRoot, fileName, content, CompilerPhase.CODE_ANALYZE);
model = ballerinaFile.getBLangPackage();
} else {
BallerinaFile ballerinaFile = ParserUtils.getBallerinaFile(filePath, fileName);
model = ballerinaFile.getBLangPackage();
}
final Map<String, ModelPackage> modelPackage = new HashMap<>();
ParserUtils.loadPackageMap(Constants.CURRENT_PACKAGE_NAME, model, modelPackage);
Optional<BLangCompilationUnit> compilationUnit = model.getCompilationUnits().stream().filter(compUnit -> fileName.equals(compUnit.getName())).findFirst();
return compilationUnit.orElse(null);
}
Aggregations