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

Rest Client @RestForm does not serialize enums like other parameter types #40167

Closed
krnhotwings opened this issue Apr 19, 2024 · 2 comments · Fixed by #40301
Closed

Rest Client @RestForm does not serialize enums like other parameter types #40167

krnhotwings opened this issue Apr 19, 2024 · 2 comments · Fixed by #40301
Labels
area/rest-client kind/bug Something isn't working
Milestone

Comments

@krnhotwings
Copy link

Describe the bug

If you create a custom enum, you can implement fromString to deserialize a string to its appropriate enum object. Conversely, you can override toString to serialize the enum to a custom string rather than the enum's name.

When implementing resource endpoints in RestEASY, deserialization is respected for @RestQuery and @RestForm where input strings are converted to appropriate enums. Serialization from enum to string is also respected when outputting an enum as plain text, in json, etc.

When implementing Rest Client endpoints that take an enum parameter, the underlying HTTP requests should contain the enum's toString serialization for query params, form params, etc.

Serialization is respected for Rest Client query params via @RestQuery.

However, serialization is not respected for Rest Client form params via @RestForm and exhibits behavior inconsistent to the other prior examples. Form params will use the enum's name rather than toString output.

Perhaps the code path that Client form params follow may be calling Enum#name() rather than toString?

Expected behavior

In form bodies, enums should be serialized by toString. For example, if you had an enum:

public enum MyEnum {
    BLAH("blah");

    private final String value;

    MyEnum(final String value) { ... }

    @Override
    public String toString() { 
        return value;
    }
}

A rest client endpoint that takes @RestForm MyEnum myEnum should pass the following to the HTTP request's form body:

myEnum=blah

Actual behavior

Form bodies will end up with the name representation of the enum:

myEnum=BLAH

How to Reproduce?

Demo repo is here:

https://github.com/krnhotwings/quarkus-rest-client-enum

Reproduction steps are in the README:

  1. Start server with quarkus dev
  2. Run curl commands

Expected output

curl -X GET "http://localhost:8080/resteasy?myEnum=test"
curl -X POST "http://localhost:8080/resteasy" -d "myEnum=test"
curl -X GET "http://localhost:8080/restclient?myEnum=test"

Note in the log output that the 3rd curl will make an HTTP request to httpbin with the lowercase enum serialization:

Request: GET https://httpbin.org/get?myEnum=test Headers[User-Agent=Quarkus REST Client], Empty body

Unexpected output

curl -X POST "http://localhost:8080/restclient" -d "myEnum=test"

However, form params will be uppercased:

Request: POST https://httpbin.org/post Headers[Content-Type=application/x-www-form-urlencoded User-Agent=Quarkus REST Client content-length=11], Body:
myEnum=TEST

Output of uname -a or ver

Linux 6.8.6-200.fc39.x86_64

Output of java -version

openjdk version "21.0.2" 2024-01-16

Quarkus version or git rev

3.9.4

Build tool (ie. output of mvnw --version or gradlew --version)

Gradle 8.6

Additional information

No response

@krnhotwings krnhotwings added the kind/bug Something isn't working label Apr 19, 2024
Copy link

quarkus-bot bot commented Apr 19, 2024

/cc @cescoffier (rest-client), @geoand (rest-client)

@krnhotwings
Copy link
Author

In light of this comment, I updated the demo repo and added a test case to cover @RestForm enums inside of @BeanParam:

curl -X POST "http://localhost:8080/restclient/bean" -d "myEnum=test"

which outputs the same uppercase enum name:

Request: POST https://httpbin.org/post Headers[Content-Type=application/x-www-form-urlencoded User-Agent=Quarkus REST Client content-length=11], Body:
myEnum=TEST

geoand added a commit to geoand/quarkus that referenced this issue Apr 26, 2024
geoand added a commit that referenced this issue Apr 26, 2024
Use toString for serializing enum form params in REST Client
@quarkus-bot quarkus-bot bot added this to the 3.11 - main milestone Apr 26, 2024
poldinik pushed a commit to poldinik/quarkus that referenced this issue Apr 29, 2024
@gsmet gsmet modified the milestones: 3.11 - main, 3.10.1 May 10, 2024
gsmet pushed a commit to gsmet/quarkus that referenced this issue May 10, 2024
holly-cummins pushed a commit to holly-cummins/quarkus that referenced this issue Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/rest-client kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants