Search in sources :

Example 16 with Document

use of io.apicurio.datamodels.core.models.Document in project apiman by apiman.

the class OpenApiProvider method rewrite.

@Override
public String rewrite(ProviderContext providerCtx, InputStream is, ApiDefinitionType apiDefinitionType) throws IOException, StorageException, GatewayAuthenticationException {
    // Special case for Swagger/OAI 1.x, we don't do anything (yet)
    JsonNode root = om(apiDefinitionType).readTree(is);
    if (root.has("swaggerVersion")) {
        // If seems to be OAI/Swagger 1.x then just return the stringified version.
        if (StringUtils.startsWith(root.get("swaggerVersion").asText(), "1.")) {
            return om(apiDefinitionType).writeValueAsString(root);
        }
    }
    // All other versions
    Document parsedLib = Library.readDocument(root);
    DocumentType docType = parsedLib.getDocumentType();
    switch(docType) {
        case openapi2:
            OPEN_API_2.rewrite(providerCtx, parsedLib);
            break;
        case openapi3:
            OPEN_API_3.rewrite(providerCtx, parsedLib);
            break;
        case asyncapi2:
        default:
            LOGGER.warn("We don't know how to handle {0} (yet), returning schema without doing anything", docType);
    }
    Object writeOut = Library.writeNode(parsedLib);
    return om(apiDefinitionType).writeValueAsString(writeOut);
}
Also used : DocumentType(io.apicurio.datamodels.core.models.DocumentType) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(io.apicurio.datamodels.core.models.Document)

Example 17 with Document

use of io.apicurio.datamodels.core.models.Document in project apicurio-data-models by Apicurio.

the class Library method readDocument.

/**
 * Reads an entire document from JSON data.  The JSON data (already parsed, not in string format) is
 * read as a data model {@link Document} and returned.
 * @param json
 */
public static Document readDocument(Object json) {
    // Clone the input because the reader is destructive to the source data.
    Object clonedJson = JsonCompat.clone(json);
    DocumentType type = discoverDocumentType(clonedJson);
    DataModelReader reader = VisitorFactory.createDataModelReader(type);
    Document document = DocumentFactory.create(type);
    reader.readDocument(clonedJson, document);
    return document;
}
Also used : DocumentType(io.apicurio.datamodels.core.models.DocumentType) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) Oas30Document(io.apicurio.datamodels.openapi.v3.models.Oas30Document) Document(io.apicurio.datamodels.core.models.Document) Oas20Document(io.apicurio.datamodels.openapi.v2.models.Oas20Document) DataModelReader(io.apicurio.datamodels.core.io.DataModelReader)

Example 18 with Document

use of io.apicurio.datamodels.core.models.Document in project apicurio-data-models by Apicurio.

the class TestValidationExtension method doCreateAndClone.

private void doCreateAndClone(DocumentType type) {
    Document document = Library.createDocument(type);
    Assert.assertNotNull(document);
    Assert.assertEquals(type, document.getDocumentType());
    Document clone = Library.cloneDocument(document);
    Assert.assertNotNull(clone);
    Assert.assertEquals(type, clone.getDocumentType());
    Assert.assertEquals(document.getDocumentType(), clone.getDocumentType());
    Assert.assertFalse(document == clone);
}
Also used : Oas30Document(io.apicurio.datamodels.openapi.v3.models.Oas30Document) Document(io.apicurio.datamodels.core.models.Document)

Example 19 with Document

use of io.apicurio.datamodels.core.models.Document in project apicurio-data-models by Apicurio.

the class DereferenceTestRunner method runChild.

/**
 * @see org.junit.runners.ParentRunner#runChild(java.lang.Object, org.junit.runner.notification.RunNotifier)
 */
@Override
protected void runChild(DereferenceTestCase child, RunNotifier notifier) {
    Description description = this.describeChild(child);
    Statement statement = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            String testCP = "fixtures/dereference/" + child.getInput();
            URL testUrl = Thread.currentThread().getContextClassLoader().getResource(testCP);
            Assert.assertNotNull("Could not load test resource: " + testCP, testUrl);
            // Read the test source
            String original = loadResource(testUrl);
            Assert.assertNotNull(original);
            // Parse into a Json object
            Object originalParsed = mapper.readTree(original);
            // Read into a data model
            Document srcDoc = Library.readDocument(originalParsed);
            Assert.assertNotNull(srcDoc);
            Assert.assertNotNull(srcDoc.getDocumentType());
            // Dereference the document
            Document dereferencedDoc = Library.dereferenceDocument(srcDoc);
            Assert.assertNotNull(dereferencedDoc);
            Assert.assertNotSame(srcDoc, dereferencedDoc);
            // Now compare with expected
            String actual = Library.writeDocumentToJSONString(dereferencedDoc);
            String expectedCP = "fixtures/dereference/" + child.getExpected();
            URL expectedUrl = Thread.currentThread().getContextClassLoader().getResource(expectedCP);
            Assert.assertNotNull("Could not load test resource: " + expectedCP, expectedUrl);
            String expected = loadResource(expectedUrl);
            assertJsonEquals(expected, actual);
        }
    };
    runLeaf(statement, description, notifier);
}
Also used : Description(org.junit.runner.Description) Statement(org.junit.runners.model.Statement) Document(io.apicurio.datamodels.core.models.Document) URL(java.net.URL)

Example 20 with Document

use of io.apicurio.datamodels.core.models.Document 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

Document (io.apicurio.datamodels.core.models.Document)21 Node (io.apicurio.datamodels.core.models.Node)7 Oas30Document (io.apicurio.datamodels.openapi.v3.models.Oas30Document)7 URL (java.net.URL)7 Description (org.junit.runner.Description)6 Statement (org.junit.runners.model.Statement)6 DocumentType (io.apicurio.datamodels.core.models.DocumentType)5 NodePath (io.apicurio.datamodels.core.models.NodePath)4 ValidationProblem (io.apicurio.datamodels.core.models.ValidationProblem)4 OasDocument (io.apicurio.datamodels.openapi.models.OasDocument)4 Oas20Document (io.apicurio.datamodels.openapi.v2.models.Oas20Document)4 Test (org.junit.Test)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)2 DataModelReader (io.apicurio.datamodels.core.io.DataModelReader)2 ValidationProblemSeverity (io.apicurio.datamodels.core.models.ValidationProblemSeverity)2 IDocumentValidatorExtension (io.apicurio.datamodels.core.validation.IDocumentValidatorExtension)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2