GrailsWebDataBinder is not binding lists of children objects when reading xml
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
This issue is related to this one closed : #10028
Using grailsWebDataBinder.bind(obj, NodeChild) will work only with simple nodes,
but do not process any list of nodes in a context where the obj has a one-to-many relationship.
The problem comes from
addElementsToCollections(...) {
...
propertyValue[otherSide.name] = obj (line 579)
...
}
'propertyValue' is in this case a GPathResultMap and when adding an Object ( put() ) it will throw a 'UnsupportedOperationException' that sadly is going to be silenced later by a mute try/catch.
### Steps to Reproduce
------ File grails-app/domain/Documento.groovy
```
package testapp
class Documento implements Serializable {
Date dateCreated
String titolo
Integer numeroItems
List nomi
static hasMany = [righe: DocumentoRiga]
// Collection righe
static mapping = {
version false
}
}
```
------ File grails-app/domain/DocumentoRiga.groovy
```
package testapp
class DocumentoRiga implements Serializable {
int ord
String prodotto
Date data
static belongsTo = [documento: Documento]
static mapping = {
id composite: ['documento', 'ord'], generator: 'assigned'
version false
}
}
```
----- rom a console
```
def xmlString = """\
2017-03-03 00:00:00.0 CET
AAAA
BBBB
3
2016-10-01 00:00:00.0 CET
1
cacciavite
2016-10-02 00:00:00.0 CET
2
sgabello
jkhgf
""")
def slurper = new XmlSlurper().parseText( xmlString )
def doc = Documento.newInstance()
ctx.grailsWebDataBinding.bind instance, slurper
instance as JSON
```
### Expected Behaviour
I should see something like this:
{"dateCreated":"2017-03-02T23:00:00Z",
"nomi":["AAAA","BBBB"],
"numeroItems":3,
"righe":[
{"data":"2016-10-01T00:00:00.0Z","prodotto":"cacciavite", "ord": 1},
{"data":"2016-10-02T00:00:00.0Z","prodotto":"sgabello", "ord": 2}
],
"titolo":"jkhgf"}
### Actual Behaviour
The result is instead:
{"dateCreated":"2017-03-02T23:00:00Z",
"nomi":["AAAA","BBBB"],
"numeroItems":3,
"righe":[],
"titolo":"jkhgf"}
Currently the workaround for this is to extend or replace grailsWebDataBinding and avoid the call to GPathResultMap.put()
I tried it and it works without problems, apparently.
### Environment Information
- **Operating System**: debian 7.11 - 2.6.32-openvz-042stab112.15-amd64
- **Grails Version:** : 3.2.6
- **JDK Version:** 1.8
- **Container Version (If Applicable):** grails run-app container.
### Provided example
[Source here](https://github.com/Mizar01/testBinder)
Contributor guide
Research direction
Start with GrailsWebDataBinder.addElementsToCollections(...) around the reported line 579, then reproduce the issue using grails-app/domain/Documento.groovy, DocumentoRiga.groovy, and the provided XML binding example. Done means binding the XML populates both Documento.righe child objects while preserving the existing simple-list behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100