Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert empty string to empty collection or array #54

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,36 @@ private ConverterUtils() {

static {
STRING_CONVERTER_MAP.put(Byte.class, v -> StringUtils.isEmpty(v) ? null : Byte.valueOf(v));
STRING_CONVERTER_MAP.put(byte.class, Byte::parseByte);
STRING_CONVERTER_MAP.put(byte.class, v -> v == null ? null : Byte.parseByte(v));

STRING_CONVERTER_MAP.put(Character.class, v -> StringUtils.isEmpty(v)
? null : v.length() > 0 ? v.charAt(0) : v);
STRING_CONVERTER_MAP.put(char.class, v -> v.length() >= 1 ? v.charAt(0) : v);
? null : v.charAt(0));
STRING_CONVERTER_MAP.put(char.class, v -> v == null ? null : v.length() >= 1 ? v.charAt(0) : v);
Mkabaka marked this conversation as resolved.
Show resolved Hide resolved

STRING_CONVERTER_MAP.put(Boolean.class, v -> StringUtils.isEmpty(v)
? null : (Boolean.parseBoolean(v) || "1".equals(v)) ? Boolean.TRUE : Boolean.FALSE);
STRING_CONVERTER_MAP.put(boolean.class, v -> (Boolean.parseBoolean(v) || "1".equals(v)));
STRING_CONVERTER_MAP.put(boolean.class, v -> v == null ? null : (Boolean.parseBoolean(v) || "1".equals(v)));

STRING_CONVERTER_MAP.put(Short.class, v -> StringUtils.isEmpty(v) ? null : Short.valueOf(v));
STRING_CONVERTER_MAP.put(short.class, Short::parseShort);
STRING_CONVERTER_MAP.put(short.class, v -> v == null ? null : Short.parseShort(v));

STRING_CONVERTER_MAP.put(Integer.class, v -> StringUtils.isEmpty(v) ? null : Integer.valueOf(v));
STRING_CONVERTER_MAP.put(int.class, Integer::parseInt);
STRING_CONVERTER_MAP.put(int.class, v -> v == null ? null : Integer.parseInt(v));

STRING_CONVERTER_MAP.put(Long.class, v -> StringUtils.isEmpty(v) ? null : Long.valueOf(v));
STRING_CONVERTER_MAP.put(long.class, Long::parseLong);
STRING_CONVERTER_MAP.put(long.class, v -> v == null ? null : Long.parseLong(v));

STRING_CONVERTER_MAP.put(Double.class, v -> StringUtils.isEmpty(v) ? null : Double.valueOf(v));
STRING_CONVERTER_MAP.put(double.class, Double::parseDouble);
STRING_CONVERTER_MAP.put(double.class, v -> v == null ? null : Double.parseDouble(v));

STRING_CONVERTER_MAP.put(Float.class, v -> StringUtils.isEmpty(v) ? null : Float.valueOf(v));
STRING_CONVERTER_MAP.put(float.class, Float::parseFloat);
STRING_CONVERTER_MAP.put(float.class, v -> v == null ? null : Float.parseFloat(v));

STRING_CONVERTER_MAP.put(Void.class, v -> null);
STRING_CONVERTER_MAP.put(void.class, v -> null);

STRING_CONVERTER_MAP.put(BigDecimal.class, v -> StringUtils.isEmpty(v)
? null : BigDecimal.valueOf(Double.parseDouble(v)));
? null : new BigDecimal(v));

// convert a empty string to a null is not supported
STRING_CONVERTER_MAP.put(Timestamp.class, Timestamp::valueOf);
Expand Down Expand Up @@ -173,12 +173,7 @@ public static Function<String, Object> str2ObjectConverter(Type requiredType, Fu
if (str2Object == null) {
return def;
}
return p -> {
if (p == null) {
return null2ObjectConverter(requiredClass);
}
return str2Object.apply(p);
};
return str2Object;
}

