use of io.apicurio.datamodels.asyncapi.models.AaiSchema in project apicurio-data-models by Apicurio.
the class AaiSchemaFactory method createSchemaDefinitionFromExample.
/**
* Creates a new definition schema from a given example. This method will analyze the example
* object and create a new schema object that represents the example. Note that this method
* does not support arbitrarily complicated examples, and should be used as a starting point
* for a schema, not a canonical one.
* @param document
* @param name
* @param example
*/
public static IDefinition createSchemaDefinitionFromExample(AaiDocument document, String name, Object example) {
AaiSchema schema;
if (document.getDocumentType() == DocumentType.asyncapi2) {
Aai20Document doc = (Aai20Document) document;
if (NodeCompat.isNullOrUndefined(doc.components)) {
doc.components = doc.createComponents();
}
schema = ((Aai20Components) doc.components).createSchemaDefinition(name);
} else {
throw new RuntimeException("Only AsyncAPI 2 is currently supported.");
}
if (JsonCompat.isString(example)) {
example = JsonCompat.parseJSON(JsonCompat.toString(example));
}
resolveAll(example, schema);
schema.title = "Root Type for " + name;
schema.description = "The root of the " + name + " type's schema.";
return (IDefinition) schema;
}
use of io.apicurio.datamodels.asyncapi.models.AaiSchema in project apicurio-data-models by Apicurio.
the class SetItemsTypeVisitor method setItems.
private void setItems(AaiSchema node) {
AaiSchema schema = (AaiSchema) node;
schema.items = schema.createItemsSchema();
if (ModelUtils.isDefined(this.type.of)) {
// TODO Handle the case where "items" is actually a List of schemas.
AaiSchema items = (AaiSchema) schema.items;
if (this.type.of.isRef()) {
items.$ref = this.type.of.type;
} else {
items.type = this.type.of.type;
items.format = this.type.of.as;
}
}
}
use of io.apicurio.datamodels.asyncapi.models.AaiSchema in project apicurio-data-models by Apicurio.
the class NewSchemaPropertyCommand_Aai20 method undo.
/**
* @see io.apicurio.datamodels.cmd.ICommand#undo(Document)
*/
@Override
public void undo(Document document) {
LoggerCompat.info("[NewSchemaPropertyCommand_Aai20] Reverting.");
if (!this._created) {
return;
}
AaiSchema schema = (AaiSchema) this._schemaPath.resolve(document);
if (this.isNullOrUndefined(schema)) {
return;
}
if (this.isNullOrUndefined(schema.getProperty(this._propertyName))) {
return;
}
schema.removeProperty(this._propertyName);
// if the property was marked as required - need to remove it from the parent's "required" array
if (ModelUtils.isDefined(this._newType) && this._newType.required == Boolean.TRUE) {
List<String> required = schema.required;
required.remove(required.indexOf(this._propertyName));
}
}
use of io.apicurio.datamodels.asyncapi.models.AaiSchema in project apicurio-data-models by Apicurio.
the class NewSchemaPropertyCommand_Aai20 method execute.
/**
* @see io.apicurio.datamodels.cmd.ICommand#execute(Document)
*/
@Override
public void execute(Document document) {
LoggerCompat.info("[NewSchemaPropertyCommand_Aai20] Executing.");
this._created = false;
AaiSchema schema = (AaiSchema) this._schemaPath.resolve(document);
if (this.isNullOrUndefined(schema)) {
LoggerCompat.info("[NewSchemaPropertyCommand_Aai20] Schema is null.");
return;
}
if (ModelUtils.isDefined(schema.getProperty(this._propertyName))) {
LoggerCompat.info("[NewSchemaPropertyCommand_Aai20] Property already exists.");
return;
}
AaiSchema property = (AaiSchema) schema.createPropertySchema(this._propertyName);
if (ModelUtils.isDefined(this._description)) {
property.description = this._description;
}
if (ModelUtils.isDefined(this._newType)) {
this._setPropertyType((IPropertySchema) property);
}
schema.addProperty(this._propertyName, property);
LoggerCompat.info("[NewSchemaPropertyCommand_Aai20] Property [%s] created successfully.", this._propertyName);
this._created = true;
}
use of io.apicurio.datamodels.asyncapi.models.AaiSchema in project apicurio-data-models by Apicurio.
the class NewSchemaPropertyCommand_Aai20 method _setPropertyType.
/**
* Sets the property type.
* @param prop
*/
protected void _setPropertyType(IPropertySchema prop) {
// Update the schema's type
SimplifiedTypeUtil.setSimplifiedType((AaiSchema) prop, this._newType);
if (ModelUtils.isDefined(this._newType) && this._newType.required == Boolean.TRUE) {
AaiSchema parent = (AaiSchema) ((Node) prop).parent();
List<String> required = parent.required;
if (this.isNullOrUndefined(required)) {
required = new ArrayList<>();
NodeCompat.setProperty(parent, Constants.PROP_REQUIRED, required);
this._nullRequired = true;
}
required.add(prop.getPropertyName());
}
}
Aggregations