use of io.apicurio.datamodels.core.models.common.Schema in project apicurio-data-models by Apicurio.
the class OasDataModelReader method readSchema.
/**
* @see io.apicurio.datamodels.core.io.DataModelReader#readSchema(java.lang.Object, io.apicurio.datamodels.core.models.common.Schema)
*/
@Override
public void readSchema(Object json, Schema node) {
OasSchema schema = (OasSchema) 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<String> enum_ = JsonCompat.consumePropertyStringArray(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);
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);
Object xml = JsonCompat.consumeProperty(json, Constants.PROP_XML);
Object externalDocs = JsonCompat.consumeProperty(json, Constants.PROP_EXTERNAL_DOCS);
Object example = JsonCompat.consumePropertyObject(json, Constants.PROP_EXAMPLE);
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.example = example;
if (items != null) {
if (JsonCompat.isArray(items)) {
List<OasSchema> schemaModels = new ArrayList<>();
List<Object> itemList = JsonCompat.toList(items);
for (Object item : itemList) {
OasSchema itemsSchemaModel = schema.createItemsSchema();
this.readSchema(item, itemsSchemaModel);
schemaModels.add(itemsSchemaModel);
}
schema.items = schemaModels;
} else {
schema.items = schema.createItemsSchema();
this.readSchema(items, (OasSchema) schema.items);
}
}
if (allOf != null) {
List<OasSchema> schemaModels = new ArrayList<>();
for (Object allOfSchema : allOf) {
OasSchema allOfSchemaModel = schema.createAllOfSchema();
this.readSchema(allOfSchema, allOfSchemaModel);
schemaModels.add(allOfSchemaModel);
}
schema.allOf = schemaModels;
}
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 {
OasSchema additionalPropertiesModel = schema.createAdditionalPropertiesSchema();
this.readSchema(additionalProperties, additionalPropertiesModel);
schema.additionalProperties = additionalPropertiesModel;
}
}
if (xml != null) {
schema.xml = schema.createXML();
this.readXML(xml, schema.xml);
}
if (externalDocs != null) {
schema.externalDocs = schema.createExternalDocumentation();
this.readExternalDocumentation(externalDocs, schema.externalDocs);
}
super.readSchema(json, schema);
}
use of io.apicurio.datamodels.core.models.common.Schema in project apicurio-data-models by Apicurio.
the class Oas20to30TransformationVisitor method visitParameter.
/**
* @see io.apicurio.datamodels.core.visitors.IVisitor#visitParameter(io.apicurio.datamodels.core.models.common.Parameter)
*/
@Override
public void visitParameter(Parameter node) {
Oas20Parameter param20 = (Oas20Parameter) node;
if (NodeCompat.equals(param20.in, "body")) {
Oas30Operation operation30 = (Oas30Operation) this.lookup(this.findParentOperation(param20));
if (!NodeCompat.isNullOrUndefined(operation30)) {
Oas30RequestBody body30 = (Oas30RequestBody) operation30.createRequestBody();
operation30.requestBody = body30;
body30.description = param20.description;
body30.required = param20.required;
if (!NodeCompat.isNullOrUndefined(param20.schema)) {
List<String> consumes = this.findConsumes(param20);
Oas20Schema schema = (Oas20Schema) param20.schema;
consumes.forEach(ct -> {
Oas30MediaType mediaType30 = body30.createMediaType(ct);
body30.addMediaType(ct, mediaType30);
Oas30Schema schema30 = mediaType30.createSchema();
mediaType30.schema = this.toSchema(schema, schema30, true);
this.mapNode(schema, schema30);
});
}
}
} else if (NodeCompat.equals(param20.in, "formData")) {
Oas30Operation operation30 = (Oas30Operation) this.lookup(this.findParentOperation(param20));
if (!NodeCompat.isNullOrUndefined(operation30)) {
List<String> consumes = this.findConsumes(param20);
if (!this.hasFormDataMimeType(consumes)) {
consumes = NodeCompat.asList("application/x-www-form-urlencoded");
}
consumes.forEach(ct -> {
if (this.isFormDataMimeType(ct)) {
Oas30RequestBody body30 = operation30.requestBody;
if (NodeCompat.isNullOrUndefined(body30)) {
body30 = operation30.createRequestBody();
operation30.requestBody = body30;
body30.required = true;
}
Oas30MediaType mediaType30 = body30.getMediaType(ct);
if (NodeCompat.isNullOrUndefined(mediaType30)) {
mediaType30 = body30.createMediaType(ct);
body30.addMediaType(ct, mediaType30);
}
Oas30Schema schema30 = mediaType30.schema;
if (NodeCompat.isNullOrUndefined(schema30)) {
schema30 = mediaType30.createSchema();
mediaType30.schema = schema30;
schema30.type = "object";
}
Oas30PropertySchema property30 = (Oas30PropertySchema) schema30.createPropertySchema(param20.name);
schema30.addProperty(param20.name, property30);
property30.description = param20.description;
this.toSchema(param20, property30, false);
this.mapNode(param20, schema30);
}
});
}
} else {
if (this.isRef(param20)) {
Oas20ParameterDefinition paramDef = (Oas20ParameterDefinition) ReferenceUtil.resolveRef(param20.$ref, param20);
// Request Bodies. So a reference to a "body" param should become a reference to a request body.
if (!NodeCompat.isNullOrUndefined(paramDef) && NodeCompat.equals(paramDef.in, "body")) {
Oas30Operation parent30 = (Oas30Operation) this.lookup(this.findParentOperation(param20));
if (!NodeCompat.isNullOrUndefined(parent30)) {
Oas30RequestBody body30 = parent30.createRequestBody();
parent30.requestBody = body30;
body30.$ref = "#/components/requestBodies/" + paramDef.getName();
this.mapNode(param20, body30);
return;
}
}
// treat the param as though it is inlined (which will result in a requestBody model).
if (!NodeCompat.isNullOrUndefined(paramDef) && NodeCompat.equals(paramDef.in, "formData")) {
// Inline the parameter definition and then re-visit it.
Library.readNode(Library.writeNode(paramDef), param20);
param20.$ref = null;
this.visitParameter(param20);
return;
}
}
// Now we have normal handling of a parameter, examples include path params, query params, header params, etc.
IOasParameterParent parent30 = (IOasParameterParent) this.lookup(param20.parent());
Oas30Parameter param30 = (Oas30Parameter) parent30.createParameter();
parent30.addParameter(param30);
this.transformParam(param20, param30);
this.mapNode(param20, param30);
}
}
Aggregations