Search in sources :

Example 16 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method pathsResolver.

@Test
public void pathsResolver() throws Exception {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
    pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
    final JsonNode rootNode = mapper.readTree(pathFile.getBytes());
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    assertEquals(new OpenAPIResolver(openAPI, new ArrayList<>(), null).resolve(), openAPI);
    // internal url pathItem
    assertEquals(openAPI.getPaths().get("/pathItemRef2"), openAPI.getPaths().get("/pet"));
    // internal array schema inside operation -> responses -> content
    ArraySchema schema = (ArraySchema) openAPI.getPaths().get("/pet").getPut().getResponses().get("400").getContent().get("application/json").getSchema();
    assertEquals(schema.getItems().get$ref(), "#/components/schemas/VeryComplexType");
    // replace of parameters in operation and remove the ones from the pathItem
    Assert.assertNotNull(openAPI.getPaths().get("/pet").getPost().getParameters());
    Assert.assertNull(openAPI.getPaths().get("/pet").getParameters());
    // remote ref pathItem
    assertEquals(openAPI.getPaths().get("/pathItemRef").getSummary(), "summary");
    assertEquals(openAPI.getPaths().get("/pathItemRef").getPost().getResponses().get("405").getDescription(), "Invalid input");
    // internal pathItem operation -> response -> schema
    Assert.assertNotNull(openAPI.getPaths().get("/pet/{petId}").getGet().getResponses());
    assertEquals(openAPI.getPaths().get("/pet/{petId}").getGet().getResponses().get("200").getContent().get("application/xml").getSchema().get$ref(), "#/components/schemas/Pet");
    // internal pathItem -> operation -> callback -> pathItem -> operation -> response -> schema
    assertEquals(openAPI.getPaths().get("/pet/{petId}").getGet().getCallbacks().get("mainHook").get("$request.body#/url").getPost().getResponses().get("200").getContent().get("application/xml").getSchema().get$ref(), "#/components/schemas/Pet");
    // internal pathItem -> operation -> requestBody
    Schema id = (Schema) openAPI.getPaths().get("/pet/findByStatus").getGet().getRequestBody().getContent().get("multipart/mixed").getSchema().getProperties().get("id");
    assertEquals(id.get$ref(), "#/components/schemas/Pet");
    // internal parameter url
    assertEquals(openAPI.getPaths().get("/store/inventory").getGet().getParameters().get(0), openAPI.getComponents().getParameters().get("limitParam"));
}
Also used : OpenAPIDeserializer(io.swagger.v3.parser.util.OpenAPIDeserializer) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) File(java.io.File) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 17 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testSharedPathParametersTest.

@Test(description = "resolve shared path parameters")
public void testSharedPathParametersTest() {
    final OpenAPI swagger = new OpenAPI();
    Operation operation = new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("ok!")));
    PathItem path = new PathItem().get(operation);
    path.addParametersItem(new QueryParameter().name("username").schema(new StringSchema()));
    swagger.path("/fun", path);
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    assertNull(resolved.getPaths().get("/fun").getParameters());
    assertTrue(resolved.getPaths().get("/fun").getGet().getParameters().size() == 1);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 18 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testSharedResponses.

@Test(description = "resolve top-level responses")
public void testSharedResponses() {
    final OpenAPI swagger = new OpenAPI();
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(0, new Parameter().$ref("username"));
    swagger.path("/fun", new PathItem().get(new Operation().parameters(parameters).responses(new ApiResponses().addApiResponse("200", new ApiResponse().$ref("#/components/responses/foo")))));
    swagger.components(new Components().addResponses("foo", new ApiResponse().description("ok!")));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    ApiResponse response = resolved.getPaths().get("/fun").getGet().getResponses().get("200");
    assertTrue(response.getDescription().equals("ok!"));
    assertTrue(response instanceof ApiResponse);
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) ArrayList(java.util.ArrayList) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 19 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testIssue291.

@Test
public void testIssue291() {
    String json = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  title: test spec\n" + "  version: '1.0'\n" + "paths:\n" + "  /test:\n" + "    get:\n" + "      description: test get\n" + "      parameters:\n" + "        - $ref: '#/components/parameters/testParam'\n" + "      responses:\n" + "        default:\n" + "          description: test response\n" + "components:\n" + "  parameters:\n" + "    testParam:\n" + "      name: test\n" + "      in: query\n" + "      style: form\n" + "      schema:\n" + "        type: array\n" + "        items:\n" + "          type: string";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    OpenAPI swagger = result.getOpenAPI();
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    Parameter param = resolved.getPaths().get("/test").getGet().getParameters().get(0);
    QueryParameter qp = (QueryParameter) param;
// assertEquals(qp.getCollectionFormat(), "csv");
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 20 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testArrayRemoteModelProperty.

private void testArrayRemoteModelProperty(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.components(new Components().addSchemas("Sample", new Schema().addProperties("remoteRef", new ArraySchema().items(new Schema().$ref(remoteRef)))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final Schema prop = (Schema) resolved.getComponents().getSchemas().get("Sample").getProperties().get("remoteRef");
    assertTrue(prop instanceof ArraySchema);
    final ArraySchema ap = (ArraySchema) prop;
    assertEquals(ap.getItems().get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : Components(io.swagger.v3.oas.models.Components) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Aggregations

OpenAPIResolver (io.swagger.v3.parser.OpenAPIResolver)21 OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Test (org.testng.annotations.Test)16 StringSchema (io.swagger.v3.oas.models.media.StringSchema)15 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)14 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)13 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)13 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)13 Schema (io.swagger.v3.oas.models.media.Schema)13 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)10 Operation (io.swagger.v3.oas.models.Operation)8 PathItem (io.swagger.v3.oas.models.PathItem)8 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)8 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)8 Components (io.swagger.v3.oas.models.Components)7 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)7 Parameter (io.swagger.v3.oas.models.parameters.Parameter)6 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)6 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5