spring-projects / spring-projects/spring-data-rest

DomainObjectReader doMerge fails for writable @JsonAnySetter properties

Open
#2,407 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Aug 12, 2024.

status: waiting-for-triage
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Hi!

When using PATCH for objects that use @JsonAnySetter for deserialization and therefore don't have a corresponding property in MappedProperties, DomainObjectReader#doMerge fails. This used to work in previous versions of this class, when

if (!mappedProperties.hasPersistentPropertyForField(fieldName))

was used (without removing the field from deserialization, see changes in commit and commit ). Now

if (!mappedProperties.isWritableProperty(fieldName)) {

checks whether the property is writable (which is an improvement). But this also returns true if the class to merge contains an @JsonAnySetter. This means that the following call

PersistentProperty<?> property = mappedProperties.getPersistentProperty(fieldName);

returns null and the call after this

Optional<Object> rawValue = Optional.ofNullable(accessor.getProperty(property));

Throws the error.

The solution IMHO is a simple null check before the accessor.getProperty call like

if (property == null) {
  continue;
}

This lets @JsonAnySetter handle the further deserialization, as I understand was the intention in previous versions and should be now.

I confirmed this fix with a test case, see referenced pull request.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.