Skip to content

Commit

Permalink
Properly segregate Jackson providers for server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 24, 2023
1 parent 8226a56 commit 11054ec
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 42 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 @@ -7,6 +7,7 @@
import java.util.Collections;
import java.util.List;

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

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
Expand Down Expand Up @@ -42,9 +43,6 @@ void additionalProviders(
BuildProducer<AdditionalBeanBuildItem> additionalBean,
BuildProducer<MessageBodyReaderBuildItem> additionalReaders,
BuildProducer<MessageBodyWriterBuildItem> additionalWriters) {
if (!jacksonProviderDefined.isEmpty()) {
return;
}
// make these beans to they can get instantiated with the Quarkus CDI configured Jsonb object
additionalBean.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(ClientJacksonMessageBodyReader.class.getName())
Expand All @@ -57,41 +55,47 @@ void additionalProviders(
Object.class.getName())
.setMediaTypeStrings(HANDLED_READ_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.CLIENT)
.build());
additionalReaders
.produce(
new MessageBodyReaderBuildItem.Builder(VertxJsonArrayBasicMessageBodyReader.class.getName(),
JsonArray.class.getName())
.setMediaTypeStrings(HANDLED_READ_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.CLIENT)
.build());
additionalReaders
.produce(
new MessageBodyReaderBuildItem.Builder(VertxJsonObjectBasicMessageBodyReader.class.getName(),
JsonObject.class.getName())
.setMediaTypeStrings(HANDLED_READ_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.CLIENT)
.build());
additionalWriters
.produce(
new MessageBodyWriterBuildItem.Builder(ClientJacksonMessageBodyWriter.class.getName(),
Object.class.getName())
.setMediaTypeStrings(HANDLED_WRITE_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.CLIENT)
.build());
additionalWriters
.produce(
new MessageBodyWriterBuildItem.Builder(VertxJsonArrayBasicMessageBodyWriter.class.getName(),
JsonArray.class.getName())
.setMediaTypeStrings(HANDLED_WRITE_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.CLIENT)
.build());
additionalWriters
.produce(
new MessageBodyWriterBuildItem.Builder(VertxJsonObjectBasicMessageBodyWriter.class.getName(),
JsonObject.class.getName())
.setMediaTypeStrings(HANDLED_WRITE_MEDIA_TYPES)
.setBuiltin(true)
.setRuntimeType(RuntimeType.CLIENT)
.build());
}
}
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
Loading

0 comments on commit 11054ec

Please sign in to comment.