Search in sources :

Example 16 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testPaths.

@Test
public void testPaths() {
    String json = "{\n" + "  \"openapi\": \"3.0.0\",\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"foo\": \"bar\",\n" + "      \"get\": {\n" + "        \"security\": [\n" + "          {\n" + "            \"petstore_auth\": [\n" + "              \"write:pets\",\n" + "              \"read:pets\"\n" + "            ]\n" + "          }\n" + "        ]\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertTrue(messages.contains("attribute paths.'/pet'.foo is unexpected"));
    OpenAPI openAPI = result.getOpenAPI();
    PathItem path = openAPI.getPaths().get("/pet");
    assertNotNull(path);
    Operation operation = path.getGet();
    assertNotNull(operation);
    List<SecurityRequirement> security = operation.getSecurity();
    assertTrue(security.size() == 1);
    Map<String, List<String>> requirement = security.get(0);
    assertTrue(requirement.containsKey("petstore_auth"));
    List<String> scopesList = requirement.get("petstore_auth");
    Set<String> scopes = new HashSet<>(scopesList);
    assertTrue(scopes.contains("read:pets"));
    assertTrue(scopes.contains("write:pets"));
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) PathItem(io.swagger.v3.oas.models.PathItem) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) OpenAPI(io.swagger.v3.oas.models.OpenAPI) HashSet(java.util.HashSet) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 17 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testPathsWithRefResponse.

@Test
public void testPathsWithRefResponse() {
    String json = "{\n" + "  \"openapi\": \"3.0.0\",\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"get\": {\n" + "        \"responses\": {\n" + "          \"200\": {\n" + "            \"$ref\": \"#/components/responses/OK\"" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    PathItem path = openAPI.getPaths().get("/pet");
    assertNotNull(path);
    Operation operation = path.getGet();
    assertNotNull(operation);
    assertTrue(operation.getResponses().containsKey("200"));
    assertEquals(ApiResponse.class, operation.getResponses().get("200").getClass());
    ApiResponse refResponse = operation.getResponses().get("200");
    assertEquals("#/components/responses/OK", refResponse.get$ref());
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 18 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method readSchemaArray.

@Test(dataProvider = "data")
public void readSchemaArray(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 19);
    // parameters operation get
    PathItem petByStatusEndpoint = paths.get("/pet/findByTags");
    Assert.assertNotNull(petByStatusEndpoint.getGet());
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().size(), 1);
    Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getSchema());
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getSchema().getType(), "array");
    Assert.assertEquals(((ArraySchema) (petByStatusEndpoint.getGet().getParameters().get(0).getSchema())).getItems().getType(), "string");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getName(), "tags");
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getExplode(), Boolean.TRUE);
    Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getStyle(), StyleEnum.FORM);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 19 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class SwaggerConverter method convert.

