Search in sources :

Example 1 with ObjectProperty

use of io.swagger.models.properties.ObjectProperty in project swagger-core by swagger-api.

the class ObjectPropertyTest method readModelWithObjectProperty.

@Test(description = "convert a model with object properties")
public void readModelWithObjectProperty() throws IOException {
    String json = "{" + "   \"properties\":{" + "      \"id\":{" + "         \"type\":\"string\"" + "      }," + "      \"someObject\":{" + "         \"type\":\"object\"," + "        \"x-foo\": \"vendor x\"," + "         \"properties\":{" + "            \"innerId\":{" + "               \"type\":\"string\"" + "            }" + "         }" + "      }" + "   }" + "}";
    ModelImpl model = Json.mapper().readValue(json, ModelImpl.class);
    Property p = model.getProperties().get("someObject");
    assertTrue(p instanceof ObjectProperty);
    ObjectProperty op = (ObjectProperty) p;
    Property sp = op.getProperties().get("innerId");
    assertTrue(sp instanceof StringProperty);
    assertTrue(op.getVendorExtensions() != null);
    assertNotNull(op.getVendorExtensions().get("x-foo"));
    assertEquals(op.getVendorExtensions().get("x-foo"), "vendor x");
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) ObjectProperty(io.swagger.models.properties.ObjectProperty) StringProperty(io.swagger.models.properties.StringProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 2 with ObjectProperty

use of io.swagger.models.properties.ObjectProperty in project swagger-core by swagger-api.

the class PropertySerializationTest method deserializeObjectPropertyWithRequiredProperties.

@Test(description = "it should deserialize an object property with required set")
public void deserializeObjectPropertyWithRequiredProperties() 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(p, m.readValue(json, ObjectProperty.class));
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) StringProperty(io.swagger.models.properties.StringProperty) Test(org.testng.annotations.Test)

Example 3 with ObjectProperty

use of io.swagger.models.properties.ObjectProperty in project java-chassis by ServiceComb.

the class ConverterMgrTest method should_support_object_property.

@Test
public void should_support_object_property() {
    Property property = new ObjectProperty();
    assertThat(ConverterMgr.findJavaType(swagger, property).getRawClass()).isEqualTo(Object.class);
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) Property(io.swagger.models.properties.Property) Test(org.junit.Test)

Example 4 with ObjectProperty

use of io.swagger.models.properties.ObjectProperty in project carbon-apimgt by wso2.

the class WSDL11SOAPOperationExtractor method createPropertyFromNode.

/**
 * Creates a swagger property from given wsdl node.
 *
 * @param node wsdl node
 * @return generated swagger property
 */
