Repository navigation
fix(databinding): bind the first of several indexers as the index - #16982
borinquenkid wants to merge 7 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
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. A single indexer still binds. Several indexers into an existing map do not. Grouping also turns data-source awareness back on, throws on a name with a space, and drops a typed object that was already supplied.
| if (key.size() > 2 && ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith('"') && key.endsWith('"')))) { | ||
| key = key[1..-2] | ||
| } | ||
| elements.computeIfAbsent(matcher.group(1)) { new LinkedHashMap<String, Object>() }.put(key + rest.group(2), source.getPropertyValue(name)) |
There was a problem hiding this comment.
A list already holds [data: old]. The request is columns[0][data]=new. The selected map should become [data: new]. It stays [data: old]. The grouped value is bound as bean properties, and a map has none named data, so the entry is ignored. Write the grouped entries into the selected map.
| } | ||
| grouped.put(element, entries) | ||
| } | ||
| new SimpleMapDataBindingSource(grouped) |
There was a problem hiding this comment.
The caller set dataSourceAware to false. Adding an unrelated name such as ignored[0][name] replaces the source with one that has it true. A later property can then load and bind an existing instance. Copy the caller's setting onto the grouped source.
| if (matcher) { | ||
| def rest = matcher.group(2) =~ /^\[\s*([^\s\]]*)\s*\](.*)$/ | ||
| rest.matches() | ||
| String key = rest.group(1) |
There was a problem hiding this comment.
A parameter named ignored[0][first name] used to be skipped. It matches the outer pattern and fails the inner one, because the key has a space. group(1) then throws, and the whole bind stops. If the inner pattern does not match, skip that name.
| if (existing instanceof Map) { | ||
| entries.putAll((Map) existing) | ||
| } | ||
| grouped.put(element, entries) |
There was a problem hiding this comment.
The source has factories[0] set to an existing factory, and factories[0][isActive] set to true. The existing factory should keep its identity and its other fields. This replaces it with a new map, so the name and the instance are gone. Keep a supplied object that is not a map, and apply the grouped entries to it.
…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
|
@jamesfredley All four reproduced, and the new commit fixes each one.
I did not bind the entries of a supplied object under a nested whitelist such as Branch policy: If you have another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot. |
jdaugherty
left a comment
There was a problem hiding this comment.
The existing grails-databinding-core test suite passes, but focused public-API tests reproduce three remaining problems in the new grouping/deferred paths: held maps bypass binding restrictions, supplied objects bypass their own binding constraints, and deferred updates ignore the prefix filter. These need to be addressed before merging.
There is also a scope/coverage gap relative to #15064: a List initialized to [['old']] still ignores rows[0][0]='new'. The added tests cover lists of maps and typed objects, but not the nested-list case requested in the issue. Please clarify whether nested lists are intended to be supported and add coverage for that case.
| Map held = findHeldMap(obj, element, filter) | ||
| if (held != null) { | ||
| held.putAll(entries) | ||
| grouped.remove(element) |
There was a problem hiding this comment.
[P1] Check binding restrictions before mutating a held map
This putAll happens before doBind checks isOkToBind, and removing the element from grouped prevents that check from ever running. With columns initialized to [[data: 'old']], binding columns[0][data]='new' changes the map even when the include list is ['name'] or the exclude list is ['columns']. I also reproduced this with GrailsWebDataBinder and columns declared bindable: false. Resolve and validate the owning MetaProperty with the same include/exclude and property checks as the normal indexed path before making any changes; add regressions for both explicit lists and bindable: false.
| deferred.each { Object target, Map<String, Object> entries -> | ||
| SimpleMapDataBindingSource deferredSource = new SimpleMapDataBindingSource(entries) | ||
| deferredSource.dataSourceAware = source.dataSourceAware | ||
| doBind(target, deferredSource, null, null, null, listener, errors) |
There was a problem hiding this comment.
[P1] Preserve the supplied object's own binding constraints
Calling doBind directly with a null whitelist bypasses GrailsWebDataBinder.bind, which normally obtains getBindingIncludeList for the nested target. For an entry whose constraints declare verified bindable: false, a source containing entries[0]=entry and entries[0][verified]=true sets entry.verified to true here; directly binding the same entry through the public binder correctly leaves it false. The parent-property check in groupMultipleIndexers does not protect the child's fields. Route the deferred update through the normal nested-object binding entry point while retaining dataSourceAware, and cover a supplied object with an unbindable field.
| if (supplied != null && !(supplied instanceof Map)) { | ||
| def descriptor = getIndexedPropertyReferenceDescriptor(elementPath(element, filter)) | ||
| if (descriptor && isOkToBind(descriptor.propertyName, whiteList, blackList)) { | ||
| deferred.computeIfAbsent(supplied) { new LinkedHashMap<String, Object>() }.putAll(entries) |
There was a problem hiding this comment.
[P2] Only defer updates for indexed properties accepted by the bind
This branch queues supplied objects without checking the prefix filter or whether the target even has the indexed property. With target.entries=[entry], source entries[0]=entry plus entries[0][name]='new', and filter='selected', the main binding loop correctly skips both unprefixed keys, but the deferred loop still changes entry.name. I also reproduced mutation of a supplied object under missing[0] when the target has no missing property. Queue deferred work only after the prefix and target-property checks have accepted the indexed binding, so ignored source entries cannot mutate objects.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. The first of several indexers is now the index. A property that binding is not allowed to change can still be changed through a held map.
| if (supplied instanceof Map) { | ||
| entries.putAll((Map) supplied) | ||
| } | ||
| Map held = findHeldMap(obj, element, filter) |
There was a problem hiding this comment.
columns is on the list of properties that must not be bound. columns[0][data] still changes the map that is already there. Check that the property may be bound before writing into that map.
…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
|
Thank you, all of these reproduce and are fixed in b0a4b32. Each case has a spec that fails without the commit.
The 91 tests of If another shape still writes where it should not, a failing spec in your style would show me which. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A property that must not be bound is now checked before a write into a held map. A filter that itself has an index still drops the update.
| protected void doBind(obj, DataBindingSource source, String filter, List whiteList, List blackList, DataBindingListener listener, errors) { | ||
|
|
||
| Map<Object, Map<String, Object>> deferred = new IdentityHashMap<Object, Map<String, Object>>() | ||
| source = groupMultipleIndexers(obj, source, filter, whiteList, blackList, deferred) |
There was a problem hiding this comment.
The filter is selected[0]. columns may be bound, and it already holds a map. The input selected[0].columns[0][data] should set that map's data. It leaves the old value, because the indexers are grouped before the filter is removed. Remove the filter first, then group the indexers.
…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
|
@jamesfredley: |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A nested index past the growth limit still grows the list.
| return false | ||
| } | ||
| entries.each { String key, Object value -> | ||
| int position = key.toInteger() |
There was a problem hiding this comment.
The list already exists. A request names an index far past the growth limit. The list grows to that index. Stop at the growth limit.
…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
|
@jamesfredley: a nested index or key past the growth limit does not grow what the object already holds now. An entry written into a list or map that the object holds follows the rule of an index of its own: it grows below |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A value that was not accepted is still changed.
| elements.each { String element, Map<String, Object> entries -> | ||
| def supplied = grouped.get(element) | ||
| MetaProperty owner = indexedOwner(obj, element, filter) | ||
| boolean okToBind = owner != null && isOkToBind(owner, whiteList, blackList) |
There was a problem hiding this comment.
The growth limit is 3. The request names index 3 and a name for that entry. The list stays empty, but the supplied object's name changes. Change only the element the list accepted.
…map it is named in takes its index Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: with the growth limit at 3, a supplied object named at index 3 is no longer changed: its entries are queued only when the list or map that holds the property would take that index, a position inside the list or below the growth limit, a key the map has or a map below the limit. The same index check as for an entry of its own. Before asking for another look, I tried each shape of the neighbourhood: a supplied object at, inside and past the limit for a list that is empty, holds one element or holds more than the limit, and for a map with room, with a new key at the limit and with an existing key; and the include and exclude lists (the owner alone, the owner's property path, the other property, both lists) and the filter (with the prefix, without it, another prefix) for a supplied object and for a held map. 25 rows; 5 fail without this change. @jdaugherty: your three points, held maps and supplied objects obeying the restrictions and the filter, are among those rows, and the nested list case |
The 7.0.x patch line takes no more fixes after the 2026-11-25 releases; this PR moves to 7.2.x. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
🔎 No tests executed 🔎🏷️ Commit: 67a49aa Learn more about TestLens at testlens.app/docs. |
|
Moved to 7.0.x as #17251: the bug is also on 7.0.x, the oldest branch with it. The same changes are rebuilt there on a new branch. Closing this one; its review history stays here. |
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.Verified locally: the three new
SimpleDataBinderSpecfeatures fail without the change and pass with it; all 76grails-databinding-coretests pass; codenarc and checkstyle are clean. The data binding guide gains a short section.Fixes #15064
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL