Repository navigation
fix(databinding): bind the first of several indexers as the index - #17251
borinquenkid wants to merge 14 commits into
Conversation
A name such as columns[0][data] was split at its last indexer, so it looked for a property named columns[0] and was ignored. The first indexer now selects the element and the rest is a key inside it, as columns[0].data already did. Fixes #15064 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…ta source setting, skip a bad name and keep a supplied object columns[0][data] is gathered into one element before it is bound. The grouped element is now: - written into the map that the object already holds at that index, so a list that holds [data: old] and a request of columns[0][data]=new gives [data: new] and keeps the other entries (a map has no bean properties for the entries to be bound to); - bound with the dataSourceAware setting of the caller, as the grouped source used to start from true; - left alone when the second index holds a space (ignored[0][first name]), as before, and no longer throws from group(1) and stops the bind; - kept as the supplied object when the request supplies one that is not a map (factories[0] = a factory and factories[0][isActive] = true): the factory keeps its identity and its other fields, and the entries are bound as its properties after the other names, under the whitelist and blacklist of the caller. The guide says what happens to a held map, a supplied object and a name with a space. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…d map, bind a supplied object through the normal entry point, and write into a held list The grouped element of columns[0][data] was written into the map the object already held before the bind decided whether it may change the property, so an include list that leaves columns out, an exclude list that names it, or bindable: false did not stop the write. The owning property is now checked with the same rules as any other indexed name. An object that the source supplies for the element is bound through the entry point a nested object is bound through, so the include list and constraints of its own class apply, and only when the indexed name is accepted by the prefix filter and names a property the object has. A list that the object holds at the first index takes the entries whose second index is a position, rows[0][1], replacing that position and extending the list when it is past the end. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…ken off A filter with an indexer of its own, such as selected[0], hid the second indexer of selected[0].columns[0][data] from the grouping, so the entry was not written into the map the object already holds. The indexers are now counted in the name that is left once the filter is removed. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…map past the growth limit An entry written into a list or map that the object already holds, columns[0][data] or rows[0][2], grew it to any index or any number of keys. It follows the rule of an index of its own now: it grows below the autoGrowCollectionLimit or inside what is already there, and an entry past it is ignored. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…map it is named in takes its index Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
Both conflicts were two additions side by side (a field and an import); both are kept. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A value outside the permitted scale is written into a map that is already there, and a nested update is applied to a bean the list did not accept.
| } | ||
| def held = okToBind ? findHeld(obj, owner, element, filter) : null | ||
| if (held instanceof Map) { | ||
| writeIntoHeldMap((Map) held, entries) |
There was a problem hiding this comment.
Binding columns[0][data] to a BigDecimal outside the permitted scale writes it into a map that is already in the list. The normal path rejects that value and reports a binding error. Check the scale before this write.
| def supplied = grouped.get(element) | ||
| MetaProperty owner = indexedOwner(obj, element, filter) | ||
| boolean okToBind = owner != null && isOkToBind(owner, whiteList, blackList) | ||
| if (supplied != null && !(supplied instanceof Map)) { |
There was a problem hiding this comment.
When beans[0] is already occupied, a different bean supplied at beans[0] is not attached, but beans[0][name] still changes that supplied bean. Apply the nested values to the element the list kept.
…h several indexers A name with several indexers, columns[0][data], that writes into a map or list the object already holds now checks a BigDecimal for its scale first, and reports a value beyond the permitted scale as a binding error, as a name with one indexer does. When the source also supplies the element as an object, beans[0] and beans[0][name], the entries are bound to the element the list holds once the supplied object has been bound. A list that already holds an element at the index keeps it and does not take the supplied object, so the entries now change the kept element and no longer change the supplied object. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
A value outside the permitted scale ( A supplied bean the list did not take (
I also compared a name with several indexers against the same entry written to a new element, over a held list of maps, a held list of lists, a held map of maps, a typed list of maps, a |
jdaugherty
left a comment
There was a problem hiding this comment.
The scale check and retained-list-element fixes are present. One further binding-contract defect is reproduced: a multi-indexer update to a held map writes before the property-level listener can veto it. Details are inline.
Validation: ran SimpleDataBinderSpec on JDK 17 with focused probes. The existing cases produced 123 passes and one skip. The one-indexer listener control passed; the two-indexer listener probe failed because the held value changed despite beforeBinding returning false. Two additional overflow probes failed for both index forms, so that pre-existing behavior is not a finding against this PR. No full repository suite was run.
| def held = okToBind ? findHeld(obj, owner, element, filter) : null | ||
| String elementName = elementPath(element, filter) | ||
| if (held instanceof Map) { | ||
| writeIntoHeldMap(obj, elementName, (Map) held, entries, listener, errors) |
There was a problem hiding this comment.
[P2] Honor the binding listener before mutating a held element
This writes directly into the held Map/List during preprocessing, then removes the entry from the source. It never reaches the normal property-level beforeBinding/afterBinding path. A listener returning false can therefore veto the ordinary indexed update but cannot veto the equivalent two-indexer update.
Reproduced through SimpleDataBinder.bind with a holder whose List<Map> columns is [[data: 'old']] and a DataBindingListenerAdapter overriding Boolean beforeBinding(Object target, String propertyName, Object value, Object errors) to return false:
['columns[0]': [data: 'new']]leavesoldintact.['columns[0][data]': 'new']writesnewdespite the veto.
Please route held-element writes through the listener contract before mutation, and preserve the corresponding after-binding callback. Add public-API regressions for held maps and lists, including a false-returning listener. The two-row probe was run on this exact head; the control passed and the multi-indexer row failed.
…ple-indexers-on-7.0.x
…stener first, as the bind of a property does A name with several indexers whose element the object already holds, as columns[0][data] for a List<Map>, wrote into that map or list while the source was being grouped, without the listener. A listener that returns false from beforeBinding could stop columns[0] and every other bind but not this one. Each key is now offered to beforeBinding, with the held map or list as the target and the key or the position as the property name, written only when it does not return false, and followed by afterBinding, as bindProperty does. The other forms never write into a held map or list and never reach the listener, so only this write needed the contract.
|
Pushed the next commit, which answers the review of 12-04 20:16. You are right: What I measured first, with a listener that records its calls, on this head, for a holder with
So the two-indexer write was the one write into a held container, and the one that did not reach the listener. The change. Each key of the element is offered to The class, not the sample.
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. The out-of-scale write into a held map is fixed. Grouping a nested index into an empty map throws before the other fields bind, and a listener for the held map is not asked.
| grouped.remove(element) | ||
| } else if (held instanceof List && writeIntoHeldList(obj, elementName, (List) held, entries, listener, errors)) { | ||
| grouped.remove(element) | ||
| } else { |
There was a problem hiding this comment.
Binding cells[a][data] into an empty Map<String, Map>, together with another field, throws while constructing the map. The other field is never bound. Previously the indexed field was ignored and the other field was bound. Do not build an element the binder cannot construct.
| return | ||
| } | ||
| if (held.containsKey(key) || held.size() < autoGrowCollectionLimit) { | ||
| if (listener == null || listener.beforeBinding(held, key, value, errors) != false) { |
There was a problem hiding this comment.
A listener registered for Map is not asked when columns[0][data] writes into a held map. A listener registered only for the parent is asked and receives the map. The write happens even when the map listener would refuse it. Ask the listeners for the object being written.
… that its type can take Before several indexers were grouped, columns[0][data] was left out of the bind. The grouping built an element for every name, and a container whose declared element type cannot be built from the entries (Map<String, Map>, Map<String, List>, Map<String, Object> and Map<String, String> as Map<String, Map> threw 'Could not find matching constructor for java.util.Map(LinkedHashMap)', List<List> threw NoSuchMethodException) made the bind throw before the other fields were bound. An element is now grouped when the type takes the entries (no declared type, a type that a map is, or a class of the application with a constructor of no arguments); for any other type the name is left out as before and the other fields bind. A map type that takes the entries is given a copy of them. GrailsWebDataBinderSpec has the 40 rows of the ten container and element types, quoted and unquoted keys, and the other field present and absent; 20 of them fail with the previous binder.
|
Pushed the next commit ( 1. A name with two indexers into an empty map throws. This is a regression of this pull request. I bound
So the class is "an element type that cannot be built from the entries", not only
2. A listener registered for
So you are right that the two paths differ: a bean that the binder reaches goes through The fix I would make is one protected method in |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An empty Map of Map no longer throws. A TreeMap value is stored as a LinkedHashMap, and an index that does not fit in an int aborts the bind.
| if (referencedType != null) { | ||
| if (val instanceof Map) { | ||
| mapInstance[indexedPropertyReferenceDescriptor.index] = referencedType.newInstance(val) | ||
| mapInstance[indexedPropertyReferenceDescriptor.index] = referencedType.isInstance(val) ? new LinkedHashMap((Map) val) : referencedType.newInstance(val) |
There was a problem hiding this comment.
A property declared Map<String, TreeMap>, given an indexed value that is already a TreeMap, stores a LinkedHashMap. holder.values.a.firstKey() then fails. The same input kept the TreeMap before. Store a value of the declared type.
… not as a number An index in a list that is not a position (not made of digits, or more than an int holds) aborted the bind with a NumberFormatException, where the name was ignored before several indexers were grouped. It is now a binding error on that parameter and the other fields bind. The text is read as digits, not converted, so any length is read the same way. The spellings 1 and 01 of one position in a list are gathered into one element, so the entries of neither are lost; a map keeps them as two keys. An element that is already of the declared map type is stored as it is, not as a LinkedHashMap. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley both are fixed, and a third case that I found while reproducing them. The index of a name with two indexers was converted with The case found on the way: A value that is already of the declared map type is stored as it is. Table: 62 rows over the index text (positions, leading zeros, the growth limit, the largest One thing it does not change: a name with two indexers whose declared element type is a map that a |
🚨 All tests passed but jobs failed 🚨Failed Jobs without Test Failures❌ CI / Hibernate5 Functional Tests (25) 🏷️ Commit: cdacd07 Rerun ControlsClick the checkbox to trigger a rerun: 🔲 Rerun jobs Learn more about TestLens at testlens.app/docs. |
Replaces #16982, which was based on 7.2.x. The bug is also on 7.0.x, the oldest branch with it, so the same changes are rebuilt there. The review history is on #16982.
columns[0][data]was split at its last indexer, so the binder looked for a property namedcolumns[0]and ignored the value without an error. The first indexer now selects the element and the rest is a key inside it, the same ascolumns[0].databinds. Names that share the first index are gathered into one element, and a name with one indexer takes the path it took before.On 7.0.x the tests of
grails-databinding-core(125, 1 skipped),grails-web-databinding(28) andgrails-test-suite-persistence(93) pass, as doescodeStyle. With the change toSimpleDataBindertaken out, 37 of the 83 tests ofSimpleDataBinderSpecfail. The data binding guide gains a short section.Branch policy: the automated check flags
SimpleDataBinderas a binder that could change a generated schema. It binds request data to objects and generates no schema.jdaugherty requested changes on #16982 on 11-18. The later commits there, which are included here, answer the review points; this has not been confirmed by a new review.
Fixes #15064
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL