Search in sources :

Example 1 with UntypedProperty

use of io.swagger.models.properties.UntypedProperty in project swagger-parser by swagger-api.

the class SwaggerCompatConverter method propertyFromTypedObject.

public Property propertyFromTypedObject(ExtendedTypedObject obj) {
    String type = obj.getType() == null ? null : obj.getType().toString();
    String format = obj.getFormat() == null ? null : obj.getFormat().toString();
    Property output = null;
    if ("array".equals(type)) {
        ArrayProperty am = new ArrayProperty();
        Items items = obj.getItems();
        if (items == null) {
            LOGGER.error("Error! Missing array type for property!  Assuming `object` -- please fix your spec");
            items = new Items();
            items.setType("object");
        }
        type = items.getType() == null ? null : items.getType().toString();
        format = items.getFormat() == null ? null : items.getFormat().toString();
        Map<PropertyBuilder.PropertyId, Object> args = new HashMap<>();
        if (items.getExtraFields().get("enum") != null && items.getExtraFields().get("enum").isArray()) {
            ArrayNode an = (ArrayNode) items.getExtraFields().get("enum");
            List<String> enumValues = new ArrayList<>();
            for (JsonNode jn : an) {
                enumValues.add(jn.textValue());
            }
            args.put(PropertyBuilder.PropertyId.ENUM, enumValues);
        }
        Property innerType = PropertyBuilder.build(type, format, args);
        if (innerType != null && !(innerType instanceof UntypedProperty)) {
            am.setItems(innerType);
        } else if (items.getRef() != null) {
            am.setItems(new RefProperty(items.getRef()));
        } else {
            am.setItems(new RefProperty(type));
        }
        output = am;
    } else {
        Map<PropertyBuilder.PropertyId, Object> args = new HashMap<PropertyBuilder.PropertyId, Object>();
        if (obj.getEnumValues() != null && obj.getEnumValues().size() > 0) {
            args.put(PropertyBuilder.PropertyId.ENUM, obj.getEnumValues());
        }
        if (obj.getMinimum() != null) {
            args.put(PropertyBuilder.PropertyId.MINIMUM, new BigDecimal(obj.getMinimum()));
        }
        if (obj.getMaximum() != null) {
            args.put(PropertyBuilder.PropertyId.MAXIMUM, new BigDecimal(obj.getMaximum()));
        }
        Property i = PropertyBuilder.build(type, format, args);
        if (i != null && !(i instanceof UntypedProperty)) {
            output = i;
        } else {
            if (obj.getRef() != null) {
                output = new RefProperty(obj.getRef());
            } else if (type != null && !type.equals("void")) {
                output = new RefProperty(type);
            }
        }
    }
    return output;
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) UntypedProperty(io.swagger.models.properties.UntypedProperty) BigDecimal(java.math.BigDecimal) RefProperty(io.swagger.models.properties.RefProperty) Items(io.swagger.models.apideclaration.Items) ExtendedTypedObject(io.swagger.models.apideclaration.ExtendedTypedObject) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) ModelProperty(io.swagger.models.apideclaration.ModelProperty) RefProperty(io.swagger.models.properties.RefProperty) UntypedProperty(io.swagger.models.properties.UntypedProperty) PropertyBuilder(io.swagger.models.properties.PropertyBuilder)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ExtendedTypedObject (io.swagger.models.apideclaration.ExtendedTypedObject)1 Items (io.swagger.models.apideclaration.Items)1 ModelProperty (io.swagger.models.apideclaration.ModelProperty)1 ArrayProperty (io.swagger.models.properties.ArrayProperty)1 Property (io.swagger.models.properties.Property)1 PropertyBuilder (io.swagger.models.properties.PropertyBuilder)1 RefProperty (io.swagger.models.properties.RefProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 UntypedProperty (io.swagger.models.properties.UntypedProperty)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1