private Property createPropertyFromNode(Node node, boolean prevNodeExist) {
    Property property = null;
    if (node.hasAttributes()) {
        if (node.getAttributes().getNamedItem(SOAPToRESTConstants.TYPE_ATTRIBUTE) != null) {
            if (node.getAttributes().getNamedItem(NAME_ATTRIBUTE) != null) {
                String dataType = node.getAttributes().getNamedItem(SOAPToRESTConstants.TYPE_ATTRIBUTE).getNodeValue().contains(":") ? node.getAttributes().getNamedItem(SOAPToRESTConstants.TYPE_ATTRIBUTE).getNodeValue().split(":")[1] : node.getAttributes().getNamedItem(SOAPToRESTConstants.TYPE_ATTRIBUTE).getNodeValue();
                property = getPropertyFromDataType(dataType);
                if (property instanceof RefProperty) {
                    ((RefProperty) property).set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + dataType);
                }
                property.setName(node.getAttributes().getNamedItem(NAME_ATTRIBUTE).getNodeValue());
            }
        } else if (node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE) != null) {
            property = new RefProperty();
            String dataType = node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue().contains(":") ? node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue().split(":")[1] : node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue();
            ((RefProperty) property).set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + dataType);
            property.setName(dataType);
        } else if (node.getAttributes().getNamedItem("base") != null) {
            String dataType = node.getAttributes().getNamedItem("base").getNodeValue().contains(":") ? node.getAttributes().getNamedItem("base").getNodeValue().split(":")[1] : node.getAttributes().getNamedItem("base").getNodeValue();
            property = getPropertyFromDataType(dataType);
            if (property instanceof RefProperty) {
                ((RefProperty) property).set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + dataType);
            }
            property.setName(dataType);
        } else if (node.getAttributes().getNamedItem(NAME_ATTRIBUTE) != null && prevNodeExist) {
            property = new ObjectProperty();
            property.setName(getNodeName(node));
            if (node.hasChildNodes()) {
                NodeList childNodes = node.getChildNodes();
                for (int i = 0; i < childNodes.getLength(); i++) {
                    Node childNode = childNodes.item(i);
                    if (SOAPToRESTConstants.COMPLEX_TYPE_NODE_NAME.equals(childNode.getLocalName())) {
                        isComplexTypeContainsArray(childNode);
                    }
                }
            }
        }
        if (isArrayType(node) || isArrayType) {
            isArrayType = false;
            Property arrayProperty = new ArrayProperty();
            ((ArrayProperty) arrayProperty).setItems(property);
            return arrayProperty;
        }
    }
    return property;
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) RefProperty(io.swagger.models.properties.RefProperty) FloatProperty(io.swagger.models.properties.FloatProperty) DateProperty(io.swagger.models.properties.DateProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) RefProperty(io.swagger.models.properties.RefProperty)

Example 5 with ObjectProperty

use of io.swagger.models.properties.ObjectProperty in project carbon-apimgt by wso2.

the class WSDL11SOAPOperationExtractor method addModelDefinition.

/**
 * Adds swagger type definitions to swagger model
 *
 * @param current current wsdl node
 * @param model   swagger model element
 * @param type    wsdl node type{i.e: complexType, simpleType}
 */
