Skip to content

Commit

Permalink
Fix client jackson body writer to propagate the client context
Browse files Browse the repository at this point in the history
Fix #26152
  • Loading branch information
Sgitario committed Feb 26, 2023
1 parent 88cfc7d commit e05af28
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] anno

@Override
public void handle(RestClientRequestContext requestContext) throws Exception {
this.context = context;
this.context = requestContext;
}

protected ObjectWriter getEffectiveWriter() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.it.rest.client;

import static com.github.tomakehurst.wiremock.client.WireMock.ok;
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;
Expand All @@ -8,9 +9,11 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
Expand Down Expand Up @@ -40,6 +43,8 @@ public class ClientWithCustomObjectMapperTest {

@BeforeEach
public void setUp() throws MalformedURLException {
ClientObjectMapperUnknown.USED.set(false);
ClientObjectMapperNoUnknown.USED.set(false);
wireMockServer = new WireMockServer(options().port(20001));
wireMockServer.start();

Expand All @@ -60,10 +65,10 @@ public void tearDown() {
}

@Test
void testCustomObjectMappersShouldBeUsed() {
void testCustomObjectMappersShouldBeUsedInReader() {
var json = "{ \"value\": \"someValue\", \"secondValue\": \"toBeIgnored\" }";
wireMockServer.stubFor(
WireMock.get(WireMock.urlMatching("/get"))
WireMock.get(WireMock.urlMatching("/client"))
.willReturn(okJson(json)));

// FAIL_ON_UNKNOWN_PROPERTIES disabled
Expand All @@ -75,12 +80,25 @@ void testCustomObjectMappersShouldBeUsed() {
.isInstanceOf(ClientWebApplicationException.class);
}

@Path("/get")
@Test
void testCustomObjectMappersShouldBeUsedInWriter() {
wireMockServer.stubFor(
WireMock.post(WireMock.urlMatching("/client"))
.willReturn(ok()));

clientDisallowsUnknown.post(new Request());
assertThat(ClientObjectMapperNoUnknown.USED.get()).isTrue();
}

@Path("/client")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface MyClient {
@GET
Uni<Request> get();

@POST
void post(Request request);
}

public static class Request {
Expand Down Expand Up @@ -119,17 +137,24 @@ public int hashCode() {
}

public static class ClientObjectMapperUnknown implements ContextResolver<ObjectMapper> {
static final AtomicBoolean USED = new AtomicBoolean(false);

@Override
public ObjectMapper getContext(Class<?> type) {
USED.set(true);
return new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
}

public static class ClientObjectMapperNoUnknown implements ContextResolver<ObjectMapper> {

static final AtomicBoolean USED = new AtomicBoolean(false);

@Override
public ObjectMapper getContext(Class<?> type) {
USED.set(true);
return new ObjectMapper()
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
Expand Down

0 comments on commit e05af28

Please sign in to comment.