use of io.swagger.models.properties.ObjectProperty in project swagger-core by swagger-api.
the class PropertyDeserializer method propertyFromNode.
Property propertyFromNode(JsonNode node) {
final String type = getString(node, PropertyBuilder.PropertyId.TYPE);
final String title = getString(node, PropertyBuilder.PropertyId.TITLE);
final String format = getString(node, PropertyBuilder.PropertyId.FORMAT);
String description = getString(node, PropertyBuilder.PropertyId.DESCRIPTION);
JsonNode detailNode = node.get("$ref");
if (detailNode != null) {
return new RefProperty(detailNode.asText()).description(description).title(title);
}
if (ObjectProperty.isType(type) || node.get("properties") != null) {
JsonNode example = getDetailNode(node, PropertyBuilder.PropertyId.EXAMPLE);
detailNode = node.get("additionalProperties");
if (detailNode != null && detailNode.getNodeType().equals(JsonNodeType.OBJECT)) {
Property items = propertyFromNode(detailNode);
if (items != null) {
MapProperty mapProperty = new MapProperty(items).description(description).title(title);
mapProperty.setExample(example);
mapProperty.setMinProperties(getInteger(node, PropertyBuilder.PropertyId.MIN_PROPERTIES));
mapProperty.setMaxProperties(getInteger(node, PropertyBuilder.PropertyId.MAX_PROPERTIES));
mapProperty.setVendorExtensionMap(getVendorExtensions(node));
return mapProperty;
}
} else {
detailNode = node.get("properties");
String detailNodeType = null;
Map<String, Property> properties = new LinkedHashMap<String, Property>();
if (detailNode != null) {
for (Iterator<Map.Entry<String, JsonNode>> iter = detailNode.fields(); iter.hasNext(); ) {
Map.Entry<String, JsonNode> field = iter.next();
Property property = propertyFromNode(field.getValue());
if (property != null) {
properties.put(field.getKey(), property);
} else {
if ("type".equals(field.getKey()) && field.getValue() != null && "array".equals(field.getValue().asText())) {
detailNodeType = "array";
}
if (("description").equals(field.getKey()) && field.getValue().getNodeType().equals(JsonNodeType.STRING)) {
description = field.getValue().asText();
}
}
}
}
if ("array".equals(detailNodeType)) {
ArrayProperty ap = new ArrayProperty().description(description).title(title);
ap.setExample(example);
PropertyBuilder.merge(ap, argsFromNode(detailNode));
ap.setDescription(description);
if (properties.keySet().size() == 1) {
String key = properties.keySet().iterator().next();
ap.setItems(properties.get(key));
}
ap.setVendorExtensionMap(getVendorExtensions(node));
return ap;
}
ObjectProperty objectProperty = new ObjectProperty(properties).description(description).title(title);
objectProperty.setExample(example);
objectProperty.setVendorExtensionMap(getVendorExtensions(node));
List<String> required = getRequired(node, PropertyBuilder.PropertyId.REQUIRED);
objectProperty.setRequiredProperties(required);
return objectProperty;
}
}
if (ArrayProperty.isType(type)) {
detailNode = node.get("items");
if (detailNode != null) {
Property subProperty = propertyFromNode(detailNode);
ArrayProperty arrayProperty = new ArrayProperty().items(subProperty).description(description).title(title);
arrayProperty.setMinItems(getInteger(node, PropertyBuilder.PropertyId.MIN_ITEMS));
arrayProperty.setMaxItems(getInteger(node, PropertyBuilder.PropertyId.MAX_ITEMS));
arrayProperty.setUniqueItems(getBoolean(node, PropertyBuilder.PropertyId.UNIQUE_ITEMS));
JsonNode example = getDetailNode(node, PropertyBuilder.PropertyId.EXAMPLE);
arrayProperty.setExample(example);
arrayProperty.setVendorExtensionMap(getVendorExtensions(node));
return arrayProperty;
}
}
Map<PropertyBuilder.PropertyId, Object> args = argsFromNode(node);
Property output = PropertyBuilder.build(type, format, args);
if (output == null) {
LOGGER.warn("no property from " + type + ", " + format + ", " + args);
return null;
}
output.setDescription(description);
return output;
}
use of io.swagger.models.properties.ObjectProperty in project swagger-core by swagger-api.
the class PropertySerializationTest method serializeObjectPropertyWithRequiredProperties.
@Test(description = "it should serialize an object property with required set")
public void serializeObjectPropertyWithRequiredProperties() throws IOException {
final ObjectProperty p = new ObjectProperty().property("stringProperty", new StringProperty().required(true));
final String json = "{\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}},\"required\":[\"stringProperty\"]}";
assertEquals(m.writeValueAsString(p), json);
}
use of io.swagger.models.properties.ObjectProperty in project swagger-core by swagger-api.
the class JsonDeserializationTest method testNestedObjectProperty.
@Test(description = "it should deserialize nested ObjectProperty(s)")
public void testNestedObjectProperty() throws IOException {
final String json = "{\n" + " \"type\":\"object\",\n" + " \"description\":\"top level object\",\n" + " \"properties\":{\n" + " \"property1\":{\n" + " \"type\":\"string\",\n" + " \"description\":\"First property\"\n" + " },\n" + " \"property2\":{\n" + " \"type\":\"string\",\n" + " \"description\":\"Second property\"\n" + " },\n" + " \"property3\":{\n" + " \"type\":\"object\",\n" + " \"description\":\"Third property\",\n" + " \"properties\":{\n" + " \"property1\":{\n" + " \"type\":\"string\",\n" + " \"description\":\"First nested property\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}";
final Property result = m.readValue(json, Property.class);
assertTrue(result instanceof ObjectProperty);
final Map<String, Property> firstLevelProperties = ((ObjectProperty) result).getProperties();
assertEquals(firstLevelProperties.size(), 3);
final Property property3 = firstLevelProperties.get("property3");
assertTrue(property3 instanceof ObjectProperty);
final Map<String, Property> secondLevelProperties = ((ObjectProperty) property3).getProperties();
assertEquals(secondLevelProperties.size(), 1);
}
use of io.swagger.models.properties.ObjectProperty in project swagger-core by swagger-api.
the class JsonDeserializationTest method testObjectProperty.
@Test(description = "it should deserialize a simple ObjectProperty")
public void testObjectProperty() throws IOException {
final String json = "{\n" + " \"type\":\"object\",\n" + " \"title\":\"objectProperty\",\n" + " \"description\":\"top level object\",\n" + " \"properties\":{\n" + " \"property1\":{\n" + " \"type\":\"string\",\n" + " \"description\":\"First property\"\n" + " },\n" + " \"property2\":{\n" + " \"type\":\"string\",\n" + " \"description\":\"Second property\"\n" + " },\n" + " \"property3\":{\n" + " \"type\":\"string\",\n" + " \"description\":\"Third property\"\n" + " }\n" + " }\n" + "}";
final Property result = m.readValue(json, Property.class);
assertTrue(result instanceof ObjectProperty);
assertEquals(3, ((ObjectProperty) result).getProperties().size());
assertEquals("objectProperty", ((ObjectProperty) result).getTitle());
}
use of io.swagger.models.properties.ObjectProperty in project teiid by teiid.
the class SwaggerMetadataProcessor method buildResponse.
private boolean buildResponse(final MetadataFactory mf, final Swagger swagger, final Procedure procedure, final Response resp) throws TranslatorException {
PropertyAction pa = new PropertyAction() {
@Override
public void execute(String name, String nameInSource, Property property, boolean array) {
String type = getPropertyType(property, array);
Column c = mf.addProcedureResultSetColumn(name, type, procedure);
if (!name.equalsIgnoreCase(nameInSource)) {
c.setNameInSource(nameInSource);
}
}
};
Property schema = resp.getSchema();
if (schema != null) {
if (isSimple(schema)) {
boolean array = false;
if (schema instanceof ArrayProperty) {
schema = ((ArrayProperty) schema).getItems();
array = true;
}
if (resp.getHeaders() == null || resp.getHeaders().isEmpty()) {
String type = SwaggerTypeManager.teiidType(schema.getType(), schema.getFormat(), array);
mf.addProcedureParameter("return", type, ProcedureParameter.Type.ReturnValue, procedure);
} else {
Map<String, Property> properties = new LinkedHashMap<String, Property>();
properties.put("return", schema);
walkProperties(swagger, properties, null, null, pa);
}
} else {
// since the return is always a collection unwrap the array without any issues.
if (schema instanceof ArrayProperty) {
schema = ((ArrayProperty) schema).getItems();
}
if (schema instanceof ObjectProperty) {
walkProperties(swagger, ((ObjectProperty) schema).getProperties(), null, null, pa);
} else if (schema instanceof RefProperty) {
String modelName = ((RefProperty) schema).getSimpleRef();
Model model = swagger.getDefinitions().get(modelName);
walkProperties(swagger, model.getProperties(), null, null, pa);
} else if (schema instanceof MapProperty) {
Property property = ((MapProperty) schema).getAdditionalProperties();
String type = SwaggerTypeManager.teiidType(property.getType(), property.getFormat(), false);
Column c = mf.addProcedureResultSetColumn(KEY_NAME, "string", procedure);
c.setNameInSource(KEY_NAME);
c = mf.addProcedureResultSetColumn(KEY_VALUE, type, procedure);
c.setNameInSource(KEY_VALUE);
} else {
throw new TranslatorException("File properties are not supported");
}
}
}
Map<String, Property> headers = resp.getHeaders();
if (headers != null && !headers.isEmpty()) {
walkProperties(swagger, headers, null, null, pa);
}
return procedure.getResultSet() != null;
}
Aggregations