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