use of io.swagger.models.resourcelisting.ResourceListing in project swagger-parser by swagger-api.
the class ResourceListingConverterTest method convertResourceListingWithRootPath.
@Test
public void convertResourceListingWithRootPath() throws Exception {
ResourceListing rl = new ResourceListing();
rl.setApiVersion("2.11");
List<ApiDeclaration> apis = new ArrayList<ApiDeclaration>();
ApiDeclaration api = new ApiDeclaration();
api.setBasePath("http://foo.com");
apis.add(api);
Swagger swagger = converter.convert(rl, apis);
assertTrue(swagger.getSchemes().size() == 1);
assertTrue(swagger.getSwagger().equals("2.0"));
Info info = swagger.getInfo();
assertNotNull(info);
assertEquals(info.getVersion(), rl.getApiVersion());
assertEquals(swagger.getBasePath(), "/");
assertEquals(swagger.getHost(), "foo.com");
}
use of io.swagger.models.resourcelisting.ResourceListing in project swagger-parser by swagger-api.
the class ResourceListingReader method main.
public static void main(String[] args) throws IOException, URISyntaxException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
String baseUrl = "http://petstore.swagger.io/api/api-docs";
ResourceListing resourceListing = objectMapper.readValue(new URL(baseUrl), ResourceListing.class);
Map<String, ApiDeclaration> apiDeclarations = new HashMap<>();
List<ApiListingReference> apis = resourceListing.getApis();
if (apis != null) {
for (ApiListingReference api : apis) {
URL apiUrl;
URI uri = new URI(api.getPath());
if (uri.isAbsolute()) {
apiUrl = uri.toURL();
} else {
apiUrl = new URL(baseUrl + api.getPath());
}
apiDeclarations.put(api.getPath(), objectMapper.readValue(apiUrl, ApiDeclaration.class));
}
}
System.out.println("---=== Resource Listing (" + baseUrl + ") ==--");
System.out.println(resourceListing);
for (Map.Entry<String, ApiDeclaration> apiDeclarationEntry : apiDeclarations.entrySet()) {
System.out.println("---=== API Declaration (" + apiDeclarationEntry.getKey() + ") ==--");
System.out.println(apiDeclarationEntry.getValue());
}
}
use of io.swagger.models.resourcelisting.ResourceListing in project swagger-parser by swagger-api.
the class SwaggerCompatConverter method read.
@Override
public Swagger read(String input, List<AuthorizationValue> auths) throws IOException {
Swagger output = null;
MessageBuilder migrationMessages = new MessageBuilder();
SwaggerLegacyParser swaggerParser = new SwaggerLegacyParser();
ResourceListing resourceListing = null;
resourceListing = readResourceListing(input, migrationMessages, auths);
List<ApiDeclaration> apis = new ArrayList<ApiDeclaration>();
if (resourceListing != null) {
List<ApiListingReference> refs = resourceListing.getApis();
boolean readAsSingleFile = false;
if (refs != null) {
for (ApiListingReference ref : refs) {
ApiDeclaration apiDeclaration = null;
JsonNode node = ref.getExtraFields();
JsonNode operations = node.get("operations");
if (operations != null) {
if (!readAsSingleFile) {
// this is a single-file swagger definition
apiDeclaration = readDeclaration(input, migrationMessages, auths);
// avoid doing this again
readAsSingleFile = true;
}
} else {
String location = null;
if (input.startsWith("http")) {
// look up as url
String pathLocation = ref.getPath();
if (pathLocation.startsWith("http")) {
// use as absolute url
location = pathLocation;
} else {
if (pathLocation.startsWith("/")) {
// handle 1.1 specs
if (resourceListing.getSwaggerVersion().equals(SwaggerVersion.V1_1) && resourceListing.getExtraFields().get("basePath") != null) {
String basePath = resourceListing.getExtraFields().get("basePath").textValue();
location = basePath + pathLocation;
} else {
location = input + pathLocation;
}
} else {
location = input + "/" + pathLocation;
}
}
} else {
// file system
File fileLocation = new File(input);
if (ref.getPath().startsWith("/")) {
location = fileLocation.getParent() + ref.getPath();
} else {
location = fileLocation.getParent() + File.separator + ref.getPath();
}
}
if (location.indexOf(".{format}") != -1) {
location = location.replaceAll("\\.\\{format\\}", ".json");
}
apiDeclaration = readDeclaration(location, migrationMessages, auths);
}
if (apiDeclaration != null) {
apis.add(apiDeclaration);
}
}
}
output = convert(resourceListing, apis);
}
return output;
}
use of io.swagger.models.resourcelisting.ResourceListing in project swagger-parser by swagger-api.
the class ResourceListingConverterTest method convertResourceListingWithSubPath.
@Test
public void convertResourceListingWithSubPath() throws Exception {
ResourceListing rl = new ResourceListing();
rl.setApiVersion("2.11");
List<ApiDeclaration> apis = new ArrayList<ApiDeclaration>();
ApiDeclaration api = new ApiDeclaration();
api.setBasePath("https://foo.com/baz/bar");
apis.add(api);
Swagger swagger = converter.convert(rl, apis);
assertTrue(swagger.getSchemes().size() == 1);
assertTrue(swagger.getSchemes().get(0).equals(Scheme.HTTPS));
assertTrue(swagger.getSwagger().equals("2.0"));
Info info = swagger.getInfo();
assertNotNull(info);
assertEquals(info.getVersion(), rl.getApiVersion());
assertEquals(swagger.getBasePath(), "/baz/bar");
assertEquals(swagger.getHost(), "foo.com");
}
use of io.swagger.models.resourcelisting.ResourceListing in project swagger-parser by swagger-api.
the class ResourceListingExtractorTest method readResourceListing.
@Test
public void readResourceListing() throws Exception {
String resourceListingUri = "http://petstore.swagger.io/api/api-docs";
ObjectMapper mapper = JacksonUtils.newMapper();
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
File resourceListingFile = new File("src/test/resources/specs/v1_2/petstore/api-docs");
ResourceListing resourceListing = mapper.readValue(resourceListingFile, ResourceListing.class);
List<ApiListingReference> apis = resourceListing.getApis();
for (ApiListingReference ref : apis) {
String path = ref.getPath();
System.out.println("found path " + path);
}
}
Aggregations