Skip to content

Commit

Permalink
Throw better exception when REST Client receives invalid JSON
Browse files Browse the repository at this point in the history
Fixes: quarkusio#32710
(cherry picked from commit e542cc9)
  • Loading branch information
geoand authored and gsmet committed Apr 25, 2023
1 parent 07a5fa2 commit afcb317
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.quarkus.rest.client.reactive.jackson.test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

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

import io.quarkus.test.QuarkusUnitTest;

/**
* Tests that when the server responds with data that is not valid JSON, we return an internal server error
*/
public class InvalidJsonFromServerTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(JsonObject.class, JsonClient.class, InvalidJsonEndpoint.class));

@RestClient
JsonClient client;

@Test
public void test() {
assertThatThrownBy(() -> client.get())
.isInstanceOf(ClientWebApplicationException.class)
.hasMessageContaining("HTTP 200")
.cause()
.hasMessageContaining("was expecting double-quote to start field name");
}

@Path("/invalid-json")
@RegisterRestClient(baseUri = "http://localhost:8081")
public interface JsonClient {

@Produces(MediaType.APPLICATION_JSON)
@GET
JsonObject get();
}

static class JsonObject {
public String name;
}

@Path("/invalid-json")
@Produces(MediaType.APPLICATION_JSON)
public static class InvalidJsonEndpoint {

@GET
public String get() {
return "{name: test}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;

import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.ClientWebApplicationException;
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.server.jackson.JacksonBasicMessageBodyReader;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

public class ClientJacksonMessageBodyReader extends JacksonBasicMessageBodyReader implements ClientRestHandler {

private static final Logger log = Logger.getLogger(ClientJacksonMessageBodyReader.class);

private final ConcurrentMap<ObjectMapper, ObjectReader> contextResolverMap = new ConcurrentHashMap<>();
private RestClientRequestContext context;

Expand All @@ -39,8 +43,11 @@ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annota
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
try {
return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
} catch (JsonParseException e) {
log.debug("Server returned invalid json data", e);
throw new ClientWebApplicationException(e, Response.Status.OK);
} catch (StreamReadException | DatabindException e) {
throw new ClientWebApplicationException(e, Response.Status.BAD_REQUEST);
throw new ClientWebApplicationException(e, Response.Status.BAD_REQUEST); // TODO: we need to check if this actually makes sense...
}
}

Expand Down

0 comments on commit afcb317

Please sign in to comment.