Search in sources :

Example 71 with Property

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

the class ParameterProcessor method applyAnnotations.

public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, Type type, List<Annotation> annotations) {
    final AnnotationsHelper helper = new AnnotationsHelper(annotations, type);
    if (helper.isContext()) {
        return null;
    }
    final ParamWrapper<?> param = helper.getApiParam();
    if (param.isHidden()) {
        return null;
    }
    final String defaultValue = helper.getDefaultValue();
    if (parameter instanceof AbstractSerializableParameter) {
        final AbstractSerializableParameter<?> p = (AbstractSerializableParameter<?>) parameter;
        if (param.isRequired()) {
            p.setRequired(true);
        }
        if (param.getReadOnly()) {
            p.readOnly(param.getReadOnly());
        }
        if (param.getAllowEmptyValue()) {
            p.allowEmptyValue(param.getAllowEmptyValue());
        }
        if (StringUtils.isNotEmpty(param.getName())) {
            p.setName(param.getName());
        }
        if (StringUtils.isNotEmpty(param.getDescription())) {
            p.setDescription(param.getDescription());
        }
        if (StringUtils.isNotEmpty(param.getExample())) {
            p.setExample(param.getExample());
        }
        if (StringUtils.isNotEmpty(param.getAccess())) {
            p.setAccess(param.getAccess());
        }
        if (StringUtils.isNoneEmpty(param.getCollectionFormat())) {
            p.setCollectionFormat(param.getCollectionFormat());
        }
        if (StringUtils.isNotEmpty(param.getDataType())) {
            if ("java.io.File".equalsIgnoreCase(param.getDataType())) {
                p.setProperty(new FileProperty());
            } else if ("long".equalsIgnoreCase(param.getDataType())) {
                p.setProperty(new LongProperty());
            } else {
                p.setType(param.getDataType());
            }
        }
        if (helper.getMin() != null) {
            p.setMinimum(helper.getMin());
            if (helper.isMinExclusive()) {
                p.setExclusiveMinimum(true);
            }
        }
        if (helper.getMax() != null) {
            p.setMaximum(helper.getMax());
            if (helper.isMaxExclusive()) {
                p.setExclusiveMaximum(true);
            }
        }
        if (helper.getMinItems() != null) {
            p.setMinItems(helper.getMinItems());
        }
        if (helper.getMaxItems() != null) {
            p.setMaxItems(helper.getMaxItems());
        }
        if (helper.getMinLength() != null) {
            p.setMinLength(helper.getMinLength());
        }
        if (helper.getMaxLength() != null) {
            p.setMaxLength(helper.getMaxLength());
        }
        if (helper.getPattern() != null) {
            p.setPattern(helper.getPattern());
        }
        if (helper.isRequired() != null) {
            p.setRequired(true);
        }
        if (helper.getType() != null) {
            p.setType(helper.getType());
        }
        if (helper.getFormat() != null) {
            p.setFormat(helper.getFormat());
        }
        AllowableValues allowableValues = AllowableValuesUtils.create(param.getAllowableValues());
        if (p.getItems() != null || param.isAllowMultiple()) {
            if (p.getItems() == null) {
                // Convert to array
                final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
                args.put(PropertyBuilder.PropertyId.DEFAULT, p.getDefaultValue());
                p.setDefaultValue(null);
                args.put(PropertyBuilder.PropertyId.ENUM, p.getEnum());
                p.setEnum(null);
                args.put(PropertyBuilder.PropertyId.MINIMUM, p.getMinimum());
                p.setMinimum(null);
                args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MINIMUM, p.isExclusiveMinimum());
                p.setExclusiveMinimum(null);
                args.put(PropertyBuilder.PropertyId.MAXIMUM, p.getMaximum());
                p.setMaximum(null);
                args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MAXIMUM, p.isExclusiveMaximum());
                args.put(PropertyBuilder.PropertyId.MIN_LENGTH, p.getMinLength());
                p.setMinLength(null);
                args.put(PropertyBuilder.PropertyId.MAX_LENGTH, p.getMaxLength());
                p.setMaxLength(null);
                args.put(PropertyBuilder.PropertyId.PATTERN, p.getPattern());
                p.setPattern(null);
                args.put(PropertyBuilder.PropertyId.EXAMPLE, p.getExample());
                p.setExclusiveMaximum(null);
                Property items = PropertyBuilder.build(p.getType(), p.getFormat(), args);
                p.type(ArrayProperty.TYPE).format(null).items(items);
            }
            final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
            if (StringUtils.isNotEmpty(defaultValue)) {
                args.put(PropertyBuilder.PropertyId.DEFAULT, defaultValue);
            }
            if (helper.getMin() != null) {
                args.put(PropertyBuilder.PropertyId.MINIMUM, helper.getMin());
                if (helper.isMinExclusive()) {
                    args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MINIMUM, true);
                }
            }
            if (helper.getMax() != null) {
                args.put(PropertyBuilder.PropertyId.MAXIMUM, helper.getMax());
                if (helper.isMaxExclusive()) {
                    args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MAXIMUM, true);
                }
            }
            if (helper.getMinLength() != null) {
                args.put(PropertyBuilder.PropertyId.MIN_LENGTH, helper.getMinLength());
            }
            if (helper.getMaxLength() != null) {
                args.put(PropertyBuilder.PropertyId.MAX_LENGTH, helper.getMaxLength());
            }
            if (helper.getPattern() != null) {
                args.put(PropertyBuilder.PropertyId.PATTERN, helper.getPattern());
            }
            //Overwrite Bean validation values with allowable values if present
            if (allowableValues != null) {
                args.putAll(allowableValues.asPropertyArguments());
            }
            PropertyBuilder.merge(p.getItems(), args);
        } else {
            if (StringUtils.isNotEmpty(defaultValue)) {
                p.setDefaultValue(defaultValue);
            }
            //Overwrite Bean validation values with allowable values if present
            if (allowableValues != null) {
                processAllowedValues(allowableValues, p);
            }
        //                else {
        //                    processJsr303Annotations(helper, p);
        //                }
        }
    } else {
        // must be a body param
        BodyParameter bp = new BodyParameter();
        if (helper.getApiParam() != null) {
            ParamWrapper<?> pw = helper.getApiParam();
            if (pw instanceof ApiParamWrapper) {
                ApiParamWrapper apiParam = (ApiParamWrapper) pw;
                Example example = apiParam.getExamples();
                if (example != null && example.value() != null) {
                    for (ExampleProperty ex : example.value()) {
                        String mediaType = ex.mediaType();
                        String value = ex.value();
                        if (!mediaType.isEmpty() && !value.isEmpty()) {
                            bp.example(mediaType.trim(), value.trim());
                        }
                    }
                }
            } else if (pw instanceof ApiImplicitParamWrapper) {
                ApiImplicitParamWrapper apiParam = (ApiImplicitParamWrapper) pw;
                Example example = apiParam.getExamples();
                if (example != null && example.value() != null) {
                    for (ExampleProperty ex : example.value()) {
                        String mediaType = ex.mediaType();
                        String value = ex.value();
                        if (!mediaType.isEmpty() && !value.isEmpty()) {
                            bp.example(mediaType.trim(), value.trim());
                        }
                    }
                }
            }
        }
        bp.setRequired(param.isRequired());
        bp.setName(StringUtils.isNotEmpty(param.getName()) ? param.getName() : "body");
        if (StringUtils.isNotEmpty(param.getDescription())) {
            bp.setDescription(param.getDescription());
        }
        if (StringUtils.isNotEmpty(param.getAccess())) {
            bp.setAccess(param.getAccess());
        }
        final Property property = ModelConverters.getInstance().readAsProperty(type);
        if (property != null) {
            final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
            if (StringUtils.isNotEmpty(defaultValue)) {
                args.put(PropertyBuilder.PropertyId.DEFAULT, defaultValue);
            }
            bp.setSchema(PropertyBuilder.toModel(PropertyBuilder.merge(property, args)));
            for (Map.Entry<String, Model> entry : ModelConverters.getInstance().readAll(type).entrySet()) {
                swagger.addDefinition(entry.getKey(), entry.getValue());
            }
        }
        parameter = bp;
    }
    return parameter;
}
Also used : ExampleProperty(io.swagger.annotations.ExampleProperty) BodyParameter(io.swagger.models.parameters.BodyParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) FileProperty(io.swagger.models.properties.FileProperty) LongProperty(io.swagger.models.properties.LongProperty) Example(io.swagger.annotations.Example) Model(io.swagger.models.Model) EnumMap(java.util.EnumMap) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) LongProperty(io.swagger.models.properties.LongProperty) Property(io.swagger.models.properties.Property) ExampleProperty(io.swagger.annotations.ExampleProperty) FileProperty(io.swagger.models.properties.FileProperty) Map(java.util.Map) EnumMap(java.util.EnumMap) PropertyBuilder(io.swagger.models.properties.PropertyBuilder)

