Search in sources :

Example 1 with Schema

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

the class AaiDataModelReader method readSchema.

@Override
public void readSchema(Object json, Schema node) {
    AaiSchema schema = (AaiSchema) node;
    String format = JsonCompat.consumePropertyString(json, Constants.PROP_FORMAT);
    String title = JsonCompat.consumePropertyString(json, Constants.PROP_TITLE);
    String description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
    Object default_ = JsonCompat.consumePropertyObject(json, Constants.PROP_DEFAULT);
    Number multipleOf = JsonCompat.consumePropertyNumber(json, Constants.PROP_MULTIPLE_OF);
    Number maximum = JsonCompat.consumePropertyNumber(json, Constants.PROP_MAXIMUM);
    Boolean exclusiveMaximum = JsonCompat.consumePropertyBoolean(json, Constants.PROP_EXCLUSIVE_MAXIMUM);
    Number minimum = JsonCompat.consumePropertyNumber(json, Constants.PROP_MINIMUM);
    Boolean exclusiveMinimum = JsonCompat.consumePropertyBoolean(json, Constants.PROP_EXCLUSIVE_MINIMUM);
    Number maxLength = JsonCompat.consumePropertyNumber(json, Constants.PROP_MAX_LENGTH);
    Number minLength = JsonCompat.consumePropertyNumber(json, Constants.PROP_MIN_LENGTH);
    String pattern = JsonCompat.consumePropertyString(json, Constants.PROP_PATTERN);
    Number maxItems = JsonCompat.consumePropertyNumber(json, Constants.PROP_MAX_ITEMS);
    Number minItems = JsonCompat.consumePropertyNumber(json, Constants.PROP_MIN_ITEMS);
    Boolean uniqueItems = JsonCompat.consumePropertyBoolean(json, Constants.PROP_UNIQUE_ITEMS);
    Number maxProperties = JsonCompat.consumePropertyNumber(json, Constants.PROP_MAX_PROPERTIES);
    Number minProperties = JsonCompat.consumePropertyNumber(json, Constants.PROP_MIN_PROPERTIES);
    List<String> required = JsonCompat.consumePropertyStringArray(json, Constants.PROP_REQUIRED);
    List<Object> enum_ = JsonCompat.consumePropertyArray(json, Constants.PROP_ENUM);
    String type = JsonCompat.consumePropertyString(json, Constants.PROP_TYPE);
    Object items = JsonCompat.consumeProperty(json, Constants.PROP_ITEMS);
    List<Object> allOf = JsonCompat.consumePropertyArray(json, Constants.PROP_ALL_OF);
    List<Object> oneOf = JsonCompat.consumePropertyArray(json, Constants.PROP_ONE_OF);
    List<Object> anyOf = JsonCompat.consumePropertyArray(json, Constants.PROP_ANY_OF);
    Object not = JsonCompat.consumeProperty(json, Constants.PROP_NOT);
    Object properties = JsonCompat.consumeProperty(json, Constants.PROP_PROPERTIES);
    Object additionalProperties = JsonCompat.consumeProperty(json, Constants.PROP_ADDITIONAL_PROPERTIES);
    Boolean readOnly = JsonCompat.consumePropertyBoolean(json, Constants.PROP_READ_ONLY);
    Boolean writeOnly = JsonCompat.consumePropertyBoolean(json, Constants.PROP_WRITE_ONLY);
    String discriminator = JsonCompat.consumePropertyString(json, Constants.PROP_DISCRIMINATOR);
    Boolean deprecated = JsonCompat.consumePropertyBoolean(json, Constants.PROP_DEPRECATED);
    schema.format = format;
    schema.title = title;
    schema.description = description;
    schema.default_ = default_;
    schema.multipleOf = multipleOf;
    schema.maximum = maximum;
    schema.exclusiveMaximum = exclusiveMaximum;
    schema.minimum = minimum;
    schema.exclusiveMinimum = exclusiveMinimum;
    schema.maxLength = maxLength;
    schema.minLength = minLength;
    schema.pattern = pattern;
    schema.maxItems = maxItems;
    schema.minItems = minItems;
    schema.uniqueItems = uniqueItems;
    schema.maxProperties = maxProperties;
    schema.minProperties = minProperties;
    schema.required = required;
    schema.enum_ = enum_;
    schema.type = type;
    schema.readOnly = readOnly;
    schema.writeOnly = writeOnly;
    schema.discriminator = discriminator;
    schema.deprecated = deprecated;
    if (items != null) {
        if (JsonCompat.isArray(items)) {
            List<AaiSchema> schemaModels = new ArrayList<>();
            List<Object> itemList = JsonCompat.toList(items);
            for (Object item : itemList) {
                AaiSchema itemsSchemaModel = schema.createItemsSchema();
                this.readSchema(item, itemsSchemaModel);
                schemaModels.add(itemsSchemaModel);
            }
            schema.items = schemaModels;
        } else {
            schema.items = schema.createItemsSchema();
            this.readSchema(items, (AaiSchema) schema.items);
        }
    }
    if (allOf != null) {
        List<AaiSchema> schemaModels = new ArrayList<>();
        for (Object allOfSchema : allOf) {
            AaiSchema allOfSchemaModel = schema.createAllOfSchema();
            this.readSchema(allOfSchema, allOfSchemaModel);
            schemaModels.add(allOfSchemaModel);
        }
        schema.allOf = schemaModels;
    }
    if (oneOf != null) {
        oneOf.forEach(oneOfSchema -> {
            AaiSchema oneOfSchemaModel = schema.createOneOfSchema();
            this.readSchema(oneOfSchema, oneOfSchemaModel);
            schema.addOneOfSchema(oneOfSchemaModel);
        });
    }
    if (anyOf != null) {
        anyOf.forEach(anyOfSchema -> {
            AaiSchema anyOfSchemaModel = schema.createAnyOfSchema();
            this.readSchema(anyOfSchema, anyOfSchemaModel);
            schema.addAnyOfSchema(anyOfSchemaModel);
        });
    }
    if (not != null) {
        schema.not = schema.createNotSchema();
        this.readSchema(not, schema.not);
    }
    if (properties != null) {
        List<String> propertyNames = JsonCompat.keys(properties);
        for (String propertyName : propertyNames) {
            Object propertySchema = JsonCompat.consumeProperty(properties, propertyName);
            Schema propertySchemaModel = schema.createPropertySchema(propertyName);
            this.readSchema(propertySchema, propertySchemaModel);
            schema.addProperty(propertyName, propertySchemaModel);
        }
    }
    if (additionalProperties != null) {
        if (JsonCompat.isBoolean(additionalProperties)) {
            schema.additionalProperties = JsonCompat.toBoolean(additionalProperties);
        } else {
            AaiSchema additionalPropertiesModel = schema.createAdditionalPropertiesSchema();
            this.readSchema(additionalProperties, additionalPropertiesModel);
            schema.additionalProperties = additionalPropertiesModel;
        }
    }
    super.readSchema(json, schema);
}
Also used : AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema) Schema(io.apicurio.datamodels.core.models.common.Schema) ArrayList(java.util.ArrayList) AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema)

