Search in sources :

Example 51 with Content

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);
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 52 with Content

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));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 53 with Content

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);
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 54 with Content

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);
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 55 with Content

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);
}
Also used : Path(java.nio.file.Path) Constants(org.ballerinalang.ballerina.swagger.convertor.Constants) SwaggerConverter(io.swagger.v3.parser.converter.SwaggerConverter) Files(java.nio.file.Files) Yaml(io.swagger.v3.core.util.Yaml) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) Swagger(io.swagger.models.Swagger) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) IOException(java.io.IOException) HashMap(java.util.HashMap) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) StringUtils(org.apache.commons.lang3.StringUtils) ParserUtils(org.ballerinalang.composer.service.ballerina.parser.service.util.ParserUtils) Collectors(java.util.stream.Collectors) ServiceNode(org.ballerinalang.model.tree.ServiceNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Paths(java.nio.file.Paths) Map(java.util.Map) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) Optional(java.util.Optional) Path(java.nio.file.Path) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) HashMap(java.util.HashMap) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)121 Test (org.testng.annotations.Test)120 MediaType (io.swagger.v3.oas.models.media.MediaType)70 Schema (io.swagger.v3.oas.models.media.Schema)70 Operation (io.swagger.v3.oas.annotations.Operation)65 Content (io.swagger.v3.oas.models.media.Content)64 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)62 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)59 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)57 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)53 Operation (io.swagger.v3.oas.models.Operation)52 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)51 StringSchema (io.swagger.v3.oas.models.media.StringSchema)50 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)42 PathItem (io.swagger.v3.oas.models.PathItem)41 ArrayList (java.util.ArrayList)41 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)40 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)37 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)37 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)33