Search in sources :

Example 16 with Content

use of io.swagger.v3.oas.models.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 17 with Content

use of io.swagger.v3.oas.models.media.Content in project syncope by apache.

the class SyncopeOpenApiCustomizer method customize.

@Override
public OpenAPIConfiguration customize(final OpenAPIConfiguration configuration) {
    Map<String, Header> headers = new LinkedHashMap<>();
    headers.put(RESTHeaders.ERROR_CODE, new Header().schema(new Schema<>().type("string")).description("Error code"));
    headers.put(RESTHeaders.ERROR_INFO, new Header().schema(new Schema<>().type("string")).description("Error message"));
    Content content = new Content();
    content.addMediaType(javax.ws.rs.core.MediaType.APPLICATION_JSON, new MediaType().schema(new Schema<ErrorTO>()));
    content.addMediaType(javax.ws.rs.core.MediaType.APPLICATION_XML, new MediaType().schema(new Schema<ErrorTO>()));
    configuration.getOpenAPI().getComponents().addResponses("400", new ApiResponse().description("An error occurred; HTTP status code can vary depending on the actual error: " + "400, 403, 404, 409, 412").headers(headers).content(content));
    return super.customize(configuration);
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) Content(io.swagger.v3.oas.models.media.Content) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) LinkedHashMap(java.util.LinkedHashMap)

Example 18 with Content

use of io.swagger.v3.oas.models.media.Content in project gravitee-management-rest-api by gravitee-io.

the class SwaggerServiceImpl method transformV3.

private OpenAPI transformV3(String content, PageConfigurationEntity config) {
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(content, null, null);
    if (result != null && config != null && config.getTryItURL() != null) {
        URI newURI = URI.create(config.getTryItURL());
        result.getOpenAPI().getServers().forEach(server -> {
            try {
                server.setUrl(new URI(newURI.getScheme(), newURI.getUserInfo(), newURI.getHost(), newURI.getPort(), newURI.getPath(), newURI.getQuery(), newURI.getFragment()).toString());
            } catch (URISyntaxException e) {
                logger.error(e.getMessage(), e);
            }
        });
    }
    if (result != null) {
        return result.getOpenAPI();
    } else {
        return null;
    }
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) URISyntaxException(java.net.URISyntaxException) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) URI(java.net.URI)

Example 19 with Content

use of io.swagger.v3.oas.models.media.Content in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testPathLabelExplodeArray.

/**
 * Test: path_label_explode_array
 * Expected parameters sent:
 * color: .blue.black.brown
 * Expected response: {"color":["blue","black","brown"]}
 * @throws Exception
 */
@Test
public void testPathLabelExplodeArray() throws Exception {
    routerFactory.addHandlerByOperationId("path_label_explode_array", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_path = params.pathParameter("color");
        assertNotNull(color_path);
        assertTrue(color_path.isArray());
        res.put("color", new JsonArray(color_path.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    List<Object> color_path;
    color_path = new ArrayList<>();
    color_path.add("blue");
    color_path.add("black");
    color_path.add("brown");
    startServer();
    apiClient.pathLabelExplodeArray(color_path, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpResponse(io.vertx.ext.web.client.HttpResponse) HashMap(java.util.HashMap) RoutingContext(io.vertx.ext.web.RoutingContext) OpenAPI3RouterFactoryImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl) ArrayList(java.util.ArrayList) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) RequestParameters(io.vertx.ext.web.api.RequestParameters) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Files(java.nio.file.Files) RequestParameter(io.vertx.ext.web.api.RequestParameter) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) WebTestValidationBase(io.vertx.ext.web.api.validation.WebTestValidationBase) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Rule(org.junit.Rule) ExternalResource(org.junit.rules.ExternalResource) Paths(java.nio.file.Paths) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 20 with Content

use of io.swagger.v3.oas.models.media.Content in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testPathSimpleNoexplodeArray.

/**
 * Test: path_simple_noexplode_array
 * Expected parameters sent:
 * color: blue,black,brown
 * Expected response: {"color":["blue","black","brown"]}
 * @throws Exception
 */
@Test
public void testPathSimpleNoexplodeArray() throws Exception {
    routerFactory.addHandlerByOperationId("path_simple_noexplode_array", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_path = params.pathParameter("color");
        assertNotNull(color_path);
        assertTrue(color_path.isArray());
        res.put("color", new JsonArray(color_path.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    List<Object> color_path;
    color_path = new ArrayList<>();
    color_path.add("blue");
    color_path.add("black");
    color_path.add("brown");
    startServer();
    apiClient.pathSimpleNoexplodeArray(color_path, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpResponse(io.vertx.ext.web.client.HttpResponse) HashMap(java.util.HashMap) RoutingContext(io.vertx.ext.web.RoutingContext) OpenAPI3RouterFactoryImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl) ArrayList(java.util.ArrayList) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) RequestParameters(io.vertx.ext.web.api.RequestParameters) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Files(java.nio.file.Files) RequestParameter(io.vertx.ext.web.api.RequestParameter) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) WebTestValidationBase(io.vertx.ext.web.api.validation.WebTestValidationBase) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Rule(org.junit.Rule) ExternalResource(org.junit.rules.ExternalResource) Paths(java.nio.file.Paths) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)22 RequestParameters (io.vertx.ext.web.api.RequestParameters)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)19 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)18 IOException (java.io.IOException)18 Files (java.nio.file.Files)18 Paths (java.nio.file.Paths)18 HashMap (java.util.HashMap)18 Map (java.util.Map)18 Collectors (java.util.stream.Collectors)18 OpenAPI (io.swagger.v3.oas.models.OpenAPI)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17 AsyncResult (io.vertx.core.AsyncResult)17 Handler (io.vertx.core.Handler)17 HttpServerOptions (io.vertx.core.http.HttpServerOptions)17 JsonArray (io.vertx.core.json.JsonArray)17 RoutingContext (io.vertx.ext.web.RoutingContext)17 RequestParameter (io.vertx.ext.web.api.RequestParameter)17 RouterFactoryOptions (io.vertx.ext.web.api.contract.RouterFactoryOptions)17