Search in sources :

Example 1 with APIValidationContext

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

the class OpenApiGenerator method info.

@Override
public APISummary info(final String specification, final APIValidationContext validation) {
    final OpenApiModelInfo modelInfo = OpenApiModelParser.parse(specification, validation);
    final OasDocument model = modelInfo.getModel();
    if (model == null) {
        return new APISummary.Builder().errors(modelInfo.getErrors()).warnings(modelInfo.getWarnings()).build();
    }
    final OasPaths paths = model.paths;
    final ActionsSummary actionsSummary = determineSummaryFrom(OasModelHelper.getPathItems(paths));
    final Info info = model.info;
    final String title = Optional.ofNullable(info).map(i -> i.title).orElse("unspecified");
    final String description = Optional.ofNullable(info).map(i -> i.description).orElse("unspecified");
    return // 
    new APISummary.Builder().name(// 
    title).description(// 
    description).actionsSummary(// 
    actionsSummary).errors(// 
    modelInfo.getErrors()).warnings(// 
    modelInfo.getWarnings()).putConfiguredProperty("specification", modelInfo.getResolvedSpecification()).build();
}
Also used : OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) OpenApi(io.syndesis.common.model.openapi.OpenApi) Action(io.syndesis.common.model.action.Action) KeyGenerator(io.syndesis.common.util.KeyGenerator) Step(io.syndesis.common.model.integration.Step) URL(java.net.URL) ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) Kind(io.syndesis.common.model.Kind) LoggerFactory(org.slf4j.LoggerFactory) Oas20FlowGenerator(io.syndesis.server.api.generator.openapi.v2.Oas20FlowGenerator) HashMap(java.util.HashMap) ConfigurationProperty(io.syndesis.common.model.connection.ConfigurationProperty) OasPathItem(io.apicurio.datamodels.openapi.models.OasPathItem) Function(java.util.function.Function) Info(io.apicurio.datamodels.core.models.common.Info) OpenApiModelParser(io.syndesis.server.api.generator.openapi.util.OpenApiModelParser) APIIntegration(io.syndesis.server.api.generator.APIIntegration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) FlowMetadata(io.syndesis.common.model.integration.FlowMetadata) OasModelHelper(io.syndesis.server.api.generator.openapi.util.OasModelHelper) Integration(io.syndesis.common.model.integration.Integration) APIValidationContext(io.syndesis.server.api.generator.APIValidationContext) APISummary(io.syndesis.common.model.api.APISummary) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Library(io.apicurio.datamodels.Library) Oas30FlowGenerator(io.syndesis.server.api.generator.openapi.v3.Oas30FlowGenerator) OasPaths(io.apicurio.datamodels.openapi.models.OasPaths) Collectors(java.util.stream.Collectors) Flow(io.syndesis.common.model.integration.Flow) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) Optional(java.util.Optional) APIGenerator(io.syndesis.server.api.generator.APIGenerator) OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) ActionsSummary(io.syndesis.common.model.action.ActionsSummary) APISummary(io.syndesis.common.model.api.APISummary) OasPaths(io.apicurio.datamodels.openapi.models.OasPaths) ActionsSummary(io.syndesis.common.model.action.ActionsSummary) Info(io.apicurio.datamodels.core.models.common.Info)

Example 2 with APIValidationContext

use of io.syndesis.server.api.generator.APIValidationContext 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

Library (io.apicurio.datamodels.Library)2 OasDocument (io.apicurio.datamodels.openapi.models.OasDocument)2 APIValidationContext (io.syndesis.server.api.generator.APIValidationContext)2 URL (java.net.URL)2 Locale (java.util.Locale)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Document (io.apicurio.datamodels.core.models.Document)1 Info (io.apicurio.datamodels.core.models.common.Info)1 OasPathItem (io.apicurio.datamodels.openapi.models.OasPathItem)1 OasPaths (io.apicurio.datamodels.openapi.models.OasPaths)1 Kind (io.syndesis.common.model.Kind)1 ResourceIdentifier (io.syndesis.common.model.ResourceIdentifier)1 Violation (io.syndesis.common.model.Violation)1 Action (io.syndesis.common.model.action.Action)1 ActionsSummary (io.syndesis.common.model.action.ActionsSummary)1 APISummary (io.syndesis.common.model.api.APISummary)1 ConfigurationProperty (io.syndesis.common.model.connection.ConfigurationProperty)1 Flow (io.syndesis.common.model.integration.Flow)1