private void addModelDefinition(Node current, ModelImpl model, String type, boolean prevNodeExist, Node prevNode) {
    if (current.getParentNode() != null) {
        String xPath = getXpathFromNode(current);
        if (log.isDebugEnabled()) {
            log.debug("Processing current document node: " + getNodeName(current) + " with the xPath:" + xPath);
        }
        String[] elements = xPath.split(XPATH_REPLACEMENT);
        if (getNodeName(current).equals(elements[elements.length - 1]) || getNodeName(current).equals(elements[elements.length - 1].substring(elements[elements.length - 1].indexOf(":") + 1)) || type.equals(SOAPToRESTConstants.RESTRICTION_ATTR)) {
            if (StringUtils.isBlank(model.getName())) {
                model.setName(getNodeName(current));
                if (!SOAPToRESTConstants.SIMPLE_TYPE_NODE_NAME.equals(type)) {
                    if (!prevNodeExist) {
                        Property prop = createPropertyFromNode(current, false);
                        Map<String, Property> propertyMap = new HashMap<>();
                        propertyMap.put(getNodeName(current), prop);
                        model.setProperties(propertyMap);
                    }
                    if (isArrayType(current)) {
                        model.setType(ArrayProperty.TYPE);
                    } else {
                        model.setType(ObjectProperty.TYPE);
                    }
                    String elementFormDefault = null;
                    if (current.getParentNode().getAttributes() != null && current.getParentNode().getAttributes().getNamedItem(SOAPToRESTConstants.ELEMENT_FORM_DEFAULT) != null) {
                        elementFormDefault = current.getParentNode().getAttributes().getNamedItem(SOAPToRESTConstants.ELEMENT_FORM_DEFAULT).getNodeValue();
                    }
                    if (StringUtils.isNotEmpty(elementFormDefault) && SOAPToRESTConstants.QUALIFIED.equals(elementFormDefault)) {
                        model.setVendorExtension(SOAPToRESTConstants.X_NAMESPACE_QUALIFIED, true);
                    } else {
                        model.setVendorExtension(SOAPToRESTConstants.X_NAMESPACE_QUALIFIED, false);
                    }
                }
            } else if (model.getProperties() == null) {
                if (SOAPToRESTConstants.RESTRICTION_ATTR.equals(type)) {
                    Property restrictionProp = createPropertyFromNode(current, true);
                    if (!(restrictionProp instanceof RefProperty || restrictionProp instanceof ObjectProperty || restrictionProp instanceof ArrayProperty)) {
                        model.setType(restrictionProp.getType());
                    } else {
                        model.setType(StringProperty.TYPE);
                    }
                } else {
                    Map<String, Property> propertyMap = new HashMap<>();
                    Property prop = createPropertyFromNode(current, true);
                    propertyMap.put(getNodeName(current), prop);
                    model.setProperties(propertyMap);
                }
            } else {
                Property parentProp = null;
                int pos = 0;
                for (String element : elements) {
                    if (model.getName().equals(element)) {
                    // do nothing
                    } else if (model.getProperties().containsKey(element)) {
                        parentProp = model.getProperties().get(element);
                        if (pos == elements.length - 1) {
                            if (SOAPToRESTConstants.RESTRICTION_ATTR.equals(type)) {
                                model.getProperties().remove(element);
                                parentProp = createPropertyFromNode(current, true);
                                parentProp.setName(element);
                                model.addProperty(element, parentProp);
                            } else {
                                if (isChildNode(current, prevNode)) {
                                    int index = Arrays.asList(elements).indexOf(element);
                                    Property parentProperty = null;
                                    String parentName = "";
                                    for (int i = 1; i < index; i++) {
                                        parentProperty = model.getProperties().get(elements[index - i]);
                                        if (parentProperty != null) {
                                            model.getProperties().remove(element);
                                            parentName = elements[index - i];
                                            break;
                                        }
                                    }
                                    if (parentProperty instanceof ArrayProperty) {
                                        if (((ArrayProperty) parentProperty).getItems() != null) {
                                            if (parentName.equals(((ArrayProperty) parentProperty).getItems().getName())) {
                                                ObjectProperty objectProperty = new ObjectProperty();
                                                Map propertiesMap = new HashMap();
                                                if (((ObjectProperty) (((ArrayProperty) parentProperty).getItems())).getProperties() != null) {
                                                    propertiesMap = ((ObjectProperty) (((ArrayProperty) parentProperty).getItems())).getProperties();
                                                }
                                                propertiesMap.put(element, createPropertyFromNode(current, true));
                                                objectProperty.setProperties(propertiesMap);
                                                objectProperty.setName(parentName);
                                                ((ArrayProperty) parentProperty).setItems(objectProperty);
                                                parentProp = parentProperty;
                                            }
                                        } else {
                                            ((ArrayProperty) parentProperty).setItems(createPropertyFromNode(current, true));
                                        }
                                    } else if (parentProperty instanceof ObjectProperty) {
                                        if (((ObjectProperty) parentProperty).getProperties() != null) {
                                            if ((((ObjectProperty) parentProperty).getProperties()).get(parentName) != null) {
                                                Property objectProperty = new ObjectProperty();
                                                Map propertiesMap = new HashMap();
                                                propertiesMap.put(element, createPropertyFromNode(current, true));
                                                ((ObjectProperty) objectProperty).setProperties(propertiesMap);
                                                ((ObjectProperty) parentProperty).setProperties(propertiesMap);
                                            }
                                        }
                                    }
                                } else {
                                    model.addProperty(getNodeName(current), createPropertyFromNode(current, true));
                                }
                            }
                        }
                    } else {
                        if (parentProp instanceof ArrayProperty) {
                            if (((ArrayProperty) parentProp).getItems().getName() == null) {
                                Property currentProp = createPropertyFromNode(current, true);
                                if (currentProp instanceof ObjectProperty) {
                                    ((ArrayProperty) parentProp).setItems(currentProp);
                                } else if (currentProp instanceof ArrayProperty) {
                                    Map<String, Property> arrayPropMap = new HashMap();
                                    arrayPropMap.put(getNodeName(current), currentProp);
                                    ObjectProperty arrayObjProp = new ObjectProperty();
                                    arrayObjProp.setProperties(arrayPropMap);
                                    ((ArrayProperty) parentProp).setItems(arrayObjProp);
                                } else {
                                    if (((ArrayProperty) parentProp).getItems() instanceof ObjectProperty) {
                                        Property objectProperty = ((ArrayProperty) parentProp).getItems();
                                        Map parentProperties = null;
                                        if (((ObjectProperty) objectProperty).getProperties() != null) {
                                            parentProperties = ((ObjectProperty) objectProperty).getProperties();
                                        }
                                        Map propertiesMap = new HashMap();
                                        propertiesMap.put(currentProp.getName(), currentProp);
                                        if (parentProperties != null) {
                                            Property obj = new ObjectProperty();
                                            obj.setName(element);
                                            if (parentProperties.get(element) instanceof ArrayProperty) {
                                                ((ObjectProperty) obj).setProperties(propertiesMap);
                                                parentProperties.remove(element);
                                                parentProperties.put(element, obj);
                                            } else if (parentProperties.get(element) instanceof ObjectProperty) {
                                                Map parentPropertyMap = ((ObjectProperty) parentProperties.get(element)).getProperties();
                                                parentPropertyMap.put(currentProp.getName(), currentProp);
                                                ((ObjectProperty) obj).setProperties(parentProperties);
                                            }
                                            ((ObjectProperty) objectProperty).setProperties(parentProperties);
                                        } else {
                                            ((ObjectProperty) objectProperty).setProperties(propertiesMap);
                                        }
                                        ((ArrayProperty) parentProp).setItems(objectProperty);
                                    } else {
                                        ((ArrayProperty) parentProp).setItems(currentProp);
                                    }
                                }
                            } else {
                                ((ArrayProperty) parentProp).setItems(createPropertyFromNode(current, true));
                            }
                            parentProp = ((ArrayProperty) parentProp).getItems();
                        } else if (parentProp instanceof ObjectProperty) {
                            if (SOAPToRESTConstants.RESTRICTION_ATTR.equals(type) && pos == elements.length - 1) {
                                parentProp = createPropertyFromNode(current, true);
                                parentProp.setName(element);
                            } else {
                                if (((ObjectProperty) parentProp).getProperties() == null) {
                                    Map<String, Property> propertyMap = new HashMap<>();
                                    ((ObjectProperty) parentProp).setProperties(propertyMap);
                                }
                                Property childProp = createPropertyFromNode(current, true);
                                if (((ObjectProperty) parentProp).getProperties().get(element) == null) {
                                    ((ObjectProperty) parentProp).getProperties().put(getNodeName(current), childProp);
                                }
                                if (childProp instanceof ObjectProperty) {
                                    parentProp = ((ObjectProperty) parentProp).getProperties().get(element);
                                } else if (childProp instanceof ArrayProperty) {
                                    parentProp = childProp;
                                }
                            }
                        } else if (parentProp == null) {
                            if (StringUtils.isNotBlank(getNodeName(current))) {
                                model.addProperty(getNodeName(current), createPropertyFromNode(current, true));
                            } else if (StringUtils.isNotBlank(getRefNodeName(current))) {
                                model.addProperty(getRefNodeName(current), createPropertyFromNode(current, true));
                            }
                        }
                    }
                    pos++;
                }
            }
        }
    }
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) RefProperty(io.swagger.models.properties.RefProperty) FloatProperty(io.swagger.models.properties.FloatProperty) DateProperty(io.swagger.models.properties.DateProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) RefProperty(io.swagger.models.properties.RefProperty)

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