Skip to content

Commit

Permalink
Ensure that Server Jackson providers are not included in the REST Client
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 23, 2023
1 parent 8226a56 commit 7d00e2f
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Set;

import javax.ws.rs.Priorities;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.core.MediaType;

import org.jboss.jandex.AnnotationInstance;
Expand Down Expand Up @@ -137,20 +138,22 @@ void additionalProviders(ContextResolversBuildItem contextResolversBuildItem,
new MessageBodyReaderBuildItem.Builder(ServerJacksonMessageBodyReader.class.getName(),
Object.class.getName())
.setMediaTypeStrings(HANDLED_MEDIA_TYPES)
.setBuiltin(true).build());
.setBuiltin(true).setRuntimeType(RuntimeType.SERVER).build());
additionalReaders
.produce(
new MessageBodyReaderBuildItem.Builder(VertxJsonArrayMessageBodyReader.class.getName(),
JsonArray.class.getName())
.setMediaTypeStrings(HANDLED_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.SERVER)
.build());
additionalReaders
.produce(
new MessageBodyReaderBuildItem.Builder(VertxJsonObjectMessageBodyReader.class.getName(),
JsonObject.class.getName())
.setMediaTypeStrings(HANDLED_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.SERVER)
.build());
additionalWriters
.produce(
Expand All @@ -160,20 +163,23 @@ void additionalProviders(ContextResolversBuildItem contextResolversBuildItem,
Object.class.getName())
.setMediaTypeStrings(HANDLED_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.SERVER)
.build());
additionalWriters
.produce(
new MessageBodyWriterBuildItem.Builder(VertxJsonArrayMessageBodyWriter.class.getName(),
JsonArray.class.getName())
.setMediaTypeStrings(HANDLED_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.SERVER)
.build());
additionalWriters
.produce(
new MessageBodyWriterBuildItem.Builder(VertxJsonObjectMessageBodyWriter.class.getName(),
JsonObject.class.getName())
.setMediaTypeStrings(HANDLED_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.SERVER)
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import org.apache.http.HttpStatus;
import org.jboss.resteasy.reactive.client.impl.MultiInvoker;
import org.jboss.resteasy.reactive.common.util.RestMediaType;
import org.jboss.resteasy.reactive.server.jackson.JacksonBasicMessageBodyReader;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.smallrye.mutiny.Multi;
Expand Down Expand Up @@ -137,7 +140,8 @@ public void testStreamJsonMultiFromMulti() {
}

private void testJsonMulti(String path) {
Client client = ClientBuilder.newBuilder().build();
Client client = ClientBuilder.newBuilder().register(new JacksonBasicMessageBodyReader(new ObjectMapper())).build();
;
WebTarget target = client.target(uri.toString() + path);
Multi<Message> multi = target.request().rx(MultiInvoker.class).get(Message.class);
List<Message> list = multi.collect().asList().await().atMost(Duration.ofSeconds(30));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.jboss.resteasy.reactive.server.jackson.JacksonBasicMessageBodyReader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.smallrye.mutiny.Multi;
Expand All @@ -34,7 +37,7 @@ public class MultiSseTest {
@Test
void shouldConsume() {
var resultList = new CopyOnWriteArrayList<>();
RestClientBuilder.newBuilder().baseUri(uri).build(SseClient.class)
createClient()
.get()
.subscribe().with(resultList::add);
await().atMost(5, TimeUnit.SECONDS)
Expand All @@ -45,7 +48,7 @@ void shouldConsume() {
@Test
void shouldConsumeJsonEntity() {
var resultList = new CopyOnWriteArrayList<>();
RestClientBuilder.newBuilder().baseUri(uri).build(SseClient.class)
createClient()
.getJson()
.subscribe().with(resultList::add);
await().atMost(5, TimeUnit.SECONDS)
Expand All @@ -56,7 +59,7 @@ void shouldConsumeJsonEntity() {
@Test
void shouldConsumeAsParametrizedType() {
var resultList = new CopyOnWriteArrayList<>();
RestClientBuilder.newBuilder().baseUri(uri).build(SseClient.class)
createClient()
.getJsonAsMap()
.subscribe().with(resultList::add);
await().atMost(5, TimeUnit.SECONDS)
Expand All @@ -68,7 +71,7 @@ void shouldConsumeAsParametrizedType() {
@Test
void shouldSendPayloadAndConsume() {
var resultList = new CopyOnWriteArrayList<>();
RestClientBuilder.newBuilder().baseUri(uri).build(SseClient.class)
createClient()
.post("test")
.subscribe().with(resultList::add);
await().atMost(5, TimeUnit.SECONDS)
Expand All @@ -79,7 +82,7 @@ void shouldSendPayloadAndConsume() {
@Test
void shouldSendPayloadAndConsumeAsParametrizedType() {
var resultList = new CopyOnWriteArrayList<>();
RestClientBuilder.newBuilder().baseUri(uri).build(SseClient.class)
createClient()
.postAndReadAsMap("test")
.subscribe().with(resultList::add);
await().atMost(5, TimeUnit.SECONDS)
Expand All @@ -90,6 +93,11 @@ void shouldSendPayloadAndConsumeAsParametrizedType() {
Map.of("name", "foo", "value", "test")));
}

private SseClient createClient() {
return RestClientBuilder.newBuilder().baseUri(uri).register(new JacksonBasicMessageBodyReader(new ObjectMapper()))
.build(SseClient.class);
}

@Path("/sse")
public interface SseClient {
@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class BasicRestClientTest {
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(HelloClient.class, HelloResource.class, TestBean.class, HelloClient2.class,
HelloNonSimpleClient.class))
HelloNonSimpleClient.class, TestJacksonBasicMessageBodyReader.class,
TestJacksonBasicMessageBodyWriter.class))
.withConfigurationResource("basic-test-application.properties");

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.smallrye.mutiny.Uni;
Expand All @@ -20,6 +21,8 @@
// https://github.com/quarkusio/quarkus/issues/21375
//
@RegisterRestClient(configKey = "hello2")
@RegisterProvider(TestJacksonBasicMessageBodyReader.class)
@RegisterProvider(TestJacksonBasicMessageBodyWriter.class)
public interface HelloNonSimpleClient {
@POST
@Consumes(MediaType.TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;
Expand All @@ -23,7 +24,7 @@ public class MediaTypeSuffixTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(HelloResource.class, Client.class))
.addClasses(HelloResource.class, Client.class, TestJacksonBasicMessageBodyReader.class))
.withConfigurationResource("media-type-suffix-application.properties");

@Test
Expand All @@ -37,6 +38,7 @@ public void test() {

@RegisterRestClient(configKey = "test")
@Path("/hello")
@RegisterProvider(TestJacksonBasicMessageBodyReader.class)
public interface Client {

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@

public class MultiNdjsonTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest();
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(TestJacksonBasicMessageBodyReader.class));

@TestHTTPResource
URI uri;

@Test
void shouldReadNdjsonStringAsMulti() throws InterruptedException {
var client = RestClientBuilder.newBuilder().baseUri(uri)
.build(Client.class);
var client = createClient(uri);
var collected = new CopyOnWriteArrayList<String>();
var completionLatch = new CountDownLatch(1);
client.readString().onCompletion().invoke(completionLatch::countDown)
Expand All @@ -60,8 +60,7 @@ void shouldReadNdjsonStringAsMulti() throws InterruptedException {

@Test
void shouldReadNdjsonPojoAsMulti() throws InterruptedException {
var client = RestClientBuilder.newBuilder().baseUri(uri)
.build(Client.class);
var client = createClient(uri);
var collected = new CopyOnWriteArrayList<Message>();
var completionLatch = new CountDownLatch(1);
client.readPojo().onCompletion().invoke(completionLatch::countDown)
Expand All @@ -79,8 +78,7 @@ void shouldReadNdjsonPojoAsMulti() throws InterruptedException {
@Test
void shouldReadNdjsonPojoFromReactiveRoutes() throws InterruptedException {
URI reactiveRoutesBaseUri = URI.create(uri.toString() + "/rr");
var client = RestClientBuilder.newBuilder().baseUri(reactiveRoutesBaseUri)
.build(Client.class);
var client = createClient(reactiveRoutesBaseUri);
var collected = new CopyOnWriteArrayList<Message>();
var completionLatch = new CountDownLatch(1);
client.readPojo().onCompletion().invoke(completionLatch::countDown)
Expand All @@ -96,8 +94,7 @@ void shouldReadNdjsonPojoFromReactiveRoutes() throws InterruptedException {

@Test
void shouldReadNdjsonFromSingleMessage() throws InterruptedException {
var client = RestClientBuilder.newBuilder().baseUri(uri)
.build(Client.class);
var client = createClient(uri);
var collected = new CopyOnWriteArrayList<Message>();
var completionLatch = new CountDownLatch(1);
client.readPojoSingle().onCompletion().invoke(completionLatch::countDown)
Expand All @@ -112,8 +109,14 @@ void shouldReadNdjsonFromSingleMessage() throws InterruptedException {
assertThat(collected).hasSize(4).containsAll(expected);
}

private Client createClient(URI uri) {
return RestClientBuilder.newBuilder().baseUri(uri).register(new TestJacksonBasicMessageBodyReader())
.build(Client.class);
}

@Path("/stream")
public interface Client {

@GET
@Path("/string")
@Produces(RestMediaType.APPLICATION_NDJSON)
Expand All @@ -131,9 +134,11 @@ public interface Client {
@Produces(RestMediaType.APPLICATION_NDJSON)
@RestStreamElementType(MediaType.APPLICATION_JSON)
Multi<Message> readPojoSingle();

}

public static class ReactiveRoutesResource {

@Route(path = "/rr/stream/pojo", produces = ReactiveRoutes.ND_JSON)
Multi<Message> people(RoutingContext context) {
return Multi.createFrom().items(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@

public class StreamJsonTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest();
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(TestJacksonBasicMessageBodyReader.class));

@TestHTTPResource
URI uri;

@Test
void shouldReadStreamJsonStringAsMulti() throws InterruptedException {
var client = RestClientBuilder.newBuilder().baseUri(uri)
.build(Client.class);
var client = createClient(uri);
var collected = new CopyOnWriteArrayList<String>();
var completionLatch = new CountDownLatch(1);
client.readString().onCompletion().invoke(completionLatch::countDown)
Expand All @@ -60,8 +60,7 @@ void shouldReadStreamJsonStringAsMulti() throws InterruptedException {

@Test
void shouldReadNdjsonPojoAsMulti() throws InterruptedException {
var client = RestClientBuilder.newBuilder().baseUri(uri)
.build(Client.class);
var client = createClient(uri);
var collected = new CopyOnWriteArrayList<Message>();
var completionLatch = new CountDownLatch(1);
client.readPojo().onCompletion().invoke(completionLatch::countDown)
Expand All @@ -79,8 +78,7 @@ void shouldReadNdjsonPojoAsMulti() throws InterruptedException {
@Test
void shouldReadNdjsonPojoFromReactiveRoutes() throws InterruptedException {
URI reactiveRoutesBaseUri = URI.create(uri.toString() + "/rr");
var client = RestClientBuilder.newBuilder().baseUri(reactiveRoutesBaseUri)
.build(Client.class);
var client = createClient(reactiveRoutesBaseUri);
var collected = new CopyOnWriteArrayList<Message>();
var completionLatch = new CountDownLatch(1);
client.readPojo().onCompletion().invoke(completionLatch::countDown)
Expand All @@ -96,8 +94,7 @@ void shouldReadNdjsonPojoFromReactiveRoutes() throws InterruptedException {

@Test
void shouldReadNdjsonFromSingleMessage() throws InterruptedException {
var client = RestClientBuilder.newBuilder().baseUri(uri)
.build(Client.class);
var client = createClient(uri);
var collected = new CopyOnWriteArrayList<Message>();
var completionLatch = new CountDownLatch(1);
client.readPojoSingle().onCompletion().invoke(completionLatch::countDown)
Expand All @@ -112,6 +109,11 @@ void shouldReadNdjsonFromSingleMessage() throws InterruptedException {
assertThat(collected).hasSize(4).containsAll(expected);
}

private Client createClient(URI uri) {
return RestClientBuilder.newBuilder().baseUri(uri).register(new TestJacksonBasicMessageBodyReader())
.build(Client.class);
}

@Path("/stream")
public interface Client {
@GET
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.rest.client.reactive;

import javax.ws.rs.Consumes;

import org.jboss.resteasy.reactive.server.jackson.JacksonBasicMessageBodyReader;

import com.fasterxml.jackson.databind.ObjectMapper;

@Consumes({ "application/json", "application/x-ndjson", "application/stream+json" })
public class TestJacksonBasicMessageBodyReader extends JacksonBasicMessageBodyReader {
public TestJacksonBasicMessageBodyReader() {
super(new ObjectMapper());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.rest.client.reactive;

import static org.jboss.resteasy.reactive.server.jackson.JacksonMessageBodyWriterUtil.doLegacyWrite;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

@Produces("application/json")
public class TestJacksonBasicMessageBodyWriter implements MessageBodyWriter<Object> {

private final ObjectWriter wWriter;

public TestJacksonBasicMessageBodyWriter() {
this.wWriter = new ObjectMapper().writer();
}

@Override
public final boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return true;
}

@Override
public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException,
WebApplicationException {
doLegacyWrite(o, annotations, httpHeaders, entityStream, wWriter);
}

}
Loading

0 comments on commit 7d00e2f

Please sign in to comment.