Search in sources :

Example 1 with IPropertySchema

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

the class ChangePropertyTypeCommand method execute.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void execute(Document document) {
    LoggerCompat.info("[ChangePropertyTypeCommand] Executing: " + this._newType);
    IPropertySchema prop = (IPropertySchema) this._propPath.resolve(document);
    if (this.isNullOrUndefined(prop)) {
        return;
    }
    IPropertyParent parent = (IPropertyParent) ((Node) prop).parent();
    List<String> required = parent.getRequiredProperties();
    // Save the old info (for later undo operation)
    this._oldProperty = Library.writeNode((Node) prop);
    this._oldRequired = ModelUtils.isDefined(required) && required.size() > 0 && required.indexOf(prop.getPropertyName()) != -1;
    // Update the schema's type
    SimplifiedTypeUtil.setSimplifiedType((Schema) prop, this._newType);
    if (!this.isNullOrUndefined(this._newType.required)) {
        // Going from optional to required
        if (Boolean.TRUE.equals(this._newType.required) && !this._oldRequired) {
            if (this.isNullOrUndefined(required)) {
                required = new ArrayList<>();
                parent.setRequiredProperties(required);
                this._nullRequired = true;
            }
            required.add(prop.getPropertyName());
        }
        // Going from required to optional - remove property name from required list.
        if (Boolean.FALSE.equals(this._newType.required) && this._oldRequired) {
            required.remove(required.indexOf(prop.getPropertyName()));
        }
    }
}
Also used : IPropertyParent(io.apicurio.datamodels.core.models.common.IPropertyParent) Node(io.apicurio.datamodels.core.models.Node) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema)

Example 2 with IPropertySchema

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

the class RenamePropertyCommand method _doPropertyRename.

/**
 * Does the work of renaming a path from one name to another.
 * @param document
 * @param from
 * @param to
 * @private
 */
private void _doPropertyRename(Document document, String from, String to) {
    OasSchema parent = (OasSchema) this._parentPath.resolve(document);
    if (this.isNullOrUndefined(parent)) {
        return;
    }
    // Don't do anything if the "to" property already exists.
    if (!this.isNullOrUndefined(parent.getProperty(to))) {
        return;
    }
    OasSchema property = (OasSchema) parent.removeProperty(from);
    if (this.isNullOrUndefined(property)) {
        return;
    }
    ((IPropertySchema) property).rename(to);
    parent.addProperty(to, property);
    List<String> required = parent.required;
    int reqIdx = ModelUtils.isDefined(required) ? required.indexOf(from) : -1;
    if (reqIdx != -1) {
        parent.required.set(reqIdx, to);
    }
}
Also used : OasSchema(io.apicurio.datamodels.openapi.models.OasSchema) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema)

Example 3 with IPropertySchema

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

the class ChangePropertyTypeCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[ChangePropertyTypeCommand] Reverting.");
    IPropertySchema prop = (IPropertySchema) this._propPath.resolve(document);
    if (this.isNullOrUndefined(prop)) {
        return;
    }
    IPropertyParent parent = (IPropertyParent) ((Node) prop).parent();
    List<String> required = parent.getRequiredProperties();
    boolean wasRequired = ModelUtils.isDefined(required) && required.size() > 0 && required.indexOf(prop.getPropertyName()) != -1;
    Schema oldProp = parent.createPropertySchema(this._propName);
    Library.readNode(this._oldProperty, oldProp);
    // Restore the schema attributes
    restoreSchemaInternalProperties((Schema) prop, oldProp);
    // Restore the "required" flag
    if (!this.isNullOrUndefined(this._newType.required)) {
        if (this._nullRequired) {
            parent.setRequiredProperties(null);
        } else {
            // Restoring optional from required
            if (wasRequired && !this._oldRequired) {
                required.remove(required.indexOf(prop.getPropertyName()));
            }
            // Restoring required from optional
            if (!wasRequired && this._oldRequired) {
                required.add(prop.getPropertyName());
            }
        }
    }
}
Also used : IPropertyParent(io.apicurio.datamodels.core.models.common.IPropertyParent) Schema(io.apicurio.datamodels.core.models.common.Schema) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema)

