Skip to content
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

BSON serialization problem for domain classes with a discriminator #159

Open
gorgiev opened this issue Nov 20, 2020 · 0 comments
Open

BSON serialization problem for domain classes with a discriminator #159

gorgiev opened this issue Nov 20, 2020 · 0 comments

Comments

@gorgiev
Copy link

gorgiev commented Nov 20, 2020

Upgrading an application from Grails 3.2.8 to Grails 4.0.5 (GORM 7.0.x) exposed a problem with domain classes mapped as a hierarchy: after a successful .save() on a domain instance, retrieving it back via .get() would result in a partially filled instance. A number of properties would not have their values loaded, and be left at null values. No exception or errors would be reported by GORM.

The base domain class in the application has "_class" persistent property defined for receiving discriminator values.

An inspection of the runtime GORM behavior shows that BsonPersistentEntityCodec writes out the discriminator "_class" property, and then does it again as a part of the persistent property set - see its encode method. The resulting class representation in persistent storage has two "_class" properties. This is possible because Mongo BSON allows storing duplicate properties.

On deserializing the domain class the discriminator-named property gets encountered twice. Processing the second occurence discards the partially resolved domain instance and creates a new instance to fill with property values - see decode method.

Removing the _class property definition from the domain class has the effect of it getting added/persisted by the codec as a dynamic attribute. The resulting BSON representation of the class still has two _class properties.

A workaround is to keep the _class property in the domain class, but define it as transient. This results in only the discriminator property getting serialized into BSON, with deserialization not getting re-started midway, and the domain instance getting loaded fully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant