swagger2.9.2 报错:java.lang.NumberFormatException: empty String

swagger2.9.2 报错:java.lang.NumberFormatException: empty String
原因:通过对比1.5.20与2.9.2源码,发现2.9.2源码缺失部分判断信息

在这里插入图片描述
解决方案:
1.降低版本自1.5.20;
2.对实体勒种Integer,BigDecimal,Long等类型,增加默认值example
在这里插入图片描述
3.推荐,重写
创建:io.swagger.models.parameters包,同时重写AbstractSerializableParameter.java
在这里插入图片描述

package io.swagger.models.parameters;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.models.properties.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* @description   修改io.swagger.models.parameters源码(通过对比1.5.20与2.9.2源码,发现2.9.2源码缺失部分判断信息)
*                解决:Integer,BigDecimal,Long等类型,没有默认值example的报错问题
* @author        plj
* @date          2020/9/27 9:42
* @version       V1.0
**/
@JsonPropertyOrder({ "name", "in", "description", "required", "type", "items", "collectionFormat", "default", "maximum",
        "exclusiveMaximum", "minimum", "exclusiveMinimum", "maxLength", "minLength", "pattern", "maxItems", "minItems",
        "uniqueItems", "multipleOf" })
public abstract class AbstractSerializableParameter<T extends AbstractSerializableParameter<T>> extends AbstractParameter implements SerializableParameter {
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSerializableParameter.class);
    protected String type;
    protected String format;
    protected String collectionFormat;
    protected Property items;
    protected Boolean exclusiveMaximum;
    protected BigDecimal maximum;
    protected Boolean exclusiveMinimum;
    protected BigDecimal minimum;
    protected String example;
    private Integer maxItems;
    private Integer minItems;
    protected Boolean allowEmptyValue;

    @JsonIgnore
    protected List<String> _enum;

    /**
     * See http://json-schema.org/latest/json-schema-validation.html#anchor26
     */
    public Integer maxLength;
    /**
     * See http://json-schema.org/latest/json-schema-validation.html#anchor29
     */
    public Integer minLength;
    /**
     * See http://json-schema.org/latest/json-schema-validation.html#anchor33
     */
    public String pattern;
    /**
     * See http://json-schema.org/latest/json-schema-validation.html#anchor49
     */
    public Boolean uniqueItems;
    /**
     * See http://json-schema.org/latest/json-schema-validation.html#anchor14
     */
    public Number multipleOf;

    @JsonIgnore
    protected String defaultValue;

    public T property(Property property) {
        this.setProperty(property);
        return castThis();
    }

    public T type(String type) {
        this.setType(type);
        return castThis();
    }

    public T format(String format) {
        this.setFormat(format);
        return castThis();
    }

    public T description(String description) {
        this.setDescription(description);
        return castThis();
    }

    public T name(String name) {
        this.setName(name);
        return castThis();
    }

    public T required(boolean required) {
        this.setRequired(required);
        return castThis();
    }

    public T collectionFormat(String collectionFormat) {
        this.setCollectionFormat(collectionFormat);
        return castThis();
    }

    public T example(String example) {
        this.setExample(example);
        return castThis();
    }

    public T allowEmptyValue(Boolean allowEmpty) {
        this.setAllowEmptyValue(allowEmpty);
        return castThis();
    }

    public T readOnly(Boolean readOnly) {
        this.setReadOnly(readOnly);
        return castThis();
    }

    @JsonIgnore
    protected String getDefaultCollectionFormat() {
        return "csv";
    }

    public T items(Property items) {
        this.items = items;
        return castThis();
    }

    public T _enum(List<String> value) {
        this._enum = value;
        return castThis();
    }

    @JsonIgnore
    @Override
    public List<String> getEnum() {
        return _enum;
    }

    @Override
    public void setEnum(List<String> _enum) {
        this._enum = _enum;
    }

    @JsonProperty("enum")
    @Override
    public List<Object> getEnumValue() {
        if (_enum == null) return null;
        if (_enum.isEmpty()) return Collections.emptyList();
        List<Object> oList = new ArrayList<Object>(_enum.size());

        if (BaseIntegerProperty.TYPE.equals(type) || DecimalProperty.TYPE.equals(type)) {
            for (String s : _enum) {
                try {
                    if ("int32".equals(format)) {
                        oList.add(Integer.valueOf(s));
                    } else if ("int64".equals(format)) {
                        oList.add(Long.valueOf(s));
                    } else if ("double".equals(format)) {
                        oList.add(Double.valueOf(s));
                    } else if ("float".equals(format)) {
                        oList.add(Float.valueOf(s));
                    } else if (BaseIntegerProperty.TYPE.equals(type)) {
                        oList.add(Integer.valueOf(s));
                    } else if (DecimalProperty.TYPE.equals(type)) {
                        oList.add(Double.valueOf(s));
                    }
                } catch (NumberFormatException e) {
                    LOGGER.warn(String.format("Illegal enum value %s for parameter type %s", s, type), e);
                    oList.add(s);
                }
            }
        } else if ((type == null || "array".equals(type)) && items != null) {
            for (String s : _enum) {
                try {
                    if (items instanceof StringProperty) {
                        oList.add(s);
                    } else if (items instanceof IntegerProperty) {
                        oList.add(Integer.valueOf(s));
                    } else if (items instanceof LongProperty) {
                        oList.add(Long.valueOf(s));
                    } else if (items instanceof FloatProperty) {
                        oList.add(Float.valueOf(s));
                    } else if (items instanceof DoubleProperty) {
                        oList.add(Double.valueOf(s));
                    } else if (items instanceof BaseIntegerProperty) {
                        oList.add(Integer.valueOf(s));
                    } else if (items instanceof DecimalProperty) {
                        oList.add(Double.valueOf(s));
                    } else {
                        oList.add(s);
                    }
                } catch (NumberFormatException e) {
                    LOGGER.warn(String.format("Illegal enum value %s for parameter type %s", s, type), e);
                    oList.add(s);
                }
            }

        } else {
            for (String s: _enum) {
                oList.add(s);
            }
        }
        return oList;
    }

    @Override
    public void setEnumValue(List<?> enumValue) {
        if (enumValue == null) {
            this._enum = null;
            return;
        }
        if (enumValue.isEmpty()) {
            this._enum = Collections.emptyList();
            return;
        }
        List<String> sList = new ArrayList<String>(enumValue.size());
        for (Object item: enumValue) {
            sList.add(item.toString());
        }
        this._enum = sList;
    }

    @Override
    public Property getItems() {
        return items;
    }

    @Override
    public void setItems(Property items) {
        this.items = items;
    }

    @Override
    public String getFormat() {
        return format;
    }

    @Override
    public void setFormat(String format) {
        this.format = format;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public void setType(String type) {
        this.type = type;
        if(ArrayProperty.isType(type)) {
            if(getCollectionFormat() == null){
                setCollectionFormat(getDefaultCollectionFormat());
            }
        } else {
            setCollectionFormat(null);
        }
    }

    @Override
    public String getCollectionFormat() {
        return collectionFormat;
    }

    @Override
    public void setCollectionFormat(String collectionFormat) {
        this.collectionFormat = collectionFormat;
    }

    public void setProperty(Property property) {
        setType(property.getType());
        this.format = property.getFormat();
        if (property instanceof StringProperty) {
            final StringProperty string = (StringProperty) property;
            setEnum(string.getEnum());
        } else if (property instanceof IntegerProperty) {
            setEnumValue(((IntegerProperty) property).getEnum());
        } else if (property instanceof LongProperty) {
            setEnumValue(((LongProperty) property).getEnum());
        } else if (property instanceof FloatProperty) {
            setEnumValue(((FloatProperty) property).getEnum());
        } else if (property instanceof DoubleProperty) {
            setEnumValue(((DoubleProperty) property).getEnum());
        } else if (property instanceof ArrayProperty) {
            final ArrayProperty array = (ArrayProperty) property;
            setItems(array.getItems());
        }
    }

    public Object getDefaultValue() {
        if(defaultValue == null) {
            return null;
        }

        // don't return a default value if types fail to convert
        try {
            if ("integer".equals(this.type)) {
                return new Integer(defaultValue);
            }
            if ("number".equals(this.type)) {
                return new BigDecimal(defaultValue);
            }
        }
        catch (Exception e) {
            return null;
        }

        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public Object getDefault() {
        if (defaultValue == null || defaultValue.isEmpty()) {
            return null;
        }
        try {
            if (BaseIntegerProperty.TYPE.equals(type)) {
                return Long.valueOf(defaultValue);
            } else if (DecimalProperty.TYPE.equals(type)) {
                return Double.valueOf(defaultValue);
            } else if (BooleanProperty.TYPE.equals(type)) {
                if ("true".equalsIgnoreCase(defaultValue) || "false".equalsIgnoreCase(defaultValue)) {
                    return Boolean.valueOf(defaultValue);
                }
            }
        } catch (NumberFormatException e) {
            LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);
        }
        return defaultValue;
    }

    public void setDefault(Object defaultValue) {
        this.defaultValue = defaultValue == null ? null : defaultValue.toString();
    }

    @Override
    public void setExclusiveMaximum(Boolean exclusiveMaximum) {
        this.exclusiveMaximum = exclusiveMaximum;
    }

    @Override
    public BigDecimal getMaximum() {
        return maximum;
    }

    @Override
    public void setMaximum(BigDecimal maximum) {
        this.maximum = maximum;
    }

    @Override
    public Boolean isExclusiveMinimum() {
        return exclusiveMinimum;
    }

    @Override
    public void setExclusiveMinimum(Boolean exclusiveMinimum) {
        this.exclusiveMinimum = exclusiveMinimum;
    }

    @Override
    public BigDecimal getMinimum() {
        return minimum;
    }

    @Override
    public void setMinimum(BigDecimal minimum) {
        this.minimum = minimum;
    }

    @Override
    public Integer getMaxItems() {
        return maxItems;
    }

    @Override
    public void setMaxItems(Integer maxItems) {
        this.maxItems = maxItems;
    }

    @Override
    public Integer getMinItems() {
        return minItems;
    }

    @Override
    public void setMinItems(Integer minItems) {
        this.minItems = minItems;
    }

    @Override
    public Boolean getAllowEmptyValue() {
        return allowEmptyValue;
    }

    @Override
    public void setAllowEmptyValue(Boolean allowEmptyValue) {
        this.allowEmptyValue = allowEmptyValue;
    }

    @JsonProperty("x-example")
    public Object getExample() {
        /* 增加example.isEmpty()判断,修复Integer,BigDecimal,Long等类型,没有默认值example的报错问题
            if (example == null) {
                return null;
            }
        */
        if (example == null||example.isEmpty()) {
            return null;
        }
        try {
            if (BaseIntegerProperty.TYPE.equals(type)) {
                return Long.valueOf(example);
            } else if (DecimalProperty.TYPE.equals(type)) {
                return Double.valueOf(example);
            } else if (BooleanProperty.TYPE.equals(type)) {
                if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) {
                    return Boolean.valueOf(example);
                }
            }
        } catch (NumberFormatException e) {
            LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);
        }
        return example;
    }

    @Override
    public Integer getMaxLength() {
        return maxLength;
    }

    @Override
    public void setMaxLength(Integer maxLength) {
        this.maxLength = maxLength;
    }

    @Override
    public Integer getMinLength() {
        return minLength;
    }

    @Override
    public void setMinLength(Integer minLength) {
        this.minLength = minLength;
    }

    @Override
    public String getPattern() {
        return pattern;
    }

    @Override
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public Boolean isUniqueItems() {
        return uniqueItems;
    }

    @Override
    public void setUniqueItems(Boolean uniqueItems) {
        this.uniqueItems = uniqueItems;
    }

    @Override
    public Number getMultipleOf() {
        return multipleOf;
    }

    @Override
    public void setMultipleOf(Number multipleOf) {
        this.multipleOf = multipleOf;
    }

    @Override
    public Boolean isExclusiveMaximum() {
        return exclusiveMaximum;
    }

    public void setExample(String example) {
        this.example = example;
    }

    @JsonIgnore
    private T castThis() {
        @SuppressWarnings("unchecked")
        final T result = (T) this;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!super.equals(obj)) return false;
        if (getClass() != obj.getClass()) return false;
        AbstractSerializableParameter<?> other = (AbstractSerializableParameter<?>) obj;
        if (_enum == null) {
            if (other._enum != null) return false;
        } else if (!_enum.equals(other._enum)) return false;
        if (collectionFormat == null) {
            if (other.collectionFormat != null) return false;
        } else if (!collectionFormat.equals(other.collectionFormat)) return false;
        if (defaultValue == null) {
            if (other.defaultValue != null) return false;
        } else if (!defaultValue.equals(other.defaultValue)) return false;
        if (example == null) {
            if (other.example != null) return false;
        } else if (!example.equals(other.example)) return false;
        if (exclusiveMaximum == null) {
            if (other.exclusiveMaximum != null) return false;
        } else if (!exclusiveMaximum.equals(other.exclusiveMaximum)) return false;
        if (exclusiveMinimum == null) {
            if (other.exclusiveMinimum != null) return false;
        } else if (!exclusiveMinimum.equals(other.exclusiveMinimum)) return false;
        if (format == null) {
            if (other.format != null) return false;
        } else if (!format.equals(other.format)) return false;
        if (items == null) {
            if (other.items != null) return false;
        } else if (!items.equals(other.items)) return false;
        if (maxItems == null) {
            if (other.maxItems != null) return false;
        } else if (!maxItems.equals(other.maxItems)) return false;
        if (maxLength == null) {
            if (other.maxLength != null) return false;
        } else if (!maxLength.equals(other.maxLength)) return false;
        if (maximum == null) {
            if (other.maximum != null) return false;
        } else if (!maximum.equals(other.maximum)) return false;
        if (minItems == null) {
            if (other.minItems != null) return false;
        } else if (!minItems.equals(other.minItems)) return false;
        if (minLength == null) {
            if (other.minLength != null) return false;
        } else if (!minLength.equals(other.minLength)) return false;
        if (minimum == null) {
            if (other.minimum != null) return false;
        } else if (!minimum.equals(other.minimum)) return false;
        if (multipleOf == null) {
            if (other.multipleOf != null) return false;
        } else if (!multipleOf.equals(other.multipleOf)) return false;
        if (pattern == null) {
            if (other.pattern != null) return false;
        } else if (!pattern.equals(other.pattern)) return false;
        if (type == null) {
            if (other.type != null) return false;
        } else if (!type.equals(other.type)) return false;
        if (uniqueItems == null) {
            if (other.uniqueItems != null) {
                return false;
            }
        } else if (!uniqueItems.equals(other.uniqueItems)) {
            return false;
        }
        if (allowEmptyValue != null ? !allowEmptyValue.equals(other.allowEmptyValue) : other.allowEmptyValue != null) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((_enum == null) ? 0 : _enum.hashCode());
        result = prime * result + ((collectionFormat == null) ? 0 : collectionFormat.hashCode());
        result = prime * result + ((defaultValue == null) ? 0 : defaultValue.hashCode());
        result = prime * result + ((example == null) ? 0 : example.hashCode());
        result = prime * result + ((exclusiveMaximum == null) ? 0 : exclusiveMaximum.hashCode());
        result = prime * result + ((exclusiveMinimum == null) ? 0 : exclusiveMinimum.hashCode());
        result = prime * result + ((format == null) ? 0 : format.hashCode());
        result = prime * result + ((items == null) ? 0 : items.hashCode());
        result = prime * result + ((maxItems == null) ? 0 : maxItems.hashCode());
        result = prime * result + ((maxLength == null) ? 0 : maxLength.hashCode());
        result = prime * result + ((maximum == null) ? 0 : maximum.hashCode());
        result = prime * result + ((minItems == null) ? 0 : minItems.hashCode());
        result = prime * result + ((minLength == null) ? 0 : minLength.hashCode());
        result = prime * result + ((minimum == null) ? 0 : minimum.hashCode());
        result = prime * result + ((multipleOf == null) ? 0 : multipleOf.hashCode());
        result = prime * result + ((pattern == null) ? 0 : pattern.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        result = prime * result + ((uniqueItems == null) ? 0 : uniqueItems.hashCode());
        result = prime * result + (allowEmptyValue != null ? allowEmptyValue.hashCode() : 0);
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值