Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat MismatchedInputException as client error in RESTEasy Reactive #15305

Merged
merged 1 commit into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.resteasy.reactive.jackson.deployment.test;

import static org.hamcrest.CoreMatchers.containsString;

import java.util.function.Supplier;

import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand All @@ -27,6 +25,6 @@ public JavaArchive get() {
@Test
public void test() {
RestAssured.with().contentType("application/json").body("{\"name\": \"brie\"}").put("/fromage")
.then().statusCode(500).body(containsString("MismatchedInputException"));
.then().statusCode(400);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.reactive.common.util.EmptyInputStream;
import org.jboss.resteasy.reactive.server.providers.serialisers.json.AbstractJsonMessageBodyReader;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

public class JacksonMessageBodyReader extends AbstractJsonMessageBodyReader {

Expand All @@ -29,13 +31,18 @@ public JacksonMessageBodyReader(ObjectMapper mapper) {
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
return doReadFrom(type, genericType, entityStream);
throw new IllegalStateException("Should never be called");
}

@Override
public Object readFrom(Class<Object> type, Type genericType, MediaType mediaType, ServerRequestContext context)
throws WebApplicationException, IOException {
return doReadFrom(type, genericType, context.getInputStream());
try {
return doReadFrom(type, genericType, context.getInputStream());
} catch (MismatchedInputException e) {
context.abortWith(Response.status(Response.Status.BAD_REQUEST).build());
return null;
}
}

private Object doReadFrom(Class<Object> type, Type genericType, InputStream entityStream) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ public void restart(RuntimeResource target, boolean setLocatorTarget) {
this.target = target;
}

/**
* Meant to be used when a error occurred early in processing chain
*/
@Override
public void abortWith(Response response) {
setResult(response);
restart(getAbortHandlerChain());
// this is a valid action after suspend, in which case we must resume
if (isSuspended()) {
resume();
}
}

/**
* Resets the build time serialization assumptions. Called if a filter
* modifies the response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.reactive.common.core.ResteasyReactiveCallbackContext;

public interface ServerRequestContext extends ResteasyReactiveCallbackContext {
Expand All @@ -18,4 +19,6 @@ public interface ServerRequestContext extends ResteasyReactiveCallbackContext {
OutputStream getOrCreateOutputStream();

ResteasyReactiveResourceInfo getResteasyReactiveResourceInfo();

void abortWith(Response response);
}