Search in sources :

Example 6 with ObjectProperty

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;
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) JsonNode(com.fasterxml.jackson.databind.JsonNode) RefProperty(io.swagger.models.properties.RefProperty) LinkedHashMap(java.util.LinkedHashMap) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap)

Example 7 with ObjectProperty

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);
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) StringProperty(io.swagger.models.properties.StringProperty) Test(org.testng.annotations.Test)

Example 8 with ObjectProperty

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);
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 9 with ObjectProperty

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());
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 10 with ObjectProperty

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;
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) LinkedHashMap(java.util.LinkedHashMap) RefProperty(io.swagger.models.properties.RefProperty) BaseColumn(org.teiid.metadata.BaseColumn) Column(org.teiid.metadata.Column) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) TranslatorException(org.teiid.translator.TranslatorException) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) TranslatorProperty(org.teiid.translator.TranslatorProperty) ExtensionMetadataProperty(org.teiid.metadata.ExtensionMetadataProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) FileProperty(io.swagger.models.properties.FileProperty)

Aggregations

ObjectProperty (io.swagger.models.properties.ObjectProperty)14 Property (io.swagger.models.properties.Property)12 ArrayProperty (io.swagger.models.properties.ArrayProperty)8 RefProperty (io.swagger.models.properties.RefProperty)7 StringProperty (io.swagger.models.properties.StringProperty)7 MapProperty (io.swagger.models.properties.MapProperty)5 Test (org.testng.annotations.Test)5 BooleanProperty (io.swagger.models.properties.BooleanProperty)3 DateProperty (io.swagger.models.properties.DateProperty)3 DateTimeProperty (io.swagger.models.properties.DateTimeProperty)3 DecimalProperty (io.swagger.models.properties.DecimalProperty)3 DoubleProperty (io.swagger.models.properties.DoubleProperty)3 FloatProperty (io.swagger.models.properties.FloatProperty)3 IntegerProperty (io.swagger.models.properties.IntegerProperty)3 LongProperty (io.swagger.models.properties.LongProperty)3 HashMap (java.util.HashMap)3 Model (io.swagger.models.Model)2 RefModel (io.swagger.models.RefModel)2 FileProperty (io.swagger.models.properties.FileProperty)2 LinkedHashMap (java.util.LinkedHashMap)2