Skip to content

Commit

Permalink
Handle UnmarshalException as Bad Request
Browse files Browse the repository at this point in the history
If UnmarshalException is thrown (which happens for example when the xml
is invalid)  then a WebApplicationException is thrown with status code
400 Bad Request.

This is also how resteasy-reactive-jackson handles invalid JSON.
  • Loading branch information
ketola committed Apr 2, 2024
1 parent 2f687bb commit eae4810
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ public void testXml() {
assertEquals(person.getLast(), secondPerson.getLast());
}

@Test
public void testInvalidXml() {
RestAssured
.with()
.body("<person><first>Bob</first></invalid>")
.contentType("application/xml")
.post("/simple/person")
.then()
.statusCode(400);
}

@Test
public void testLargeXmlPost() {
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Providers;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.UnmarshalException;
import jakarta.xml.bind.Unmarshaller;

import org.jboss.resteasy.reactive.common.util.StreamUtil;
Expand Down Expand Up @@ -73,6 +75,8 @@ protected Object unmarshal(InputStream entityStream, Class<Object> type) {
JAXBElement<Object> item = getUnmarshall(type)
.unmarshal(new StreamSource(entityStream), type);
return item.getValue();
} catch (UnmarshalException e) {
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit eae4810

Please sign in to comment.