Skip to content

Fix issue where links are wrongly serialized when MappingJacksonValue is used as a return type - #2605

Open
ruthst00 wants to merge 2 commits into
spring-projects:mainfrom
ruthst00:issue/1544
Open

ruthst00 wants to merge 2 commits into
spring-projects:mainfrom
ruthst00:issue/1544

Conversation

@ruthst00

Copy link
Copy Markdown

Fixes #1544

Root cause: In Spring Framework 7, MappingJacksonValue is deprecated and marked for removal. The Jackson 3 (tools.jackson) message converters (AbstractJacksonHttpMessageConverter) explicitly throw UnsupportedOperationException when they receive a MappingJacksonValue body, and instead use a hints map (Map<String, Object>) keyed by class name. When a @RepositoryRestController method returns a MappingJacksonValue wrapping a RepresentationModel, the HAL converter never sees the inner model — causing _links to be absent from the response.

Fix: Introduced MappingJacksonValueResponseBodyAdvice — a ResponseBodyAdvice that:

  1. beforeBodyWrite() — detects MappingJacksonValue return values, unwraps the inner value (the RepresentationModel), and stashes any serializationView or FilterProvider hints in a ThreadLocal
  2. determineWriteHints() — a new Spring 7 ResponseBodyAdvice hook that returns the stashed hints as a Map<String, Object> for the Jackson 3 converter to consume, then clears the ThreadLocal

The advice is registered first in RepositoryRestMvcConfiguration.repositoryExporterHandlerAdapter() so it runs before HalFormsAdaptingResponseBodyAdvice. A warning is logged when a Jackson 2 FilterProvider is detected (it cannot be bridged to Jackson 3's incompatible tools.jackson.databind.ser.FilterProvider API). All 8 unit tests pass.

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

Signed-off-by: ruthes00 <ruthes00@gmail.com>
In Spring Framework 7, MappingJacksonValue is deprecated and no longer
supported by the Jackson 3 (tools.jackson) message converters. When a
@RepositoryRestController method returns a MappingJacksonValue, the HAL
converters receive the wrapper object rather than the inner
RepresentationModel, causing _links to be omitted from the response.

Introduce MappingJacksonValueResponseBodyAdvice, a ResponseBodyAdvice
that intercepts MappingJacksonValue return values before they reach the
message converter. The advice:

- Unwraps the inner value so the HAL converter serializes it correctly
- Propagates any serializationView as a Jackson 3 converter hint
- Logs a warning when a Jackson 2 FilterProvider is detected (it cannot
  be bridged to Jackson 3's tools.jackson.databind.ser.FilterProvider)

The advice is registered first in the repositoryExporterHandlerAdapter
response body advice chain so it runs before HalFormsAdaptingResponseBodyAdvice.

Closes spring-projects#1544

Signed-off-by: ruthes00 <ruthes00@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Links are wrongly serialized when MappingJacksonValue is used as a return type [DATAREST-1179]

2 participants