alex
alex

Reputation: 705

Spring Data REST updates null properties on PATCH (when it shouldn't)

I am sending a PATCH request with null values in some properties of the entity and I see that the fields are updated in the database whereas according to spec they shouldn't (partial update). Trying to understand what's going on I see that the DomainObjectMerger is instantiated as a @Bean but its merge method never used (no references found and in debug mode breakpoint is never fired). Could someone explain how and when is the DomainObjectMerger used?

EDIT: I created a sample project with a failing test. The test tries to PATCH an entity passing null as a password and expects the password to not have been affected. But it fails because the password is now null in the database

https://github.com/otinanism/demo-rest-data

Upvotes: 0

Views: 916

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83081

The code works as expected. Your PATCH payload looks like this:

{"id":"bc421109-edaf-4d4f-8d4c-71b62aa4d99f","username":"alex","password":null}

That's telling the server to wipe out the value for the password field. If you want to leave the password field untouched, make sure it's not even contained in the request payload, e.g. by configuring the ObjectMapper to not render null values.

Upvotes: 4

Related Questions