Skip to content

Commit

Permalink
Merge pull request #22021 from geoand/#21957
Browse files Browse the repository at this point in the history
Support media type suffixes in reactive rest client
  • Loading branch information
michalszynkiewicz committed Dec 8, 2021
2 parents 2b44967 + ee400a7 commit f200627
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.quarkus.rest.client.reactive;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.is;

import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class MediaTypeSuffixTest {

private static final String CUSTOM_JSON_MEDIA_TYPE = "application/vnd.search.v1+json";

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(HelloResource.class, Client.class))
.withConfigurationResource("media-type-suffix-application.properties");

@Test
public void test() {
when()
.get("/hello")
.then()
.statusCode(200)
.body("foo", is("bar"));
}

@RegisterRestClient(configKey = "test")
@Path("/hello")
public interface Client {

@GET
@Path("/custom")
@Produces(CUSTOM_JSON_MEDIA_TYPE)
Map<String, Object> test();
}

@Path("/hello")
public static class HelloResource {

private final Client client;

public HelloResource(@RestClient Client client) {
this.client = client;
}

@GET
@Path("/custom")
@Produces(CUSTOM_JSON_MEDIA_TYPE)
public Map<String, Object> hello() {
return Map.of("foo", "bar");
}

@GET
public Map<String, Object> test() {
return client.test();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.rest-client.test.url=http://localhost:${quarkus.http.test-port:8081}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jboss.resteasy.reactive.common.core.Serialisers;
import org.jboss.resteasy.reactive.common.jaxrs.ConfigurationImpl;
import org.jboss.resteasy.reactive.common.util.CaseInsensitiveMap;
import org.jboss.resteasy.reactive.common.util.MediaTypeHelper;

public class ClientReaderInterceptorContextImpl extends AbstractClientInterceptorContextImpl
implements ReaderInterceptorContext {
Expand All @@ -34,7 +35,7 @@ public ClientReaderInterceptorContextImpl(Annotation[] annotations, Class<?> ent
Map<String, Object> properties, MultivaluedMap<String, String> headers,
ConfigurationImpl configuration, Serialisers serialisers, InputStream inputStream,
ReaderInterceptor[] interceptors) {
super(annotations, entityClass, entityType, mediaType, properties);
super(annotations, entityClass, entityType, MediaTypeHelper.withSuffixAsSubtype(mediaType), properties);
this.configuration = configuration;
this.serialisers = serialisers;
this.inputStream = inputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ public static boolean isUnsupportedWildcardSubtype(MediaType mediaType) {
* that uses the suffix as the subtype
*/
public static MediaType withSuffixAsSubtype(MediaType mediaType) {
if (mediaType == null) {
return null;
}
int plusIndex = mediaType.getSubtype().indexOf('+');
if ((plusIndex > -1) && (plusIndex < mediaType.getSubtype().length() - 1)) {
mediaType = new MediaType(mediaType.getType(),
Expand Down

0 comments on commit f200627

Please sign in to comment.