use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processSchema.
private void processSchema(Schema property, String file) {
if (property != null) {
if (StringUtils.isNotBlank(property.get$ref())) {
processRefSchema(property, file);
}
if (property.getProperties() != null) {
processProperties(property.getProperties(), file);
}
if (property instanceof ArraySchema) {
processSchema(((ArraySchema) property).getItems(), file);
}
if (property.getAdditionalProperties() instanceof Schema) {
processSchema(((Schema) property.getAdditionalProperties()), file);
}
if (property instanceof ComposedSchema) {
ComposedSchema composed = (ComposedSchema) property;
processProperties(composed.getAllOf(), file);
processProperties(composed.getAnyOf(), file);
processProperties(composed.getOneOf(), file);
}
}
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class SchemaProcessorTest method testProcessArrayProperty_ItemsIsRefProperty.
@Test
public void testProcessArrayProperty_ItemsIsRefProperty() throws Exception {
expectCreationOfExternalRefProcessor();
final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
final Schema refProperty = new Schema().$ref(ref);
ArraySchema arrayProperty = new ArraySchema();
arrayProperty.setItems(refProperty);
expectCallToExternalRefProcessor(ref, RefFormat.URL, "bar");
new SchemaProcessor(cache, openAPI).processSchema(arrayProperty);
new FullVerifications() {
{
}
};
assertEquals((arrayProperty.getItems()).get$ref(), "#/components/schemas/bar");
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class InlineModelResolver method flattenResponses.
private void flattenResponses(String pathname, Map<String, ApiResponse> responses) {
if (responses == null) {
return;
}
for (String key : responses.keySet()) {
ApiResponse response = responses.get(key);
if (response.getContent() != null) {
Map<String, MediaType> content = response.getContent();
for (String name : content.keySet()) {
if (content.get(name) != null) {
MediaType media = content.get(name);
if (media.getSchema() != null) {
Schema mediaSchema = media.getSchema();
if (isObjectSchema(mediaSchema)) {
if (mediaSchema.getProperties() != null && mediaSchema.getProperties().size() > 0 || mediaSchema instanceof ComposedSchema) {
String modelName = resolveModelName(mediaSchema.getTitle(), "inline_response_" + key);
String existing = matchGenerated(mediaSchema);
if (existing != null) {
media.setSchema(this.makeRefProperty(existing, mediaSchema));
} else {
media.setSchema(this.makeRefProperty(modelName, mediaSchema));
addGenerated(modelName, mediaSchema);
openAPI.getComponents().addSchemas(modelName, mediaSchema);
}
} else if (mediaSchema.getAdditionalProperties() != null && !(mediaSchema.getAdditionalProperties() instanceof Boolean)) {
Schema innerProperty = (Schema) mediaSchema.getAdditionalProperties();
if (isObjectSchema(innerProperty)) {
key = "inline_response_map" + key;
flattenMapSchema(innerProperty, key, pathname, mediaSchema);
} else if (innerProperty instanceof ArraySchema) {
ArraySchema arraySchema = (ArraySchema) innerProperty;
Schema inner = arraySchema.getItems();
if (isObjectSchema(inner)) {
key = "inline_response_map_items" + key;
flattenMapSchema(inner, key, pathname, mediaSchema);
}
}
}
} else if (mediaSchema instanceof ArraySchema) {
ArraySchema ap = (ArraySchema) mediaSchema;
Schema inner = ap.getItems();
if (isObjectSchema(inner)) {
flattenArraySchema(inner, key, pathname, ap);
}
} else if (mediaSchema.getAdditionalProperties() != null && !(mediaSchema.getAdditionalProperties() instanceof Boolean)) {
Schema innerProperty = (Schema) mediaSchema.getAdditionalProperties();
if (isObjectSchema(innerProperty)) {
key = "inline_response_map" + key;
flattenMapSchema(innerProperty, key, pathname, mediaSchema);
}
}
}
}
}
}
}
}
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(ArraySchema object, @SuppressWarnings("unused") String path) {
String description = object.getDescription();
String example = null;
Object obj = object.getExample();
if (obj != null) {
example = obj.toString();
}
Schema inner = object.getItems();
if (inner instanceof ObjectSchema) {
ArraySchema model = new ArraySchema();
model.setDescription(description);
model.setExample(example);
model.setItems(object.getItems());
return model;
}
return null;
}
use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.
the class InlineModelResolver method flattenProperties.
public void flattenProperties(Map<String, Schema> properties, String path) {
if (properties == null) {
return;
}
Map<String, Schema> propsToUpdate = new HashMap<>();
Map<String, Schema> modelsToAdd = new HashMap<>();
for (String key : properties.keySet()) {
Schema property = properties.get(key);
if (isObjectSchema(property) && property.getProperties() != null && property.getProperties().size() > 0) {
String modelName = resolveModelName(property.getTitle(), path + "_" + key);
Schema model = createModelFromProperty(property, modelName);
String existing = matchGenerated(model);
if (existing != null) {
propsToUpdate.put(key, new Schema().$ref(existing));
} else {
propsToUpdate.put(key, new Schema().$ref(RefType.SCHEMAS.getInternalPrefix() + modelName));
modelsToAdd.put(modelName, model);
addGenerated(modelName, model);
openAPI.getComponents().addSchemas(modelName, model);
}
} else if (property instanceof ArraySchema) {
ArraySchema ap = (ArraySchema) property;
Schema inner = ap.getItems();
if (isObjectSchema(inner)) {
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
flattenProperties(inner.getProperties(), path);
String modelName = resolveModelName(inner.getTitle(), path + "_" + key);
Schema innerModel = createModelFromProperty(inner, modelName);
String existing = matchGenerated(innerModel);
if (existing != null) {
ap.setItems(new Schema().$ref(existing));
} else {
ap.setItems(new Schema().$ref(modelName));
addGenerated(modelName, innerModel);
openAPI.getComponents().addSchemas(modelName, innerModel);
}
} else if (inner instanceof ComposedSchema && this.flattenComposedSchemas) {
flattenComposedSchema(inner, key);
String modelName = resolveModelName(inner.getTitle(), path + "_" + key);
Schema innerModel = createModelFromProperty(inner, modelName);
String existing = matchGenerated(innerModel);
if (existing != null) {
ap.setItems(new Schema().$ref(existing));
} else {
ap.setItems(new Schema().$ref(modelName));
addGenerated(modelName, innerModel);
openAPI.getComponents().addSchemas(modelName, innerModel);
}
}
}
} else if (property.getAdditionalProperties() != null && !(property.getAdditionalProperties() instanceof Boolean)) {
Schema inner = (Schema) property.getAdditionalProperties();
if (isObjectSchema(inner)) {
if (inner.getProperties() != null && inner.getProperties().size() > 0) {
flattenProperties(inner.getProperties(), path);
String modelName = resolveModelName(inner.getTitle(), path + "_" + key);
Schema innerModel = createModelFromProperty(inner, modelName);
String existing = matchGenerated(innerModel);
if (existing != null) {
property.setAdditionalProperties(new Schema().$ref(existing));
} else {
property.setAdditionalProperties(new Schema().$ref(modelName));
addGenerated(modelName, innerModel);
openAPI.getComponents().addSchemas(modelName, innerModel);
}
}
}
}
}
if (propsToUpdate.size() > 0) {
for (String key : propsToUpdate.keySet()) {
properties.put(key, propsToUpdate.get(key));
}
}
for (String key : modelsToAdd.keySet()) {
openAPI.getComponents().addSchemas(key, modelsToAdd.get(key));
this.addedModels.put(key, modelsToAdd.get(key));
}
}
Aggregations