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;
}
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);
}
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;
}
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;
}
Aggregations