4

Spring Common Data 2.1.2 (and Data MongoDB 2.1.2) cannot deserialise Optional field from my class. It worked with previous version (2.0.8), now my object is saved in DB (with MongoRepository.save), but there is an exception when i try to read it (MongoRepository.findById). I've narrowed down an exact field and the problem is with storing Optional:

java.lang.UnsupportedOperationException: Cannot set immutable property java.util.Optional.value!
    at org.springframework.data.mapping.model.BeanWrapper.setProperty(BeanWrapper.java:86) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:61) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELE

and my class:

public class Coordinates { private double latitude; private double longitude; private Optional altitude; }

there is an exception when data in DB is in a form:

altitude: Object
   value: 0

and no error when altitude doesn't have a 'value'

altitude: Object

(no value)

It worked with previous Spring Boot version (and Spring Data Commons/Mongodb 2.0.8).

I can't find any information about changed behaviour/different handling of Optionals etc.

How to solve it ?

Should i keep it as

private Double altitude;

with getter:

public Optional<Double> getAltitude() {
    return Optional.ofNullable(altitude);
}

or any other way ?

razor
  • 2,727
  • 6
  • 33
  • 45
  • 1
    I created a JIRA ticket for that https://jira.spring.io/browse/DATACMNS-1501 – joemat Mar 21 '19 at 14:58
  • Thanks, however my code probably was not good enough, i shouldn't have Optional properties in my class – razor Apr 02 '19 at 09:47

0 Answers0