question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ServerJacksonMessageBodyReader and JacksonBasicMessageBodyReader "should" catch JsonProcessingException

See original GitHub issue

Describe the bug

When de-serializing JSON in a request, we want to catch client input errors due to malformed data. Unfortunately, we have run into situations where the exception Jackson is throwing is one of several JsonProcessingExceptions, i.e., a JsonMappingException, JsonParseException or DatabindException. As a result, the intended BAD_REQUEST is met with a SERVER_ERROR.

Using ExceptionMappers can work, if we are diligent in catching JsonProcessingException’s in any location we use an ObjectMapper. If we are not diligent, then an ExceptionMapper will lead to a client error when it is actually a server error.

Corresponding locations: JacksonBasicMessageBodyReader

ServerJacksonMessageBodyReader

Updating these locations to catch JsonProcessingException should resolve the issues we are seeing.

Expected behavior

BAD_REQUEST is thrown when input is malformed.

Actual behavior

SERVER_ERROR is thrown.

How to Reproduce?

This is a simplified version of what we are doing, only the details causing the issue are included.

public class Data {
  private String type;

  @JsonCreator
   public Data(
      @JsonProperty("type") String type
      ...
   ) {
      this.type = Objects.requireNonNull(type);
   }
}

Deserialize with the following line of code (throws a ValueInstantiationException):

mapper.readValue("{}", Data.class)

When via a controller, i.e., we should get 400 if the request body is “{}”.

public Controller {

  @POST
  @Path("/")
  public void receive(Data data) {
  
  }
}

Output of uname -a or ver

Darwin Kernel Version 21.6.0: Thu Sep 29 20:12:57 PDT 2022; root:xnu-8020.240.7~1/RELEASE_X86_64

Output of java -version

17.0.3 (Eclipse Adoptium 17.0.3+7)

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.8.3-Final

Build tool (ie. output of mvnw --version or gradlew --version)

------------------------------------------------------------ Gradle 7.3 ------------------------------------------------------------ Build time: 2021-11-09 20:40:36 UTC Revision: 96754b8c44399658178a768ac764d727c2addb37 Kotlin: 1.5.31 Groovy: 3.0.9 Ant: Apache Ant™ version 1.10.11 compiled on July 10 2021 JVM: 17.0.3 (Eclipse Adoptium 17.0.3+7) OS: Mac OS X 12.6.1 x86_64

Additional information

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
damonsutherlandcommented, Dec 3, 2022

@geoand, my replacement PR has been linked above.

Thanks again for all your help.

1reaction
damonsutherlandcommented, Nov 18, 2022

Sure

Read more comments on GitHub >

github_iconTop Results From Across the Web

Additional JSON input validation on request #29548 - GitHub
ServerJacksonMessageBodyReader and JacksonBasicMessageBodyReader "should" catch JsonProcessingException #29316.
Read more >
How to get a JsonProcessingException using Jackson
I just figured there should be a way to produce that exception with some sort of random object that would not parse correctly...
Read more >
Catch JsonProcessingException from Jackson in Websphere ...
Hi everyone,. we are trying to write a custom javax.ws.rs.ext.ExceptionMapper to catch JsonProcessingExceptions thrown by the Liberty when someone posts ...
Read more >
quarkus - Github Plus
I received the error "Error: Classes that should be initialized at run time got ... and JacksonBasicMessageBodyReader "should" catch JsonProcessingException ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found