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

Mongo Auditing: @CreatedDate field gets set to null on updates with Spring Data Rest [DATAREST-1204]

Open
#1,565 12 comments 7 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

Benjamin Cody opened DATAREST-1204 and commented

I've set up a Spring Boot application with spring-boot-starter-data-rest and spring-boot-starter-data-mongodb. I'm using the Mongo auditing feature to automatically set insert and update timestamps on my records using the @CreatedDate and @LastModifiedDate annotations.

I'm using the generated REST/HAL endpoint to insert and update records. When I insert a record, its insert and update timestamp both get set to the current time, as expected. However, when I perform an update, the insert timestamp get overwritten with null! :( The update timestamp gets updated correctly (as does the @Version annotated field).

This issue occurs when I have public getters/setters for my insert/update fields. When I remove those getters/setters, the fields get written as I would expect: the insert timestamp only gets written once on inserts and doesn't change on updates, and the update timestamp always gets written. With the @Version annotated field, it doesn't matter whether or not I have a public getter/setter: the version is never output by the REST endpoint and also cannot be set explicitly by the client.

I prepared a minimal project with a test which shows this scenario: https://github.com/bencody/spring_data_mongo_createddate


Affects: 2.6.10 (Ingalls SR10)

Reference URL: https://github.com/bencody/spring_data_mongo_createddate

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