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

JsonPatch test operation is testing against updated json node [DATAREST-769]

Open
#1,143 1 comment 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

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

Description

Mathias D opened DATAREST-769 and commented

The JsonPatchHandler is using the patchedNode for TestOperation. So it is checking against the result of the update. The test operation is a precondition - so it should be checked against the existingNode.

At the moment I cannot use the TestOperation to assert the old value of an attribute.

I wrote a simple test to reproduce the behaviour in org.springframework.data.rest.webmvc.jpa.JpaWebTests

@Test
	public void createThenPatchWithTest() throws Exception {

		Link peopleLink = client.discoverUnique("people");

		MockHttpServletResponse bilbo = postAndGet(peopleLink, "{ \"firstName\" : \"Bilbo\", \"lastName\" : \"Baggins\" }",
				MediaType.APPLICATION_JSON);

		Link bilboLink = client.assertHasLinkWithRel("self", bilbo);

		assertThat((String) JsonPath.read(bilbo.getContentAsString(), "$.firstName"), is("Bilbo"));
		assertThat((String) JsonPath.read(bilbo.getContentAsString(), "$.lastName"), is("Baggins"));
		String json = mapper.writeValueAsString(ImmutableList.of(
				ImmutableMap.builder()
						.put("op", "replace")
						.put("path", "/firstName")
						.put("value", "patched name").build(),
				ImmutableMap.builder()
						.put("op", "test")
						.put("path", "/firstName")
						.put("value", "Bilbo").build()
		));
		MockHttpServletResponse frodo = patchAndGet(bilboLink, json, RestMediaTypes.JSON_PATCH_JSON);

		assertThat((String) JsonPath.read(frodo.getContentAsString(), "$.firstName"), is("patched name"));
	}

At the moment the order of operations matters - it works if the replace operation is before the test operation.

I think the JsonPatchHandler should handle the test operations before the other operations


Affects: 2.4.2 (Gosling SR2)

2 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.