Skip to content

ObjectMapper should not serialize to expected type on 400+ response code #335

@mdentremont

Description

@mdentremont

I'm using the latest Unirest, with the Jackson object mapper. I'm doing the following:

  • POST, using asObject(Response.class) to parse the response
  • I'm also using ifFailure(ErrorResponse.class, ...) to handle error responses

Expected:

  • On 400+ error responses Unirest should use ErrorResponse.class when trying to parse the response

Actual:

  • It first tries to serialize the response to Response.class
    • If that fails, it uses the response body when trying to parse the ErrorResponse.class
    • HOWEVER, if the parsing of a Response.class is successful, Unirest tries to coerce the Response.class to an ErrorResponse.class, and in my case, that leaves me with an empty ErrorResponse.class

We probably should go directly to using the error class on 400+ status codes.

It is also annoying that when using .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);, Jackson treats JSON which has completely different fields than my POJO as a successful de-serialization. I'd like to just instruct JSON to treat all my POJO properties as required by default, but that doesn't appear to be an option.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions