Search in sources :

Example 1 with DocumentType

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

the class Library method readNode.

/**
 * Call this to do a "partial read" on a given node.  You must pass the JSON data for the node
 * type and an empty instance of the property node class.  For example, you could read just an
 * Operation by passing the JSON data for the operation along with an instance of e.g.
 * {@link Oas30Operation} and this will read the data and store it in the instance.
 * @param json
 * @param node
 */
public static Node readNode(Object json, Node node) {
    // Clone the input because the reader is destructive to the source data.
    Object clonedJson = JsonCompat.clone(json);
    DocumentType type = node.ownerDocument().getDocumentType();
    DataModelReader reader = VisitorFactory.createDataModelReader(type);
    DataModelReaderDispatcher dispatcher = VisitorFactory.createDataModelReaderDispatcher(type, clonedJson, reader);
    Library.visitNode(node, dispatcher);
    return node;
}
Also used : DocumentType(io.apicurio.datamodels.core.models.DocumentType) DataModelReaderDispatcher(io.apicurio.datamodels.core.io.DataModelReaderDispatcher) DataModelReader(io.apicurio.datamodels.core.io.DataModelReader)

Example 2 with DocumentType

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

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

the class ValidationRuleSet method getRulesFor.

/**
 * Gets the actual rule instances (visitors) that should be applied to the given document.
 * @param document
 */
public List<ValidationRule> getRulesFor(Document document) {
    List<ValidationRule> rval = new ArrayList<>();
    DocumentType type = document.getDocumentType();
    for (ValidationRuleMetaData rule : this.rules) {
        if (rule.appliesTo(type)) {
            ValidationRule rc = ValidationCompat.instantiate(rule);
            rval.add(rc);
        }
    }
    return rval;
}
Also used : ArrayList(java.util.ArrayList) DocumentType(io.apicurio.datamodels.core.models.DocumentType) OasBodyParameterUniquenessValidationRule(io.apicurio.datamodels.core.validation.rules.other.OasBodyParameterUniquenessValidationRule) OasParameterUniquenessValidationRule(io.apicurio.datamodels.core.validation.rules.other.OasParameterUniquenessValidationRule) SecurityRequirementUniquenessValidationRule(io.apicurio.datamodels.core.validation.rules.other.SecurityRequirementUniquenessValidationRule) OasOperationIdUniquenessValidationRule(io.apicurio.datamodels.core.validation.rules.other.OasOperationIdUniquenessValidationRule) TagUniquenessValidationRule(io.apicurio.datamodels.core.validation.rules.other.TagUniquenessValidationRule)

Example 4 with DocumentType

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

Aggregations

DocumentType (io.apicurio.datamodels.core.models.DocumentType)4 DataModelReader (io.apicurio.datamodels.core.io.DataModelReader)2 Document (io.apicurio.datamodels.core.models.Document)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)1 DataModelReaderDispatcher (io.apicurio.datamodels.core.io.DataModelReaderDispatcher)1 OasBodyParameterUniquenessValidationRule (io.apicurio.datamodels.core.validation.rules.other.OasBodyParameterUniquenessValidationRule)1 OasOperationIdUniquenessValidationRule (io.apicurio.datamodels.core.validation.rules.other.OasOperationIdUniquenessValidationRule)1 OasParameterUniquenessValidationRule (io.apicurio.datamodels.core.validation.rules.other.OasParameterUniquenessValidationRule)1 SecurityRequirementUniquenessValidationRule (io.apicurio.datamodels.core.validation.rules.other.SecurityRequirementUniquenessValidationRule)1 TagUniquenessValidationRule (io.apicurio.datamodels.core.validation.rules.other.TagUniquenessValidationRule)1 Oas20Document (io.apicurio.datamodels.openapi.v2.models.Oas20Document)1 Oas30Document (io.apicurio.datamodels.openapi.v3.models.Oas30Document)1 ArrayList (java.util.ArrayList)1