Example 72 with Property

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

the class SpecFilter method filterDefinitions.

public Map<String, Model> filterDefinitions(SwaggerSpecFilter filter, Map<String, Model> definitions, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    if (definitions == null) {
        return null;
    }
    Map<String, Model> clonedDefinitions = new LinkedHashMap<String, Model>();
    for (String key : definitions.keySet()) {
        Model definition = definitions.get(key);
        Map<String, Property> clonedProperties = new LinkedHashMap<String, Property>();
        if (definition.getProperties() != null) {
            for (String propName : definition.getProperties().keySet()) {
                Property property = definition.getProperties().get(propName);
                if (property != null) {
                    boolean shouldInclude = filter.isPropertyAllowed(definition, property, propName, params, cookies, headers);
                    if (shouldInclude) {
                        clonedProperties.put(propName, property);
                    }
                }
            }
        }
        Model clonedModel = (Model) definition.clone();
        if (clonedModel.getProperties() != null) {
            clonedModel.getProperties().clear();
        }
        if (definition.getVendorExtensions() != null && clonedModel.getVendorExtensions() != null) {
            clonedModel.getVendorExtensions().putAll(definition.getVendorExtensions());
        }
        clonedModel.setProperties(clonedProperties);
        clonedDefinitions.put(key, clonedModel);
    }
    return clonedDefinitions;
}
Also used : Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) LinkedHashMap(java.util.LinkedHashMap)