public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
    if (parse == null) {
        return null;
    }
    SwaggerParseResult output = new SwaggerParseResult().messages(parse.getMessages());
    if (parse.getSwagger() == null) {
        return output;
    }
    OpenAPI openAPI = new OpenAPI();
    SwaggerInventory inventory = new SwaggerInventory().process(parse.getSwagger());
    Swagger swagger = parse.getSwagger();
    if (swagger.getVendorExtensions() != null) {
        openAPI.setExtensions(convert(swagger.getVendorExtensions()));
    }
    // Set extension to retain original version of OAS document.
    openAPI.addExtension("x-original-swagger-version", swagger.getSwagger());
    if (swagger.getExternalDocs() != null) {
        openAPI.setExternalDocs(convert(swagger.getExternalDocs()));
    }
    if (swagger.getInfo() != null) {
        openAPI.setInfo(convert(swagger.getInfo()));
    }
    openAPI.setServers(convert(swagger.getSchemes(), swagger.getHost(), swagger.getBasePath()));
    if (swagger.getTags() != null) {
        openAPI.setTags(convertTags(swagger.getTags()));
    }
    if (swagger.getConsumes() != null) {
        this.globalConsumes.addAll(swagger.getConsumes());
    }
    if (swagger.getProduces() != null) {
        this.globalProduces.addAll(swagger.getProduces());
    }
    if (swagger.getSecurity() != null && swagger.getSecurity().size() > 0) {
        openAPI.setSecurity(convertSecurityRequirements(swagger.getSecurity()));
    }
    List<Model> models = inventory.getModels();
    // TODO until we have the example object working correctly in v3 pojos...
    for (Model model : models) {
        if (model instanceof RefModel) {
            RefModel ref = (RefModel) model;
            if (ref.get$ref().indexOf("#/definitions") == 0) {
                String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
                ref.set$ref(updatedRef);
            }
        }
    }
    for (Property property : inventory.getProperties()) {
        if (property instanceof RefProperty) {
            RefProperty ref = (RefProperty) property;
            if (ref.get$ref().indexOf("#/definitions") == 0) {
                String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
                ref.set$ref(updatedRef);
            }
        }
        if (property instanceof ComposedProperty) {
            ComposedProperty comprop = (ComposedProperty) property;
            if (comprop.getAllOf() != null) {
                for (Property item : comprop.getAllOf()) {
                    if (item instanceof RefProperty) {
                        RefProperty ref = (RefProperty) item;
                        if (ref.get$ref().indexOf("#/definitions") == 0) {
                            String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
                            ref.set$ref(updatedRef);
                        }
                    }
                }
            }
        }
    }
    if (swagger.getParameters() != null) {
        globalV2Parameters.putAll(swagger.getParameters());
        swagger.getParameters().forEach((k, v) -> {
            if ("body".equals(v.getIn())) {
                components.addRequestBodies(k, convertParameterToRequestBody(v));
            } else if ("formData".equals(v.getIn())) {
                // formData_ is added not to overwrite existing schemas
                components.addSchemas("formData_" + k, convertFormDataToSchema(v));
            } else {
                components.addParameters(k, convert(v));
            }
        });
    }
    Paths v3Paths = new Paths();
    Map<String, Path> pathMap = Optional.ofNullable(swagger.getPaths()).orElse(new HashMap<>());
    for (String pathname : pathMap.keySet()) {
        io.swagger.models.Path v2Path = swagger.getPath(pathname);
        PathItem v3Path = convert(v2Path);
        v3Paths.put(pathname, v3Path);
    }
    openAPI.setPaths(v3Paths);
    if (swagger.getResponses() != null) {
        swagger.getResponses().forEach((k, v) -> components.addResponses(k, convert(v)));
    }
    if (swagger.getDefinitions() != null) {
        for (String key : swagger.getDefinitions().keySet()) {
            Model model = swagger.getDefinitions().get(key);
            Schema schema = convert(model);
            components.addSchemas(key, schema);
        }
    }
    if (swagger.getSecurityDefinitions() != null) {
        swagger.getSecurityDefinitions().forEach((k, v) -> components.addSecuritySchemes(k, convert(v)));
    }
    openAPI.setComponents(components);
    output.setOpenAPI(openAPI);
    return output;
}
Also used : RefPath(io.swagger.models.RefPath) Path(io.swagger.models.Path) RefModel(io.swagger.models.RefModel) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) PathItem(io.swagger.v3.oas.models.PathItem) Path(io.swagger.models.Path) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 20 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class V2ConverterTest method testIssue15.

@Test(description = "Convert extensions everywhere applicable #15")
public void testIssue15() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_15_JSON);
    assertNotNull(oas);
    Info info = oas.getInfo();
    assertNotNull(info.getExtensions().get("x-apis-json"));
    assertNotNull(info.getLicense().getExtensions().get("x-notes"));
    assertNotNull(oas.getExternalDocs().getExtensions().get("x-docs-extension"));
    assertNotNull(oas.getTags().get(0).getExtensions().get("x-tag-extension"));
    assertNotNull(oas.getTags().get(0).getExternalDocs().getExtensions().get("x-tag-docs-extension"));
    PathItem pathItem = oas.getPaths().get("/something");
    assertNotNull(pathItem.getExtensions().get("x-path-item-extension"));
    Operation get = pathItem.getGet();
    assertNotNull(get.getExtensions().get("x-version"));
    assertNotNull(get.getExternalDocs().getExtensions().get("x-operation-docs-extension"));
    assertNotNull(get.getResponses().get("200").getExtensions().get("x-response-extension"));
    ArraySchema schema = (ArraySchema) get.getParameters().get(0).getSchema();
    assertNull(schema.getItems().getExtensions().get(X_EXAMPLE));
    Map<String, SecurityScheme> securitySchemes = oas.getComponents().getSecuritySchemes();
    assertNotNull(securitySchemes);
    assertNotNull(securitySchemes.get("OAuth2Implicit").getExtensions().get("x-auth-extension"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Aggregations

PathItem (io.swagger.v3.oas.models.PathItem)135 OpenAPI (io.swagger.v3.oas.models.OpenAPI)109 Operation (io.swagger.v3.oas.models.Operation)104 Test (org.testng.annotations.Test)92 Schema (io.swagger.v3.oas.models.media.Schema)57 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)50 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)45 StringSchema (io.swagger.v3.oas.models.media.StringSchema)44 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)42 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)42 MediaType (io.swagger.v3.oas.models.media.MediaType)41 Content (io.swagger.v3.oas.models.media.Content)38 Paths (io.swagger.v3.oas.models.Paths)36 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)35 Parameter (io.swagger.v3.oas.models.parameters.Parameter)29 Components (io.swagger.v3.oas.models.Components)26 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)25 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)24 Map (java.util.Map)22 HashMap (java.util.HashMap)20