Uploaded image for project: 'Spring Data MongoDB'
  1. Spring Data MongoDB
  2. DATAMONGO-1842

Optimistic locking allows ReactiveMongoTemplate to modify immutable objects

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0.2 (Kay SR2)
    • Fix Version/s: 2.1 RC1 (Lovelace)
    • Component/s: None
    • Labels:
      None
    • Environment:
      Ubuntu 17.10 64 bit OpenJDK 1.8 Kotlin 1.2

      Description

      Steps to reproduce

      A minimal working example is here:

      https://github.com/martin-drozdik/reactive-mongo-bug-demo

      Given an immutable class:

      data class Person
      (
          val id: String,
          @Version val version: Long? = null
      )
      

      It is nevertheless possible to modify its instances, just by calling `ReactiveMongoTemplate::save`

      @Component
      class PersonHandler(val template: ReactiveMongoTemplate)
      {
          fun initialize()
          {
              val jim = Person("Jim")
              require(jim.version == null)
      
              template.save<Person>(jim).block()
              require(jim.version == null) // This require will fail
              {
                  "Immutable object has been modified"
              }
          }
      }
      

      If this is by design, it could be beneficial to add a warning, or just a simple mention in the documentation. Otherwise I find it counterintuitive that a `save` method could break a fundamental principle of the language.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              olivergierke Oliver Drotbohm
              Reporter:
              martin-drozdik Martin Drozdik
              Last updater:
              Mark Paluch
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: