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

Handle an empty query parameter with @Default #5534

Merged
merged 4 commits into from
Apr 4, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -753,12 +753,17 @@ private static AnnotatedValueResolver ofInjectableTypes0(String name, AnnotatedE

// Do not convert value here because the element type is String.
if (values != null && !values.isEmpty()) {
if (queryDelimiter != null && values.size() == 1) {
if (values.size() == 1) {
final String first = values.get(0);
Splitter.on(queryDelimiter)
.splitToStream(first)
.map(resolver::convert)
.forEach(resolvedValues::add);
if (first.isEmpty()) {
return resolvedValues;
}
if (queryDelimiter != null) {
Splitter.on(queryDelimiter)
.splitToStream(first)
.map(resolver::convert)
.forEach(resolvedValues::add);
}
} else {
values.stream().map(resolver::convert).forEach(resolvedValues::add);
}
Expand Down Expand Up @@ -1026,6 +1031,17 @@ private Object convert(@Nullable String value) {
if (value == null) {
return defaultOrException();
}
if (value.isEmpty()) {
logger.warn("Parameter is present but the value is missing: " + httpElementName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is an expected behavior, we can remove the warning.

if (String.class.isAssignableFrom(elementType)) {
return value;
}
if (elementType.isPrimitive()) {
throw new IllegalArgumentException("Cannot set null to primitive type parameter: " +
httpElementName);
}
return null;
}
return convert(value, elementType, enumConverter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Set;

/**
* Specifies the default value of an optional parameter.
Expand All @@ -35,7 +37,19 @@
* When {@link Default} annotation exists but {@link Default#value()} is not specified, {@code null}
* value would be set if the parameter is not present in the request.
*
* {@link Default} annotation is not allowed for a path variable. If a user uses {@link Default}
* <p>When {@link Default} annotation exists and {@link Default#value()} is not specified, but parameter is
* present in the request without the value e.g., {@code /foo?bar=}, then actions below will be processed
* depending on the type of the parameter.
facewise marked this conversation as resolved.
Show resolved Hide resolved
* <table>
* <caption>Actions</caption>
* <tr><th>Type</th><th>Action</th></tr>
* <tr><td>Primitive types</td><td>An {@link IllegalArgumentException} will be thrown</td></tr>
* <tr><td>{@link String}</td><td>An empty string {@code ""} will be set</td></tr>
* <tr><td>{@link List} or {@link Set}</td><td>An empty collection will be set</td></tr>
* <tr><td>Other</td><td>The {@code null} value will be set</td></tr>
* </table>
*
* <p>{@link Default} annotation is not allowed for a path variable. If a user uses {@link Default}
* annotation on a path variable, {@link IllegalArgumentException} would be raised.
*/
String value() default UNSPECIFIED;
Expand Down