Example 2 with Schema

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

the class Oas20Parameter method createSchema.

/**
 * @see io.apicurio.datamodels.core.models.common.Parameter#createSchema()
 */
@Override
public Schema createSchema() {
    Schema rval = new Oas20Schema();
    rval._ownerDocument = this.ownerDocument();
    rval._parent = this;
    return rval;
}
Also used : Schema(io.apicurio.datamodels.core.models.common.Schema)

Example 3 with Schema

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

the class RenameSchemaDefinitionCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[RenameSchemaDefinitionCommand] Reverting.");
    if (this._renameSchemaDefinition(document, this._newName, this._oldName)) {
        String oldRef = this._nameToReference(this._oldName);
        if (ModelUtils.isDefined(this._references)) {
            this._references.forEach(ref -> {
                Schema schema = (Schema) ref.resolve(document);
                schema.$ref = oldRef;
            });
        }
        if (ModelUtils.isDefined(this._messageReferences)) {
            this._messageReferences.forEach(ref -> {
                AaiMessage aaiMessage = (AaiMessage) ref.resolve(document);
                JsonCompat.setPropertyString(aaiMessage.payload, "$ref", oldRef);
            });
        }
    }
}
Also used : Schema(io.apicurio.datamodels.core.models.common.Schema) Oas30OneOfSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30OneOfSchema) Oas30NotSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30NotSchema) AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema) OasSchema(io.apicurio.datamodels.openapi.models.OasSchema) Oas30AnyOfSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30AnyOfSchema) AaiMessage(io.apicurio.datamodels.asyncapi.models.AaiMessage)

