Search in sources :

Example 6 with AaiSchema

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;
}
Also used : Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema) IDefinition(io.apicurio.datamodels.core.models.common.IDefinition)

Example 7 with AaiSchema

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;
        }
    }
}
Also used : AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema)

Example 8 with AaiSchema

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));
    }
}
Also used : AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema)

Example 9 with AaiSchema

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;
}
Also used : AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema)

Example 10 with AaiSchema

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());
    }
}
Also used : AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema)

Aggregations

AaiSchema (io.apicurio.datamodels.asyncapi.models.AaiSchema)13 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)2 Node (io.apicurio.datamodels.core.models.Node)2 AaiChannelBindingsDefinition (io.apicurio.datamodels.asyncapi.models.AaiChannelBindingsDefinition)1 AaiComponents (io.apicurio.datamodels.asyncapi.models.AaiComponents)1 AaiCorrelationId (io.apicurio.datamodels.asyncapi.models.AaiCorrelationId)1 AaiMessage (io.apicurio.datamodels.asyncapi.models.AaiMessage)1 AaiMessageBindingsDefinition (io.apicurio.datamodels.asyncapi.models.AaiMessageBindingsDefinition)1 AaiMessageTraitDefinition (io.apicurio.datamodels.asyncapi.models.AaiMessageTraitDefinition)1 AaiOperationBindingsDefinition (io.apicurio.datamodels.asyncapi.models.AaiOperationBindingsDefinition)1 AaiOperationTraitDefinition (io.apicurio.datamodels.asyncapi.models.AaiOperationTraitDefinition)1 AaiParameter (io.apicurio.datamodels.asyncapi.models.AaiParameter)1 AaiSecurityScheme (io.apicurio.datamodels.asyncapi.models.AaiSecurityScheme)1 AaiServerBindingsDefinition (io.apicurio.datamodels.asyncapi.models.AaiServerBindingsDefinition)1 Aai20NodeFactory (io.apicurio.datamodels.asyncapi.v2.models.Aai20NodeFactory)1 IDefinition (io.apicurio.datamodels.core.models.common.IDefinition)1 Schema (io.apicurio.datamodels.core.models.common.Schema)1 ArrayList (java.util.ArrayList)1