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

Conversation

facewise
Copy link
Contributor

Motivation:

Say we have an annotated service that uses @Default annotation to optionally bind query parameters.

class MyService {
    @Get("/list")
    public String list(@Default @Param Integer page) {
        ...
    }
}

It does not inject null if there is a parameter but no value. A NumberFormatException is raised when /list?page= is sent as the request path.

It would be better to solve this problem as follows the value of the parameter is empty.

Modifications:

  • Logger will warn that the parameter value is not present (It may be intended, such as sending an empty string as the parameter)
  • Inject an empty string "" if the type of the parameter is String
  • Inject null if the type of the parameter is not String such as Integer, Boolean and so on
  • Throw an IllegalArgumentException if the type of the parameter is primitive type
  • Inject an empty list [] if the type of the parameter is List or Set

Result:

  - if the type of the parameter is primitive, throw exception
  - if the type of the parameter is collection, empty collection
  - add test cases
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

The overall changes look nice.

@@ -86,7 +86,11 @@ class AnnotatedValueResolverTest {
static final Set<String> existingHttpParameters = ImmutableSet.of("param1",
"enum1",
"sensitive");

// These parameters will be present without the values. e.g., ?emptyParam1=&emptyParam2=&...
static final Set<String> existingWithoutValueParameters = ImmutableSet.of("emptyParam1",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also add an e2e test in which a client sends a query parameter with a value or an empty value? EmptyParameterWithDefaultAnnotationTest could be added for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Why not.

@@ -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.

@ikhoon ikhoon added this to the 1.28.0 milestone Mar 27, 2024
@facewise facewise requested a review from ikhoon March 27, 2024 06:51
Copy link

codecov bot commented Mar 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.08%. Comparing base (bc2454a) to head (d30a857).
Report is 33 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5534       +/-   ##
===========================================
+ Coverage        0   74.08%   +74.08%     
- Complexity      0    20982    +20982     
===========================================
  Files           0     1819     +1819     
  Lines           0    77315    +77315     
  Branches        0     9881     +9881     
===========================================
+ Hits            0    57282    +57282     
- Misses          0    15378    +15378     
- Partials        0     4655     +4655     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Nice work! 🚀 I can't believe that this is your first contribution. 😄

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

Changes look straightforward 👍 Thanks @facewise ! 🙇 👍 🙇

Co-authored-by: jrhee17 <guins_j@guins.org>
Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Great! Thanks!

@jrhee17 jrhee17 merged commit 66cfaa4 into line:main Apr 4, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle an empty query parameters with @Default
4 participants