Example 73 with Property

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

the class ModelResolver method resolveSubtypes.

private boolean resolveSubtypes(ModelImpl model, BeanDescription bean, ModelConverterContext context) {
    final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
    if (types == null) {
        return false;
    }
    /**
         * As the introspector will find @JsonSubTypes for a child class that are present on its super classes, the
         * code segment below will also run the introspector on the parent class, and then remove any sub-types that are
         * found for the parent from the sub-types found for the child. The same logic all applies to implemented
         * interfaces, and is accounted for below.
         */
    removeSuperClassAndInterfaceSubTypes(types, bean);
    int count = 0;
    final Class<?> beanClass = bean.getClassInfo().getAnnotated();
    for (NamedType subtype : types) {
        final Class<?> subtypeType = subtype.getType();
        if (!beanClass.isAssignableFrom(subtypeType)) {
            continue;
        }
        final Model subtypeModel = context.resolve(subtypeType);
        if (subtypeModel instanceof ModelImpl) {
            final ModelImpl impl = (ModelImpl) subtypeModel;
            // check if model name was inherited
            if (impl.getName().equals(model.getName())) {
                impl.setName(_typeNameResolver.nameForType(_mapper.constructType(subtypeType), TypeNameResolver.Options.SKIP_API_MODEL));
            }
            // remove shared properties defined in the parent
            final Map<String, Property> baseProps = model.getProperties();
            final Map<String, Property> subtypeProps = impl.getProperties();
            if (baseProps != null && subtypeProps != null) {
                for (Map.Entry<String, Property> entry : baseProps.entrySet()) {
                    if (entry.getValue().equals(subtypeProps.get(entry.getKey()))) {
                        subtypeProps.remove(entry.getKey());
                    }
                }
            }
            impl.setDiscriminator(null);
            ComposedModel child = new ComposedModel().parent(new RefModel(model.getName())).child(impl);
            context.defineModel(impl.getName(), child, subtypeType, null);
            ++count;
        }
    }
    return count != 0;
}
Also used : RefModel(io.swagger.models.RefModel) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ApiModel(io.swagger.annotations.ApiModel) ModelImpl(io.swagger.models.ModelImpl) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) UUIDProperty(io.swagger.models.properties.UUIDProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 74 with Property

use of io.swagger.models.properties.Property 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 75 with Property

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

the class ModelWithRangesTest method modelWithRangesTest.

@Test(description = "test model with @ApiModelProperty.allowableValues")
public void modelWithRangesTest() {
    final Map<String, Property> properties = ModelConverters.getInstance().read(ModelWithRanges.class).get("ModelWithRanges").getProperties();
    final IntegerProperty inclusiveRange = (IntegerProperty) properties.get("inclusiveRange");
    assertEquals(inclusiveRange.getMinimum(), new BigDecimal(1));
    assertEquals(inclusiveRange.getMaximum(), new BigDecimal(5));
    assertNull(inclusiveRange.getExclusiveMaximum());
    assertNull(inclusiveRange.getExclusiveMinimum());
    final IntegerProperty exclusiveRange = (IntegerProperty) properties.get("exclusiveRange");
    assertEquals(exclusiveRange.getMinimum(), new BigDecimal(1));
    assertEquals(exclusiveRange.getMaximum(), new BigDecimal(5));
    assertEquals(exclusiveRange.getExclusiveMinimum(), Boolean.TRUE);
    assertEquals(exclusiveRange.getExclusiveMaximum(), Boolean.TRUE);
    final IntegerProperty positiveInfinityRange = (IntegerProperty) properties.get("positiveInfinityRange");
    assertEquals(positiveInfinityRange.getMinimum(), new BigDecimal(1.0));
    assertNull(positiveInfinityRange.getMaximum());
    assertNull(positiveInfinityRange.getExclusiveMaximum());
    assertNull(positiveInfinityRange.getExclusiveMinimum());
    final IntegerProperty negativeInfinityRange = (IntegerProperty) properties.get("negativeInfinityRange");
    assertNull(negativeInfinityRange.getMinimum());
    assertEquals(negativeInfinityRange.getMaximum(), new BigDecimal(5.0));
    assertNull(negativeInfinityRange.getExclusiveMaximum());
    assertNull(negativeInfinityRange.getExclusiveMinimum());
    final StringProperty stringValues = (StringProperty) properties.get("stringValues");
    assertEquals(stringValues.getEnum(), Arrays.asList("str1", "str2"));
    final DoubleProperty doubleValues = (DoubleProperty) properties.get("doubleValues");
    assertEquals(doubleValues.getMinimum(), new BigDecimal("1.0"));
    assertEquals(doubleValues.getMaximum(), new BigDecimal("8.0"));
    assertEquals(doubleValues.getExclusiveMaximum(), Boolean.TRUE);
    assertNull(doubleValues.getExclusiveMinimum());
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) StringProperty(io.swagger.models.properties.StringProperty) DoubleProperty(io.swagger.models.properties.DoubleProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) DoubleProperty(io.swagger.models.properties.DoubleProperty) StringProperty(io.swagger.models.properties.StringProperty) Property(io.swagger.models.properties.Property) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

Property (io.swagger.models.properties.Property)145 Test (org.testng.annotations.Test)96 ArrayProperty (io.swagger.models.properties.ArrayProperty)86 StringProperty (io.swagger.models.properties.StringProperty)75 RefProperty (io.swagger.models.properties.RefProperty)65 MapProperty (io.swagger.models.properties.MapProperty)59 Model (io.swagger.models.Model)52 IntegerProperty (io.swagger.models.properties.IntegerProperty)48 LongProperty (io.swagger.models.properties.LongProperty)35 DoubleProperty (io.swagger.models.properties.DoubleProperty)32 FloatProperty (io.swagger.models.properties.FloatProperty)27 BooleanProperty (io.swagger.models.properties.BooleanProperty)25 ObjectProperty (io.swagger.models.properties.ObjectProperty)25 Operation (io.swagger.models.Operation)24 DateTimeProperty (io.swagger.models.properties.DateTimeProperty)23 DateProperty (io.swagger.models.properties.DateProperty)22 ApiModelProperty (io.swagger.annotations.ApiModelProperty)20 FileProperty (io.swagger.models.properties.FileProperty)18 ModelImpl (io.swagger.models.ModelImpl)17 Response (io.swagger.models.Response)16