Search in sources :

Example 1 with ApiListingReference

use of io.swagger.models.resourcelisting.ApiListingReference 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 2 with ApiListingReference

use of io.swagger.models.resourcelisting.ApiListingReference 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 3 with ApiListingReference

use of io.swagger.models.resourcelisting.ApiListingReference 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

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