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

Handle json deserialization exception #56

Closed
maheshrajamani opened this issue Jan 27, 2023 · 9 comments
Closed

Handle json deserialization exception #56

maheshrajamani opened this issue Jan 27, 2023 · 9 comments
Assignees

Comments

@maheshrajamani
Copy link
Contributor

Create exception mapper for JsonMappingException

@tatu-at-datastax
Copy link
Contributor

JsonMappingException is a subtype of JsonProcessingException; maybe add mapper of that type to handle all exceptions Jackson throws?

JsonMappingException is used to report data-binding level problems, its sibling type JsonParseException(both subtypes of JsonProcessingException) is used to indicate json-parsing (invalid JSON) problems.

@ivansenic
Copy link
Contributor

Possible duplicate of #87

@ivansenic
Copy link
Contributor

@maheshrajamani
Copy link
Contributor Author

I created this issue for 3 issues that was observed.

  • If command is not found api throws 404, but if there is serialization error it returns as error messages with 200. We need to fix this to always return 200 with error message.
  • Since these are application error message, imo don't need to return "WebApplicationException" in error message as "errors[0].message"

@ivansenic ivansenic self-assigned this Feb 14, 2023
@ivansenic
Copy link
Contributor

I was trying to test some other deserialization errors in order to see if we are covered now. I just hit that this:

      String json =
          """
          {
            "createNamespace": {
              "name": true
            }
          }
          """;

deserializes the name to the String. Was expecting to fail with wrong type or smth like this. @tatu-at-datastax any idea what's going on here?

@tatu-at-datastax
Copy link
Contributor

@ivansenic Jackson is quite permissive when target type is String: this conversion is indeed allowed and automatic by default. Same would be true for numbers.
(original rationale -- this is from Jackson 1.0 or before -- is to try support coercions that are safe -- there are users who really like looseness, and others who dislike it, difficult to find defaults everyone agrees with).

There are ways to configure stricter handling (added in 2.14.x) if we feel this is important to prevent, see:

(default behavior cannot be changed due to backwards-compatibility constraints)

@tatu-at-datastax
Copy link
Contributor

Change to ObjectMapper building would be something like (from PR test above):

   ObjectMapper mapper = JsonMapper.builder()
           // For logical target type of "Textual" (includes String)
            .withCoercionConfig(LogicalType.Textual, cfg ->
                    // fail conversion from source Shape of Boolean
                    cfg.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail))
            .build();

@ivansenic
Copy link
Contributor

@tatu-at-datastax I merged #119. If you think we need more stricter handling please open a new issue.

@tatu-at-datastax
Copy link
Contributor

@tatu-at-datastax I merged #119. If you think we need more stricter handling please open a new issue.

Thank you! I think we are good now (I don't see an immediate problem); will keep this in mind should there be something to make it an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants