Search in sources :

Example 1 with FileProperty

use of io.swagger.models.properties.FileProperty 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)

Aggregations

Example (io.swagger.annotations.Example)1 ExampleProperty (io.swagger.annotations.ExampleProperty)1 Model (io.swagger.models.Model)1 AbstractSerializableParameter (io.swagger.models.parameters.AbstractSerializableParameter)1 BodyParameter (io.swagger.models.parameters.BodyParameter)1 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)1 ArrayProperty (io.swagger.models.properties.ArrayProperty)1 FileProperty (io.swagger.models.properties.FileProperty)1 LongProperty (io.swagger.models.properties.LongProperty)1 Property (io.swagger.models.properties.Property)1 PropertyBuilder (io.swagger.models.properties.PropertyBuilder)1 StringProperty (io.swagger.models.properties.StringProperty)1 EnumMap (java.util.EnumMap)1 Map (java.util.Map)1