/**
Expand All @@ -194,19 +189,8 @@ public static Function<Collection<String>, Object> strs2ObjectConverter(Type req
if (strs2Object == null) {
return null;
}
return p -> {
if (p == null) {
return null2ObjectConverter(requiredClass);
}
return strs2Object.apply(p);
};
}

private static Object null2ObjectConverter(Class<?> requiredType) {
if (Optional.class.isAssignableFrom(requiredType)) {
return Optional.empty();
}
return null;
return strs2Object;
}

private static Function<Collection<String>, Object> strs2ObjectConverter(Class<?> requiredClass,
Expand Down Expand Up @@ -256,7 +240,7 @@ private static Str2OptionalConverter of(Type requiredType) {

@Override
public Object apply(String s) {
return Optional.ofNullable(elementConverter.apply(s));
return s == null ? Optional.empty() : Optional.ofNullable(elementConverter.apply(s));
}
}

Expand Down Expand Up @@ -285,6 +269,9 @@ private static Strs2ArrayConverter of(Class<?> requiredClass) {

@Override
public Object apply(Collection<String> value) {
if (value == null) {
return null;
}
final Object array = Array.newInstance(elementType, value.size());

int i = 0;
Expand Down Expand Up @@ -317,7 +304,7 @@ private static Str2ArrayConverter of(Class<?> requiredClass) {

@Override
public Object apply(String value) {
return strList2ArrayConverter.apply(extractStringFields(value));
return value == null ? null : strList2ArrayConverter.apply(extractStringFields(value));
}
}

Expand Down Expand Up @@ -357,6 +344,9 @@ private static Strs2CollectionConverter of(Class<?> requiredClass, Type required
@SuppressWarnings("unchecked")
@Override
public Object apply(Collection<String> value) {
if (value == null) {
return null;
}
final Collection collection = collectionGenerator.apply(value.size());
for (String v : value) {
// convert to target type and fill in the collection.
Expand Down Expand Up @@ -389,7 +379,7 @@ private static Str2CollectionConverter of(Class<?> requiredClass, Type requiredT

@Override
public Object apply(String value) {
return strs2CollectionConverter.apply(extractStringFields(value));
return value == null ? null : strs2CollectionConverter.apply(extractStringFields(value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,20 @@ private NameAndValue updateNamedValueInfo(Param param, NameAndValue nav) {
"] not available, and parameter name information not found in class file either.");
}
}
Object defaultValue = null;
boolean hasDefaultValue = false;
Object defaultValue;
boolean hasDefaultValue;
if (nav.hasDefaultValue) {
defaultValue = nav.defaultValue;
hasDefaultValue = true;
} else if (!nav.required && (useObjectDefaultValueIfRequired(param, nav))) {
defaultValue = defaultValue(param.type());
hasDefaultValue = true;
} else if (Optional.class.isAssignableFrom(param.type())) {
} else if (Optional.class.equals(param.type())) {
defaultValue = Optional.empty();
hasDefaultValue = true;
} else {
hasDefaultValue = false;
defaultValue = null;
}

if (defaultValue instanceof String && !param.type().isInstance(defaultValue)) {
Expand All @@ -114,7 +117,7 @@ private NameAndValue updateNamedValueInfo(Param param, NameAndValue nav) {
}

private static Object defaultValue(Class<?> type) {
if (Optional.class.isAssignableFrom(type)) {
if (Optional.class.equals(type)) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ public class NameAndValue {
public final Object defaultValue;
public final boolean hasDefaultValue;

public NameAndValue(String name, boolean required, Object defaultValue, boolean hasDefaultValue) {
this.name = name;
this.required = required;
this.defaultValue = defaultValue;
this.hasDefaultValue = hasDefaultValue;
public NameAndValue(String name, boolean required) {
this(name, required, null);
}

public NameAndValue(String name, boolean required, Object defaultValue) {
this(name, required, defaultValue, defaultValue != null);
}

public NameAndValue(String name, boolean required) {
this(name, required, null);
public NameAndValue(String name, boolean required, Object defaultValue, boolean hasDefaultValue) {
this.name = name;
this.required = required;
this.defaultValue = defaultValue;
this.hasDefaultValue = hasDefaultValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ protected NameAndValue createNameAndValue(Param parameter) {
assert cookieParam != null;
return new NameAndValue(cookieParam.value(),
false,
JaxrsMappingUtils.extractDefaultValue(parameter),
JaxrsMappingUtils.hasDefaultValue(parameter));
JaxrsMappingUtils.extractDefaultValue(parameter));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ protected NameAndValue createNameAndValue(Param parameter) {
assert formParam != null;
return new NameAndValue(formParam.value(),
false,
JaxrsMappingUtils.extractDefaultValue(parameter),
JaxrsMappingUtils.hasDefaultValue(parameter));
JaxrsMappingUtils.extractDefaultValue(parameter));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ protected NameAndValue createNameAndValue(Param param) {
assert headerParam != null;
return new NameAndValue(headerParam.value(),
false,
JaxrsMappingUtils.extractDefaultValue(param),
JaxrsMappingUtils.hasDefaultValue(param));
JaxrsMappingUtils.extractDefaultValue(param));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ protected NameAndValue createNameAndValue(Param parameter) {
parameter.getAnnotation(MatrixParam.class);
assert matrixParam != null;
return new NameAndValue(matrixParam.value(), false,
JaxrsMappingUtils.extractDefaultValue(parameter),
JaxrsMappingUtils.hasDefaultValue(parameter));
JaxrsMappingUtils.extractDefaultValue(parameter));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ protected NameAndValue createNameAndValue(Param parameter) {
parameter.getAnnotation(PathParam.class);
assert pathParam != null;
return new NameAndValue(pathParam.value(), false,
JaxrsMappingUtils.extractDefaultValue(parameter),
JaxrsMappingUtils.hasDefaultValue(parameter));
JaxrsMappingUtils.extractDefaultValue(parameter));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ protected NameAndValue createNameAndValue(Param parameter) {
assert queryParam != null;
return new NameAndValue(queryParam.value(),
false,
JaxrsMappingUtils.extractDefaultValue(parameter),
JaxrsMappingUtils.hasDefaultValue(parameter));
JaxrsMappingUtils.extractDefaultValue(parameter));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public boolean supports(Param param) {

@Override
protected NameAndValue createNameAndValue(Param param) {
return new NameAndValue(param.name(), false, JaxrsMappingUtils.extractDefaultValue(param),
JaxrsMappingUtils.hasDefaultValue(param));
return new NameAndValue(param.name(), false, JaxrsMappingUtils.extractDefaultValue(param));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ protected boolean supports0(Param param) {

@Override
protected NameAndValue createNameAndValue(Param param) {
return new NameAndValue(param.name(), false, JaxrsMappingUtils.extractDefaultValue(param),
JaxrsMappingUtils.hasDefaultValue(param));
return new NameAndValue(param.name(), false, JaxrsMappingUtils.extractDefaultValue(param));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public final class JaxrsMappingUtils {
private JaxrsMappingUtils() {
}

public static boolean hasDefaultValue(Param param) {
return param != null && param.getAnnotation(DefaultValue.class) != null;
}

/**
* Extracts the default value from given {@link MethodParam} which may be annotated by the JAX-RS annotation {@link
* DefaultValue}.
Expand Down