Example 4 with Schema

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

the class RenameSchemaDefinitionCommand method execute.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void execute(Document document) {
    LoggerCompat.info("[RenameSchemaDefinitionCommand] Executing.");
    this._references = new ArrayList<>();
    this._messageReferences = new ArrayList<>();
    if (this._renameSchemaDefinition(document, this._oldName, this._newName)) {
        String oldRef = this._nameToReference(this._oldName);
        String newRef = this._nameToReference(this._newName);
        SchemaRefFinder schemaFinder = new SchemaRefFinder(oldRef);
        SchemaRefFinder.ReferenceHolder referenceHolder = schemaFinder.findIn(document);
        for (Schema schema : referenceHolder.getSchemas()) {
            this._references.add(Library.createNodePath(schema));
            schema.$ref = newRef;
        }
        for (AaiMessage aaiMessage : referenceHolder.getMessages()) {
            this._messageReferences.add(Library.createNodePath(aaiMessage));
            JsonCompat.setPropertyString(aaiMessage.payload, "$ref", newRef);
        }
    }
}
Also used : Schema(io.apicurio.datamodels.core.models.common.Schema) Oas30OneOfSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30OneOfSchema) Oas30NotSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30NotSchema) AaiSchema(io.apicurio.datamodels.asyncapi.models.AaiSchema) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema) OasSchema(io.apicurio.datamodels.openapi.models.OasSchema) Oas30AnyOfSchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30AnyOfSchema) AaiMessage(io.apicurio.datamodels.asyncapi.models.AaiMessage)

Example 5 with Schema

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

the class DataModelWriter method visitSchemaDefinition.

/**
 * @see io.apicurio.datamodels.core.visitors.IVisitor#visitSchemaDefinition(io.apicurio.datamodels.core.models.common.IDefinition)
 */
@Override
public void visitSchemaDefinition(IDefinition node) {
    Schema schema = (Schema) node;
    Object parent = this.lookupParentJson(schema);
    Object json = JsonCompat.objectNode();
    writeSchema(json, schema);
    writeExtraProperties(json, schema);
    addSchemaDefinitionToParent(parent, json, node);
    this.updateIndex(schema, json);
}
Also used : Schema(io.apicurio.datamodels.core.models.common.Schema)

Aggregations

Schema (io.apicurio.datamodels.core.models.common.Schema)12 IPropertySchema (io.apicurio.datamodels.core.models.common.IPropertySchema)5 OasSchema (io.apicurio.datamodels.openapi.models.OasSchema)4 AaiSchema (io.apicurio.datamodels.asyncapi.models.AaiSchema)3 IPropertyParent (io.apicurio.datamodels.core.models.common.IPropertyParent)3 ArrayList (java.util.ArrayList)3 AaiMessage (io.apicurio.datamodels.asyncapi.models.AaiMessage)2 Library (io.apicurio.datamodels.Library)1 JsonCompat (io.apicurio.datamodels.compat.JsonCompat)1 NodeCompat (io.apicurio.datamodels.compat.NodeCompat)1 Constants (io.apicurio.datamodels.core.Constants)1 Document (io.apicurio.datamodels.core.models.Document)1 DocumentType (io.apicurio.datamodels.core.models.DocumentType)1 ExtensibleNode (io.apicurio.datamodels.core.models.ExtensibleNode)1 Extension (io.apicurio.datamodels.core.models.Extension)1 IReferenceNode (io.apicurio.datamodels.core.models.IReferenceNode)1 Node (io.apicurio.datamodels.core.models.Node)1 NodePath (io.apicurio.datamodels.core.models.NodePath)1 ValidationProblem (io.apicurio.datamodels.core.models.ValidationProblem)1 Contact (io.apicurio.datamodels.core.models.common.Contact)1