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

PATCH overrides ReadOnlyProperty [DATAREST-1144]

Open
#1,507 5 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

status: feedback-provided type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Michael S opened DATAREST-1144 and commented

PATCHing overrides a read-only property, PUTting doesn't.

E.g. using the following entity:

@Entity
public class Person {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	private String firstName;
	private String lastName;
	
	@ReadOnlyProperty
	private String place;

	// snip
}

The property "place" gets set internally using

@HandleBeforeCreate
public void handleBeforeCreate(Person person) {
	if(person.getPlace() == null) {
		person.setPlace("The Shire");
	}
}

A PUT call using e.g. a json payload like the following results in an unchanged place:

{ "firstName" : "Frodo", "lastName" :"Baggins", "place" : "Mordor" } 

However PATCHing the same (or a partial) payload changes the place to the new value.

Please see the referenced quickstart that reproduces the problem.

I dug a bit into the issue and found out that during a PATCH request JsonPatchHandler calls DomainObjectMerger's read that omits the property checking whereas PUT calls readPut that does the property checks.

Note that this should also work using

@JsonProperty(access = Access.READ_ONLY) 

but this isn't tested in the referenced project since a bug (DATAREST-1006 got fixed some hours ago) is responsible for also overrides on PUT


Affects: 2.6.7 (Ingalls SR7)

Reference URL: https://github.com/msparer/gs-accessing-data-rest

1 votes, 3 watchers

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.