-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Support java.util.Optional<T> passing to @QueryParam/@RestQuery for RestСlient #45672
Merged
+90
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ent/deployment/src/test/java/io/quarkus/rest/client/reactive/OptionalQueryParamsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.quarkus.rest.client.reactive; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.URI; | ||
import java.util.*; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.UriInfo; | ||
|
||
import org.jboss.resteasy.reactive.RestQuery; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
|
||
public class OptionalQueryParamsTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(Resource.class, Client.class)); | ||
|
||
@TestHTTPResource | ||
URI baseUri; | ||
|
||
@Test | ||
void testQueryParamsWithOptionals() { | ||
Client client = QuarkusRestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class); | ||
String responseForEmptyOptional = client.optional(Optional.empty()); | ||
assertThat(responseForEmptyOptional).isNullOrEmpty(); | ||
|
||
String responseForValueOptional = client.optional(Optional.of("value")); | ||
assertThat(responseForValueOptional).isEqualTo("parameter=value"); | ||
} | ||
|
||
@Path("optional") | ||
public static class Resource { | ||
|
||
@Path("query") | ||
@GET | ||
public String queryParams(@Context UriInfo uriInfo) { | ||
|
||
var entries = new ArrayList<String>(uriInfo.getQueryParameters().size()); | ||
for (var entry : uriInfo.getQueryParameters().entrySet()) { | ||
entries.add(entry.getKey() + "=" + String.join(",", entry.getValue())); | ||
} | ||
return String.join(";", entries); | ||
|
||
} | ||
} | ||
|
||
@Path("optional") | ||
public interface Client { | ||
|
||
@Path("query") | ||
@GET | ||
String optional(@RestQuery Optional<String> parameter); | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically the type argument of
Optional
could also be an array.EDIT: And a wildcard. Let's not get too wild with type variables :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, i am tried to understand these mechanisms for hours, but still having some troubles :)
Do you mean, that i can just drop
and assign
componentType
directly tocomponentType = paramType.name().toString();
and that's safe ?or i need to limit support of parameters type (exclude arrays and etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I honestly have no idea, I'm not familiar with this code at all. Just pointed out something that I saw while briefly going through this PR for no particular reason :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. At some point we might need to handle all the possible types, but we can keep that change for another day