New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserializing java.util.Optional throws exception [DATACMNS-1501]
Comments
Joerg commented See also this discussion on stackoverflow https://stackoverflow.com/questions/53395198/spring-common-data-2-1-2-mongodb-cant-deserialise-optional-value |
Mark Paluch commented That's expected behavior in the first place. |
Joerg commented From a technical point of view I agree, the interface of This issue currently blocks us from updating to spring-boot 2.1 - is there any workaround you suggest to make our code work again? |
Mark Paluch commented There are a couple of aspect here:
We aren't convinced that However, one might want to use a domain-specific value type in the entity model. E.g. you might want to represent an Properly modeling a value type with factory methods is a common pattern. We might be able to store a value type but not deserialize it. In the scope of discussing value types, we might want to consider an approach how to interact with value types and solving |
Joerg commented Thanks for the explanation, I understand that there probably won't be a short term fix for that. Is there any workaround for that? Is it possible to register an own mapper for Optionals? |
Mark Paluch commented You can provide a @ReadingConverter
enum MyConverter implements Converter<org.bson.Document, Optional<String>>{
INSTANCE;
@Override
public Optional<String> convert(org.bson.Document document) {
return Optional.ofNullable((String) document.get("value"));
}
} Please note that this converter applies on a property/value type-basis (database value type to property type) and will affect all entities |
Dennis Doubleday commented Adding to this, I see the same error because my class is implementing org.springframework.data.domain.Auditable, so now I have to have Optional as part of the signature for Optional<T> getCreatedDate(); Shouldn't this work out of the box without a custom converter? |
I am also stuck with this issues, getting below exception when I try to read Optional value from document.
Is it possible to write converter for all |
spring-projects-issues commentedMar 21, 2019
Joerg opened DATACMNS-1501 and commented
After upgrading from SpringBoot 2.0.6 to SpringBoot 2.1.3 it's no longer possible deserialize data object that contain fields from type
Optional
.Example:
Serialization works fine (and looks same as in 2.0.6) - but when the data is read an exception is thrown:
Seems like it first creates the object using
new Optional()
and tries to set the value afterwards (which is impossible for an immutable object).I uploaded an example project to https://github.com/joemat/mongorepo-optional-fails
There are two branches:
master
(uses SpringBoot 2.1.3) test failspringboot-2.0.6
- test passAffects: 2.1.5 (Lovelace SR5)
Reference URL: https://github.com/joemat/mongorepo-optional-fails
The text was updated successfully, but these errors were encountered: