Skip to content
Open
Changes from all commits
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
16 changes: 15 additions & 1 deletion templates-v7/libraries/jersey3/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,24 @@ public class {{classname}} extends Service {
{{#hasQueryParams}}
//Add query params
Map<String, String> queryParams = new HashMap<>();

{{! Add first all required query params }}
{{#queryParams}}
if ({{{paramName}}} != null) {
{{#required}}
if ({{{paramName}}} == null) {
throw new IllegalArgumentException("Please provide the {{{paramName}}} query parameter");
}
queryParams.put("{{baseName}}", {{{paramName}}}{{^isString}}.toString(){{/isString}});
{{/required}}
{{/queryParams}}

{{! Then, add all the optional query params }}
{{#queryParams}}
{{^required}}
if ({{{paramName}}} != null) {
queryParams.put("{{baseName}}", {{{paramName}}}{{^isString}}.toString(){{/isString}});
}
{{/required}}
{{/queryParams}}
Comment on lines +74 to 91
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation iterates over the queryParams list twice: once for required parameters and once for optional ones. This can be optimized by combining these into a single loop. This refactoring will make the template more efficient and easier to read.

        {{! Process all query params }}
        {{#queryParams}}
        {{#required}}
        if ({{{paramName}}} == null) {
            throw new IllegalArgumentException("Please provide the {{{paramName}}} query parameter");
        }
        queryParams.put("{{baseName}}", {{{paramName}}}{{^isString}}.toString(){{/isString}});
        {{/required}}
        {{^required}}
        if ({{{paramName}}} != null) {
            queryParams.put("{{baseName}}", {{{paramName}}}{{^isString}}.toString(){{/isString}});
        }
        {{/required}}
        {{/queryParams}}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried this locally, so as expected, you loop once but you have required/optionals mixed (in the order they are declared). I don't have any preference on that, so I'm leaving for reviewers.


{{/hasQueryParams}}
Expand Down