forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always use ClientJacksonMessageBodyReader over ServerJacksonMessageBody
After quarkusio#27203, we can customize the object mappers to be used by REST Client Reactive. However, because of quarkusio#16368, the implementation was never picked up when the resteasy reactive jackson extension is in place. I tried to remove the ResteasyReactiveJacksonProviderDefinedBuildItem build item and surprisingly everything kept working fine (I verified the test that was added as part of quarkusio#16368. Fix quarkusio#23979
- Loading branch information
Showing
6 changed files
with
147 additions
and
23 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
...eactive/jackson/deployment/processor/ResteasyReactiveJacksonProviderDefinedBuildItem.java
This file was deleted.
Oops, something went wrong.
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
141 changes: 141 additions & 0 deletions
141
...t/java/io/quarkus/rest/client/reactive/jackson/test/ClientWithCustomObjectMapperTest.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,141 @@ | ||
package io.quarkus.rest.client.reactive.jackson.test; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.okJson; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Objects; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.ext.ContextResolver; | ||
|
||
import org.jboss.resteasy.reactive.ClientWebApplicationException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
|
||
import io.quarkus.rest.client.reactive.runtime.RestClientBuilderImpl; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class ClientWithCustomObjectMapperTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest(); | ||
|
||
MyClient clientAllowsUnknown; | ||
MyClient clientDisallowsUnknown; | ||
WireMockServer wireMockServer; | ||
|
||
@BeforeEach | ||
public void setUp() throws MalformedURLException { | ||
wireMockServer = new WireMockServer(options().port(20001)); | ||
wireMockServer.start(); | ||
|
||
clientAllowsUnknown = new RestClientBuilderImpl() | ||
.baseUrl(new URL(wireMockServer.baseUrl())) | ||
.register(ClientObjectMapperUnknown.class) | ||
.build(MyClient.class); | ||
|
||
clientDisallowsUnknown = new RestClientBuilderImpl() | ||
.baseUrl(new URL(wireMockServer.baseUrl())) | ||
.register(ClientObjectMapperNoUnknown.class) | ||
.build(MyClient.class); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
wireMockServer.stop(); | ||
} | ||
|
||
@Test | ||
void testCustomObjectMappersShouldBeUsed() { | ||
var json = "{ \"value\": \"someValue\", \"secondValue\": \"toBeIgnored\" }"; | ||
wireMockServer.stubFor( | ||
WireMock.get(WireMock.urlMatching("/get")) | ||
.willReturn(okJson(json))); | ||
|
||
// FAIL_ON_UNKNOWN_PROPERTIES disabled | ||
assertThat(clientAllowsUnknown.get().await().indefinitely()) | ||
.isEqualTo(new Request("someValue")); | ||
|
||
// FAIL_ON_UNKNOWN_PROPERTIES enabled | ||
assertThatThrownBy(() -> clientDisallowsUnknown.get().await().indefinitely()) | ||
.isInstanceOf(ClientWebApplicationException.class); | ||
} | ||
|
||
@Path("/get") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public interface MyClient { | ||
@GET | ||
Uni<Request> get(); | ||
} | ||
|
||
public static class Request { | ||
private String value; | ||
|
||
public Request() { | ||
|
||
} | ||
|
||
public Request(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
Request request = (Request) o; | ||
return Objects.equals(value, request.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} | ||
|
||
public static class ClientObjectMapperUnknown implements ContextResolver<ObjectMapper> { | ||
@Override | ||
public ObjectMapper getContext(Class<?> type) { | ||
return new ObjectMapper() | ||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | ||
} | ||
} | ||
|
||
public static class ClientObjectMapperNoUnknown implements ContextResolver<ObjectMapper> { | ||
@Override | ||
public ObjectMapper getContext(Class<?> type) { | ||
return new ObjectMapper() | ||
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | ||
} | ||
} | ||
} |
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