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 1672206 commit fc9615f
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 34 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 @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.ReactiveClientHeadersFactory;
import io.quarkus.rest.client.reactive.TestJacksonBasicMessageBodyReader;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.smallrye.common.annotation.Blocking;
Expand All @@ -46,7 +47,7 @@ public class ReactiveClientHeadersFromProviderTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(ReactiveClientHeadersFromProviderTest.Client.class)
.withApplicationRoot((jar) -> jar.addClasses(Client.class, TestJacksonBasicMessageBodyReader.class)
.addAsResource(
new StringAsset("my.property-value=" + HEADER_VALUE),
"application.properties"));
Expand Down Expand Up @@ -86,6 +87,7 @@ public Map<String, List<String>> returnHeaderValues(@Context HttpHeaders headers
@POST
public Map<String, List<String>> callClient(String uri) {
ReactiveClientHeadersFromProviderTest.Client client = RestClientBuilder.newBuilder().baseUri(URI.create(uri))
.register(new TestJacksonBasicMessageBodyReader())
.build(ReactiveClientHeadersFromProviderTest.Client.class);
return client.getWithHeader(DIRECT_HEADER_PARAM_VAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.jboss.resteasy.reactive.PartType;
import org.jboss.resteasy.reactive.RestForm;
import org.jboss.resteasy.reactive.multipart.FileUpload;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.TestJacksonBasicMessageBodyReader;
import io.quarkus.rest.client.reactive.TestJacksonBasicMessageBodyWriter;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.smallrye.mutiny.Multi;
Expand All @@ -33,7 +36,9 @@ public class MultipartDetectionTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar.addClasses(Resource.class, Client.class, Person.class));
.withApplicationRoot(
jar -> jar.addClasses(Resource.class, Client.class, Person.class, TestJacksonBasicMessageBodyReader.class,
TestJacksonBasicMessageBodyWriter.class));

@Test
void shouldCallExplicitEndpoints() throws IOException {
Expand Down Expand Up @@ -104,6 +109,8 @@ public String uploadMultipart(@RestForm String name) {
}

@Path("form")
@RegisterProvider(TestJacksonBasicMessageBodyReader.class)
@RegisterProvider(TestJacksonBasicMessageBodyWriter.class)
public interface Client {
@Path("multipart")
@POST
Expand Down
Loading

0 comments on commit fc9615f

Please sign in to comment.