Skip to content

ControllerAdvice class extending ResponseEntityExceptionHandler renders invalid exception json #31511

Closed as not planned
@Jenson3210

Description

@Jenson3210

Description

Affects: Spring framework 6

Expected behaviour:

A project that has a @ControllerAdvice annotated class that extends ResponseEntityExceptionHandler renders correct json of the exception it handles.

Actual behaviour:
When an exception occurs during serialization (jackson) of a field in ((open) projection) interfaces, there is an invalid json being created. First the object get serialized up till where the exception occured, that object is closed and then the object of the rfc7807 is serialized afterwards.


Reproduce

This bug is easily reproducible with a very limited sample codebase: https://github.com/Jenson3210/bug-demo

  • Run this codebase
  • Call endpoint localhost:8080/interface and see returns 2 objects iso 1 error object {"type":"interface","interfaceField":"field"} and {"type":"about:blank","title":"Internal Server Error","status":500,"detail":"Failed to write request","instance":"/interface"}
    This first object contains a type annotation derived from Jackson type annotation and a field which was succefully "calculated"
  • Call endpoint localhost:8080/projection and see it returns 2 objects instead of 1 error object {} and {"type":"about:blank","title":"Internal Server Error","status":500,"detail":"Failed to write request","instance":"/projection"}
  • Remove extends ResponseEntityExceptionHandler from ControllerExceptionHandler class. (or simply switch the commented lines)
  • See the endpoints now return proper json.

Possible solutions

The proxy for projections could be caching and checking on instantiation if all fields are retrievable. If resource-expensive operations need to be calculated, by caching they do not need to be recalculated by Jackson again. This is of course only a solution for the projections.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions