Search in sources :

Example 1 with ResourceListing

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");
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) ResourceListing(io.swagger.models.resourcelisting.ResourceListing) Swagger(io.swagger.models.Swagger) ArrayList(java.util.ArrayList) Info(io.swagger.models.Info) Test(org.testng.annotations.Test)

Example 2 with ResourceListing

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());
    }
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) ResourceListing(io.swagger.models.resourcelisting.ResourceListing) HashMap(java.util.HashMap) ApiListingReference(io.swagger.models.resourcelisting.ApiListingReference) URI(java.net.URI) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL)

Example 3 with ResourceListing

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;
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiListingReference(io.swagger.models.resourcelisting.ApiListingReference) ResourceListing(io.swagger.models.resourcelisting.ResourceListing) MessageBuilder(io.swagger.report.MessageBuilder) Swagger(io.swagger.models.Swagger) File(java.io.File)

Example 4 with ResourceListing

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");
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) ResourceListing(io.swagger.models.resourcelisting.ResourceListing) Swagger(io.swagger.models.Swagger) ArrayList(java.util.ArrayList) Info(io.swagger.models.Info) Test(org.testng.annotations.Test)

Example 5 with ResourceListing

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);
    }
}
Also used : ResourceListing(io.swagger.models.resourcelisting.ResourceListing) ApiListingReference(io.swagger.models.resourcelisting.ApiListingReference) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Aggregations

ResourceListing (io.swagger.models.resourcelisting.ResourceListing)6 ApiDeclaration (io.swagger.models.apideclaration.ApiDeclaration)4 Swagger (io.swagger.models.Swagger)3 ApiListingReference (io.swagger.models.resourcelisting.ApiListingReference)3 ArrayList (java.util.ArrayList)3 Test (org.testng.annotations.Test)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Info (io.swagger.models.Info)2 File (java.io.File)2 MessageBuilder (io.swagger.report.MessageBuilder)1 ResourceListingMigrator (io.swagger.transform.migrate.ResourceListingMigrator)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1