Search in sources :

Example 1 with OpenApiVersion

use of io.syndesis.server.api.generator.openapi.OpenApiVersion in project syndesis by syndesisio.

the class OpenApiModelParser method parse.

public static OpenApiModelInfo parse(final String specification, final APIValidationContext validationContext) {
    final OpenApiModelInfo.Builder resultBuilder = new OpenApiModelInfo.Builder();
    final String resolvedSpecification;
    try {
        resolvedSpecification = resolve(specification);
        resultBuilder.resolvedSpecification(resolvedSpecification);
    } catch (final Exception e) {
        LOG.debug("Unable to resolve OpenAPI document\n{}\n", specification, e);
        return resultBuilder.addError(new Violation.Builder().error("error").property("").message("Unable to resolve OpenAPI document from: " + ofNullable(specification).map(s -> StringUtils.abbreviate(s, 100)).orElse("")).build()).build();
    }
    final JsonNode tree;
    try {
        tree = JsonUtils.reader().readTree(resolvedSpecification);
    } catch (final IOException e) {
        return new OpenApiModelInfo.Builder().addError(new Violation.Builder().property("").error("unreadable-document").message("Unable to read OpenAPI document: " + e.getMessage()).build()).build();
    }
    OpenApiVersion openApiVersion = getOpenApiVersion(tree);
    if (openApiVersion == null) {
        return new OpenApiModelInfo.Builder().addError(new Violation.Builder().property("").error("unsupported-version").message(String.format("This document cannot be uploaded. " + "Provide an OpenAPI document (supported versions are %s).", OpenApiVersion.getSupportedVersions())).build()).build();
    }
    final Document parsed = Library.readDocumentFromJSONString(resolvedSpecification);
    if (!(parsed instanceof OasDocument)) {
        LOG.debug("Unable to read OpenAPI document\n{}\n", specification);
        return resultBuilder.addError(new Violation.Builder().error("error").property("").message("Unable to read OpenAPI document from: '" + StringUtils.abbreviate(resolvedSpecification, 100)).build()).build();
    }
    resultBuilder.model((OasDocument) parsed);
    if (validationContext != APIValidationContext.NONE) {
        try {
            validateJsonSchema(convertToJson(resolvedSpecification), resultBuilder, openApiVersion, parsed.getClass());
        } catch (IOException e) {
            return resultBuilder.addError(new Violation.Builder().error("error").property("").message("Unable to read OpenAPI document from: '" + StringUtils.abbreviate(resolvedSpecification, 100)).build()).build();
        }
        return applyValidationRules(validationContext, resultBuilder.build(), openApiVersion);
    }
    return resultBuilder.build();
}
Also used : Violation(io.syndesis.common.model.Violation) HttpURLConnection(java.net.HttpURLConnection) Violation(io.syndesis.common.model.Violation) OpenApiModelInfo(io.syndesis.server.api.generator.openapi.OpenApiModelInfo) Logger(org.slf4j.Logger) URL(java.net.URL) Library(io.apicurio.datamodels.Library) Optional.ofNullable(java.util.Optional.ofNullable) Oas30ValidationRules(io.syndesis.server.api.generator.openapi.v3.Oas30ValidationRules) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) Oas30SchemaValidator(io.syndesis.server.api.generator.openapi.v3.Oas30SchemaValidator) OpenApiVersion(io.syndesis.server.api.generator.openapi.OpenApiVersion) Yaml(org.yaml.snakeyaml.Yaml) IOStreams(io.syndesis.common.util.IOStreams) JsonUtils(io.syndesis.common.util.json.JsonUtils) Locale(java.util.Locale) JsonNode(com.fasterxml.jackson.databind.JsonNode) OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) Document(io.apicurio.datamodels.core.models.Document) APIValidationContext(io.syndesis.server.api.generator.APIValidationContext) Oas20SchemaValidator(io.syndesis.server.api.generator.openapi.v2.Oas20SchemaValidator) Oas20ValidationRules(io.syndesis.server.api.generator.openapi.v2.Oas20ValidationRules) OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) OpenApiModelInfo(io.syndesis.server.api.generator.openapi.OpenApiModelInfo) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) Document(io.apicurio.datamodels.core.models.Document) IOException(java.io.IOException) OpenApiVersion(io.syndesis.server.api.generator.openapi.OpenApiVersion)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Library (io.apicurio.datamodels.Library)1 Document (io.apicurio.datamodels.core.models.Document)1 OasDocument (io.apicurio.datamodels.openapi.models.OasDocument)1 Violation (io.syndesis.common.model.Violation)1 IOStreams (io.syndesis.common.util.IOStreams)1 JsonUtils (io.syndesis.common.util.json.JsonUtils)1 APIValidationContext (io.syndesis.server.api.generator.APIValidationContext)1 OpenApiModelInfo (io.syndesis.server.api.generator.openapi.OpenApiModelInfo)1 OpenApiVersion (io.syndesis.server.api.generator.openapi.OpenApiVersion)1 Oas20SchemaValidator (io.syndesis.server.api.generator.openapi.v2.Oas20SchemaValidator)1 Oas20ValidationRules (io.syndesis.server.api.generator.openapi.v2.Oas20ValidationRules)1 Oas30SchemaValidator (io.syndesis.server.api.generator.openapi.v3.Oas30SchemaValidator)1 Oas30ValidationRules (io.syndesis.server.api.generator.openapi.v3.Oas30ValidationRules)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Locale (java.util.Locale)1 Optional.ofNullable (java.util.Optional.ofNullable)1 StringUtils (org.apache.commons.lang3.StringUtils)1