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

Updates with If-Unmodified-Since header return 412 but update the entity [DATAREST-1234]

Open
#1,593 0 comments 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

Stefan Fussenegger opened DATAREST-1234 and commented

Currently a request containing If-Unmodified-Since returns 412 Precondition Failed but the object state is updated. This can be verified by appending this code to example.springdata.rest.headers.WebIntegrationTests in spring-data-examples/rest/headers:

mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).//
    andExpect(status().isNotModified());

Thread.sleep(1000L);
mvc.perform(patch(uri).header(IF_UNMODIFIED_SINCE, response.getHeader(LAST_MODIFIED)).accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_UTF8).content("{\"firstname\": \"changed\"}")).//
                andExpect(status().isPreconditionFailed());

mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).//
    andExpect(status().isNotModified()); // fails with 200 OK

The reason this happens is that SDR ignores the header completely, updates the state accordingly and returns with 200 OK. Later a 412 Precondition Failed is generated by org.springframework.web.context.request.ServletWebRequest.checkNotModified(String, long) as the returned Last-Modified generated by the updated object is compared with If-Unmodified-Since.

Note that this behavior can only be observed for 200 OK response, i.e. if the response contains the body - which it does by default if an Accept header has been sent (see returnStatus == 200 in org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(Object, MethodParameter, ModelAndViewContainer, NativeWebRequest))


1 votes, 2 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.