Embedded object saved partially when some fields are unset
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
When I change an embedded object to another instance of the same class but with some fields which have no values, those fields are not updated.
For example with a class Position and a field address with several fields as described below :
```groovy
class Position {
Address address
static embedded = ['address']
static constraints = {
address nullable: true
}
}
class Address {
String streetAddress1
String streetAddress2
String streetAddress3
String postalCode
String city
CountryCode countryCode
String formattedAddress
static mapping = {
// No versioning, address is only a embedded object
version false
}
}
```
If I have a position with an address with city = Paris and country = FR, when I want to update the position and I use et new address with just country = ES in mongodb I see an address with city = Paris and country = ES.
I should have an address with just country = ES and no city.
When I try to debug my application I see that the
org.grails.datastore.mapping.mongo.engine.codecs.PersistentEntityCodec doesn't manage correctly the embedded use case because encodeEmbeddedUpdate call encodeUpdate with a value but without the embedded set to true. This issue cause the property with null value to be ignored instead of being set to null.
**Workaround** : to ensure the correct behavior of the udpate I changed my service to markDirty all the properties of my address class. It's no really cleaned but I didn't any other solution.
**Context** :
Grails : 4.0.0
GORM : 7.0.0.RELEASE
Groovy : 2.5.6
Contributor guide
Research direction
Start with org.grails.datastore.mapping.mongo.engine.codecs.PersistentEntityCodec and inspect encodeEmbeddedUpdate and its call to encodeUpdate. Reproduce the Position and Address update described in the issue, then verify that unset embedded fields are written as null rather than retained; the workaround of marking all Address properties dirty provides a comparison case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy, mongodb
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100