spring-projects / spring-projects/spring-data-rest
DomainObjectReader doMerge fails for writable @JsonAnySetter properties
@odrotbohm is already working on this.
Since Aug 12, 2024.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.