Search in sources :

Example 1 with MessageBuilder

use of io.swagger.report.MessageBuilder 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 2 with MessageBuilder

use of io.swagger.report.MessageBuilder in project swagger-parser by swagger-api.

the class SwaggerLegacyParser method read.

public ApiDeclaration read(String url, String resourcePath, Authentication authentication) {
    MessageBuilder messageBuilder = new MessageBuilder();
    SwaggerReader swaggerReader = new SwaggerReaderFactory(new SwaggerReaderConfiguration()).newReader();
    JsonNode jsonNode = null;
    try {
        String resourceListingURL = getResourceListingURL(url, resourcePath);
        jsonNode = swaggerReader.read(resourceListingURL, authentication, messageBuilder);
    } catch (URISyntaxException e) {
        messageBuilder.append(new Message("", e.getMessage(), Severity.ERROR));
    }
    validateMessageReport(messageBuilder);
    ApiDeclarationMigrator apiDeclarationMigrator = new ApiDeclarationMigrator();
    jsonNode = apiDeclarationMigrator.migrate(messageBuilder, jsonNode);
    validateMessageReport(messageBuilder);
    ApiDeclarationJsonValidator apiDeclarationJsonValidator = new ApiDeclarationJsonValidator();
    apiDeclarationJsonValidator.validate(messageBuilder, jsonNode);
    validateMessageReport(messageBuilder);
    ApiDeclarationDeserializer apiDeclarationDeserializer = new ApiDeclarationDeserializer();
    ApiDeclaration apiDeclaration = apiDeclarationDeserializer.deserialize(jsonNode, messageBuilder);
    validateMessageReport(messageBuilder);
    return apiDeclaration;
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) SwaggerReaderFactory(io.swagger.reader.SwaggerReaderFactory) ApiDeclarationJsonValidator(io.swagger.validate.ApiDeclarationJsonValidator) MessageBuilder(io.swagger.report.MessageBuilder) Message(io.swagger.report.Message) ApiDeclarationDeserializer(io.swagger.deserializer.ApiDeclarationDeserializer) SwaggerReaderConfiguration(io.swagger.reader.SwaggerReaderConfiguration) ApiDeclarationMigrator(io.swagger.transform.migrate.ApiDeclarationMigrator) JsonNode(com.fasterxml.jackson.databind.JsonNode) URISyntaxException(java.net.URISyntaxException) SwaggerReader(io.swagger.reader.SwaggerReader)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ApiDeclaration (io.swagger.models.apideclaration.ApiDeclaration)2 MessageBuilder (io.swagger.report.MessageBuilder)2 ApiDeclarationDeserializer (io.swagger.deserializer.ApiDeclarationDeserializer)1 Swagger (io.swagger.models.Swagger)1 ApiListingReference (io.swagger.models.resourcelisting.ApiListingReference)1 ResourceListing (io.swagger.models.resourcelisting.ResourceListing)1 SwaggerReader (io.swagger.reader.SwaggerReader)1 SwaggerReaderConfiguration (io.swagger.reader.SwaggerReaderConfiguration)1 SwaggerReaderFactory (io.swagger.reader.SwaggerReaderFactory)1 Message (io.swagger.report.Message)1 ApiDeclarationMigrator (io.swagger.transform.migrate.ApiDeclarationMigrator)1 ApiDeclarationJsonValidator (io.swagger.validate.ApiDeclarationJsonValidator)1 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1