Example 4 with IPropertySchema

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

the class DeletePropertyCommand method execute.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void execute(Document document) {
    LoggerCompat.info("[DeletePropertyCommand] Executing.");
    this._oldProperty = null;
    IPropertySchema property = (IPropertySchema) this._propertyPath.resolve(document);
    if (this.isNullOrUndefined(property)) {
        return;
    }
    IPropertyParent schema = (IPropertyParent) ((Node) property).parent();
    this._oldProperty = Library.writeNode(schema.removeProperty(this._propertyName));
    this._oldRequired = schema.isPropertyRequired(this._propertyName);
    if (this._oldRequired) {
        schema.unsetPropertyRequired(this._propertyName);
    }
}
Also used : IPropertyParent(io.apicurio.datamodels.core.models.common.IPropertyParent) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema)

Example 5 with IPropertySchema

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

the class SimplifiedTypeTest method testFromPropertySchema.

@Test
public void testFromPropertySchema() {
    Oas20Document doc = (Oas20Document) Library.readDocumentFromJSONString(OPENAPI_20_DATA);
    Oas20SchemaDefinition definition = doc.definitions.getDefinition("Examples");
    // Array
    SimplifiedPropertyType propType = SimplifiedPropertyType.fromPropertySchema((IPropertySchema) definition.getProperty("array-property"));
    assertSimplifiedType(propType, false, true, false, false, false, "array", null, null);
    Assert.assertFalse("#required", propType.required);
    // Boolean
    propType = SimplifiedPropertyType.fromPropertySchema((IPropertySchema) definition.getProperty("boolean-property"));
    assertSimplifiedType(propType, false, false, false, false, true, "boolean", null, null);
    Assert.assertTrue("#required", propType.required);
    // Enum
    propType = SimplifiedPropertyType.fromPropertySchema((IPropertySchema) definition.getProperty("enum-property"));
    List<Object> vals = new ArrayList<>(3);
    vals.add("val1");
    vals.add("val2");
    vals.add("val3");
    assertSimplifiedType(propType, false, false, true, false, true, "string", null, vals);
    Assert.assertFalse("#required", propType.required);
    // Number
    propType = SimplifiedPropertyType.fromPropertySchema((IPropertySchema) definition.getProperty("number-property"));
    assertSimplifiedType(propType, false, false, false, false, true, "number", null, null);
    Assert.assertTrue("#required", propType.required);
    // Ref
    propType = SimplifiedPropertyType.fromPropertySchema((IPropertySchema) definition.getProperty("ref-property"));
    assertSimplifiedType(propType, true, false, false, false, false, "#/definitions/ExampleObject", null, null);
    Assert.assertFalse("#required", propType.required);
    // String
    propType = SimplifiedPropertyType.fromPropertySchema((IPropertySchema) definition.getProperty("string-property"));
    assertSimplifiedType(propType, false, false, false, false, true, "string", null, null);
    Assert.assertFalse("#required", propType.required);
}
Also used : Oas20Document(io.apicurio.datamodels.openapi.v2.models.Oas20Document) ArrayList(java.util.ArrayList) Oas20SchemaDefinition(io.apicurio.datamodels.openapi.v2.models.Oas20SchemaDefinition) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema) Test(org.junit.Test)

Aggregations

IPropertySchema (io.apicurio.datamodels.core.models.common.IPropertySchema)5 IPropertyParent (io.apicurio.datamodels.core.models.common.IPropertyParent)3 Node (io.apicurio.datamodels.core.models.Node)1 Schema (io.apicurio.datamodels.core.models.common.Schema)1 OasSchema (io.apicurio.datamodels.openapi.models.OasSchema)1 Oas20Document (io.apicurio.datamodels.openapi.v2.models.Oas20Document)1 Oas20SchemaDefinition (io.apicurio.datamodels.openapi.v2.models.Oas20SchemaDefinition)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1