use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class InlineModelResolver method modelFromProperty.
@SuppressWarnings("static-method")
public Schema modelFromProperty(Schema object, @SuppressWarnings("unused") String path) {
String description = object.getDescription();
String example = null;
Object obj = object.getExample();
if (obj != null) {
example = obj.toString();
}
ArraySchema model = new ArraySchema();
model.setDescription(description);
model.setExample(example);
if (object.getAdditionalProperties() != null && !(object.getAdditionalProperties() instanceof Boolean)) {
model.setItems((Schema) object.getAdditionalProperties());
}
return model;
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class InlineModelResolver method flattenDefinitions.
private void flattenDefinitions(Map<String, Schema> models) {
if (models == null) {
return;
}
List<String> modelNames = new ArrayList<String>(models.keySet());
for (String modelName : modelNames) {
Schema model = models.get(modelName);
if (model.getProperties() != null) {
Map<String, Schema> properties = model.getProperties();
flattenProperties(properties, modelName);
fixStringModel(model);
} else if (model instanceof ArraySchema) {
ArraySchema m = (ArraySchema) model;
Schema inner = m.getItems();
if (isObjectSchema(inner)) {
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
String innerModelName = resolveModelName(inner.getTitle(), modelName + "_inner");
String existing = matchGenerated(inner);
if (existing == null) {
openAPI.getComponents().addSchemas(innerModelName, inner);
addGenerated(innerModelName, inner);
m.setItems(new Schema().$ref(innerModelName));
} else {
m.setItems(new Schema().$ref(existing));
}
} else if (inner instanceof ComposedSchema && this.flattenComposedSchemas) {
flattenComposedSchema(inner, modelName);
if (inner.get$ref() == null) {
modelName = resolveModelName(inner.getTitle(), "inline_array_items_" + modelName);
m.setItems(this.makeRefProperty(modelName, inner));
addGenerated(modelName, inner);
openAPI.getComponents().addSchemas(modelName, inner);
}
}
}
} else if (model instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) model;
String inlineModelName = "";
List<Schema> list = null;
if (composedSchema.getAllOf() != null) {
list = composedSchema.getAllOf();
inlineModelName = "AllOf";
} else if (composedSchema.getAnyOf() != null) {
list = composedSchema.getAnyOf();
inlineModelName = "AnyOf";
} else if (composedSchema.getOneOf() != null) {
list = composedSchema.getOneOf();
inlineModelName = "OneOf";
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i).get$ref() == null) {
Schema inline = list.get(i);
if (inline.getProperties() != null) {
flattenProperties(inline.getProperties(), modelName);
}
if (this.flattenComposedSchemas) {
int position = i + 1;
inlineModelName = resolveModelName(inline.getTitle(), modelName + inlineModelName + "_" + position);
list.set(i, new Schema().$ref(inlineModelName));
addGenerated(inlineModelName, inline);
openAPI.getComponents().addSchemas(inlineModelName, inline);
}
}
}
}
}
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class InlineModelResolver method flattenParams.
private void flattenParams(String pathname, List<Parameter> parameters) {
if (parameters == null) {
return;
}
for (Parameter parameter : parameters) {
if (parameter.getSchema() != null) {
Schema model = parameter.getSchema();
if (model.getProperties() != null) {
if (model.getType() == null || "object".equals(model.getType())) {
if (model.getProperties() != null && model.getProperties().size() > 0) {
flattenProperties(model.getProperties(), pathname);
String modelName = resolveModelName(model.getTitle(), parameter.getName());
parameter.setSchema(new Schema().$ref(modelName));
addGenerated(modelName, model);
openAPI.getComponents().addSchemas(modelName, model);
}
}
} else if (model instanceof ComposedSchema) {
String modelName = resolveModelName(model.getTitle(), parameter.getName());
parameter.setSchema(new Schema().$ref(modelName));
addGenerated(modelName, model);
openAPI.getComponents().addSchemas(modelName, model);
} else if (model instanceof ArraySchema) {
ArraySchema am = (ArraySchema) model;
Schema inner = am.getItems();
if (isObjectSchema(inner)) {
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
flattenProperties(inner.getProperties(), pathname);
String modelName = resolveModelName(inner.getTitle(), parameter.getName());
String existing = matchGenerated(inner);
if (existing != null) {
am.setItems(new Schema().$ref(existing));
} else {
am.setItems(new Schema().$ref(modelName));
addGenerated(modelName, am);
openAPI.getComponents().addSchemas(modelName, am);
}
} else if (inner instanceof ComposedSchema && this.flattenComposedSchemas) {
flattenComposedSchema(inner, parameter.getName());
if (inner.get$ref() == null) {
String modelName = resolveModelName(inner.getTitle(), "inline_parameter_items_" + parameter.getName());
am.setItems(this.makeRefProperty(modelName, inner));
addGenerated(modelName, inner);
openAPI.getComponents().addSchemas(modelName, inner);
}
}
}
}
}
}
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalSchema.
public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Schema schema = cache.loadRef($ref, refFormat, Schema.class);
if (schema == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn("unable to load model reference from `" + $ref + "`. It may not be available " + "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
if (schemas == null) {
schemas = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
newRef = finalNameRec(schemas, possiblyConflictingDefinitionName, schema, 0);
cache.putRenamedRef($ref, newRef);
Schema existingModel = schemas.get(newRef);
if (existingModel != null && existingModel.get$ref() != null) {
// use the new model
existingModel = null;
}
if (existingModel == null) {
// don't overwrite existing model reference
openAPI.getComponents().addSchemas(newRef, schema);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (schema.get$ref() != null) {
RefFormat ref = computeRefFormat(schema.get$ref());
if (isAnExternalRefFormat(ref)) {
if (!ref.equals(RefFormat.URL)) {
String schemaFullRef = schema.get$ref();
String parent = file.substring(0, file.lastIndexOf('/'));
if (!parent.isEmpty()) {
if (schemaFullRef.contains("#/")) {
String[] parts = schemaFullRef.split("#/");
String schemaFullRefFilePart = parts[0];
String schemaFullRefInternalRefPart = parts[1];
schemaFullRef = Paths.get(parent, schemaFullRefFilePart).normalize().toString() + "#/" + schemaFullRefInternalRefPart;
} else {
schemaFullRef = Paths.get(parent, schemaFullRef).normalize().toString();
}
}
schema.set$ref(processRefToExternalSchema(schemaFullRef, ref));
}
} else {
processRefToExternalSchema(file + schema.get$ref(), RefFormat.RELATIVE);
}
}
if (schema instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) schema;
if (composedSchema.getAllOf() != null) {
for (Schema item : composedSchema.getAllOf()) {
if (item.get$ref() != null) {
processRefSchema(item, file);
} else if (item.getProperties() != null) {
processProperties(item.getProperties(), file);
}
}
}
if (composedSchema.getOneOf() != null) {
for (Schema item : composedSchema.getOneOf()) {
if (item.get$ref() != null) {
if (item.get$ref() != null) {
processRefSchema(item, file);
}
}
}
}
if (composedSchema.getAnyOf() != null) {
for (Schema item : composedSchema.getAnyOf()) {
if (item.get$ref() != null) {
if (item.get$ref() != null) {
processRefSchema(item, file);
}
}
}
}
}
// Loop the properties and recursively call this method;
Map<String, Schema> subProps = schema.getProperties();
processProperties(subProps, file);
processDiscriminator(schema.getDiscriminator(), file);
if (schema.getAdditionalProperties() != null && schema.getAdditionalProperties() instanceof Schema) {
Schema additionalProperty = (Schema) schema.getAdditionalProperties();
if (additionalProperty.get$ref() != null) {
processRefSchema(additionalProperty, file);
} else if (additionalProperty instanceof ArraySchema) {
ArraySchema arrayProp = (ArraySchema) additionalProperty;
if (arrayProp.getItems() != null && arrayProp.getItems().get$ref() != null && StringUtils.isNotBlank(arrayProp.get$ref())) {
processRefSchema(arrayProp.getItems(), file);
}
} else if (additionalProperty.getAdditionalProperties() != null && additionalProperty.getAdditionalProperties() instanceof Schema) {
Schema mapProp = (Schema) additionalProperty.getAdditionalProperties();
if (mapProp.get$ref() != null) {
processRefSchema(mapProp, file);
} else if (mapProp.getAdditionalProperties() instanceof ArraySchema && ((ArraySchema) mapProp).getItems() != null && ((ArraySchema) mapProp).getItems().get$ref() != null && StringUtils.isNotBlank(((ArraySchema) mapProp).getItems().get$ref())) {
processRefSchema(((ArraySchema) mapProp).getItems(), file);
}
}
}
if (schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null) {
ArraySchema arraySchema = (ArraySchema) schema;
if (StringUtils.isNotBlank(arraySchema.getItems().get$ref())) {
processRefSchema(((ArraySchema) schema).getItems(), file);
} else {
processProperties(arraySchema.getItems().getProperties(), file);
}
}
}
return newRef;
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method propertyNameMixup.
@Test
public void propertyNameMixup() {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().read("simple.yaml", null, parseOptions);
assertEquals(((StringSchema) openAPI.getComponents().getSchemas().get("Manufacturer").getProperties().get("name")).getExample(), "ACME Corporation");
Schema schema = openAPI.getPaths().get("/inventory").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
assertEquals(((ObjectSchema) ((ArraySchema) schema).getItems().getProperties().get("manufacturer")).getProperties().get("name").getExample(), "ACME Corporation");
}
Aggregations