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