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"));
}
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);
}
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);
}
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");
}
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"));
}
Aggregations