Search in sources :

Example 1 with IDefinition

use of io.apicurio.datamodels.core.models.common.IDefinition 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 2 with IDefinition

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

the class Oas20IReferenceManipulationStrategy method removeComponent.

@Override
public boolean removeComponent(Document document, String name) {
    if (document.getDocumentType() != DocumentType.openapi2) {
        throw new IllegalArgumentException("Oas20Document expected.");
    }
    Oas20Document model = (Oas20Document) document;
    IDefinition removed = model.definitions.items.remove(name);
    if (removed != null)
        return true;
    removed = model.parameters.items.remove(name);
    if (removed != null)
        return true;
    return model.responses.items.remove(name) != null;
}
Also used : Oas20Document(io.apicurio.datamodels.openapi.v2.models.Oas20Document) IDefinition(io.apicurio.datamodels.core.models.common.IDefinition)

Example 3 with IDefinition

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

the class Oas30IReferenceManipulationStrategy method removeComponent.

@Override
public boolean removeComponent(Document document, String name) {
    if (document.getDocumentType() != DocumentType.openapi3) {
        throw new IllegalArgumentException("Oas30Document expected.");
    }
    Oas30Document model = (Oas30Document) document;
    IDefinition removed = model.components.schemas.remove(name);
    if (removed != null)
        return true;
    removed = model.components.responses.remove(name);
    if (removed != null)
        return true;
    removed = model.components.parameters.remove(name);
    if (removed != null)
        return true;
    removed = model.components.examples.remove(name);
    if (removed != null)
        return true;
    removed = model.components.requestBodies.remove(name);
    if (removed != null)
        return true;
    removed = model.components.headers.remove(name);
    if (removed != null)
        return true;
    removed = model.components.securitySchemes.remove(name);
    if (removed != null)
        return true;
    removed = model.components.links.remove(name);
    if (removed != null)
        return true;
    return model.components.callbacks.remove(name) != null;
}
Also used : Oas30Document(io.apicurio.datamodels.openapi.v3.models.Oas30Document) IDefinition(io.apicurio.datamodels.core.models.common.IDefinition)

Example 4 with IDefinition

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

the class OasSchemaFactory 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(OasDocument document, String name, Object example) {
    OasSchema schema = null;
    if (document.getDocumentType() == DocumentType.openapi2) {
        Oas20Document doc20 = (Oas20Document) document;
        if (NodeCompat.isNullOrUndefined(doc20.definitions)) {
            doc20.definitions = doc20.createDefinitions();
        }
        schema = doc20.definitions.createSchemaDefinition(name);
    } else if (document.getDocumentType() == DocumentType.openapi3) {
        Oas30Document doc30 = (Oas30Document) document;
        if (NodeCompat.isNullOrUndefined(doc30.components)) {
            doc30.components = doc30.createComponents();
        }
        schema = doc30.components.createSchemaDefinition(name);
    } else {
        throw new RuntimeException("Only OpenAPI 2 and 3 are 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 : OasSchema(io.apicurio.datamodels.openapi.models.OasSchema) Oas30Document(io.apicurio.datamodels.openapi.v3.models.Oas30Document) Oas20Document(io.apicurio.datamodels.openapi.v2.models.Oas20Document) IDefinition(io.apicurio.datamodels.core.models.common.IDefinition)

Aggregations

IDefinition (io.apicurio.datamodels.core.models.common.IDefinition)4 Oas20Document (io.apicurio.datamodels.openapi.v2.models.Oas20Document)2 Oas30Document (io.apicurio.datamodels.openapi.v3.models.Oas30Document)2 AaiSchema (io.apicurio.datamodels.asyncapi.models.AaiSchema)1 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)1 OasSchema (io.apicurio.datamodels.openapi.models.OasSchema)1