Search in sources :

Example 1 with ApiDeclarationMigrator

use of io.swagger.transform.migrate.ApiDeclarationMigrator 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)

Example 2 with ApiDeclarationMigrator

use of io.swagger.transform.migrate.ApiDeclarationMigrator in project swagger-parser by swagger-api.

the class SwaggerCompatConverter method readDeclaration.

public ApiDeclaration readDeclaration(String input, MessageBuilder messages, List<AuthorizationValue> auths) {
    ApiDeclaration output = null;
    try {
        JsonNode jsonNode = null;
        if (input.startsWith("http")) {
            String json = RemoteUrl.urlToString(input, auths);
            jsonNode = Json.mapper().readTree(json);
        } else {
            final String fileScheme = "file:";
            java.nio.file.Path path;
            if (input.toLowerCase().startsWith(fileScheme)) {
                path = Paths.get(URI.create(input));
            } else {
                path = Paths.get(input);
            }
            String json;
            if (Files.exists(path)) {
                json = FileUtils.readFileToString(path.toFile(), "UTF-8");
            } else {
                json = ClasspathHelper.loadFileFromClasspath(input);
            }
            jsonNode = Json.mapper().readTree(json);
        }
        // this should be moved to a json patch
        if (jsonNode.isObject()) {
            ((ObjectNode) jsonNode).remove("authorizations");
        }
        ApiDeclarationMigrator migrator = new ApiDeclarationMigrator();
        JsonNode transformed = migrator.migrate(messages, jsonNode);
        output = Json.mapper().convertValue(transformed, ApiDeclaration.class);
    } catch (java.lang.IllegalArgumentException e) {
        return null;
    } catch (Exception e) {
        LOGGER.error("failed to read api declaration", e);
    }
    return output;
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ApiDeclarationMigrator(io.swagger.transform.migrate.ApiDeclarationMigrator) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ApiDeclaration (io.swagger.models.apideclaration.ApiDeclaration)2 ApiDeclarationMigrator (io.swagger.transform.migrate.ApiDeclarationMigrator)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ApiDeclarationDeserializer (io.swagger.deserializer.ApiDeclarationDeserializer)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 MessageBuilder (io.swagger.report.MessageBuilder)1 ApiDeclarationJsonValidator (io.swagger.validate.ApiDeclarationJsonValidator)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1