Skip to content

fix(databinding): bind the first of several indexers as the index - #16982

Closed
borinquenkid wants to merge 7 commits into
7.2.xfrom
fix/databinder-multiple-indexers-7.0.x
Closed

borinquenkid wants to merge 7 commits into
7.2.xfrom
fix/databinder-multiple-indexers-7.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

columns[0][data] was split at its last indexer, so the binder looked for a property named columns[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 as columns[0].data binds. 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 SimpleDataBinderSpec features fail without the change and pass with it; all 76 grails-databinding-core tests pass; codenarc and checkstyle are clean. The data binding guide gains a short section.

Fixes #15064

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

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
Copilot AI balanced review requested due to automatic review settings November 13, 2026 23:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley All four reproduced, and the new commit fixes each one.

  • A held map. A list that holds [data: old, keep: me], and a request of columns[0][data]=new, now gives [data: new, keep: me]. The grouped entries are written into the map the object holds at that index, for a list or a map property, and not bound as bean properties of a map. When the property is reached through a prefix (a filter), the held map is found the same way.
  • The data source setting. The grouped source takes dataSourceAware from the caller's source. The spec records the setting each indexed property sees, with false and with true, using the unrelated name ignored[0][name] you described.
  • A space in the second index. ignored[0][first name] is left ungrouped, as before, so it is skipped and the other names are still bound.
  • A supplied object. factories[0] supplied as a factory, together with factories[0][isActive], keeps the factory (the same instance, its name, its other fields) and binds the grouped entries as its properties after the other names. They are bound only when the caller's whitelist and blacklist allow the property factories, and with the caller's data source setting.

SimpleDataBinderSpec has a row for each, 4 of them fail on the previous commit, and grails-databinding-core (81 tests) and grails-web-databinding (28 tests) pass with codeStyle. The guide says what happens to a held map, a supplied object and a name with a space.

I did not bind the entries of a supplied object under a nested whitelist such as factories.name; they are bound with no whitelist once the property factories itself is allowed. Say if that should be tighter.

Branch policy: SimpleDataBinder is the request data binder and generates no schema (the check that flags files named *Binder matches it by name). The change only affects names with more than one indexer, which did not bind before.

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 jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +323 to +326
Map held = findHeldMap(obj, element, filter)
if (held != null) {
held.putAll(entries)
grouped.remove(element)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Comment on lines +275 to +278
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Comment on lines +313 to +316
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@borinquenkid

Copy link
Copy Markdown
Member Author

Thank you, all of these reproduce and are fixed in b0a4b32. Each case has a spec that fails without the commit.

  • A held map bypasses the binding restrictions (P1, and James's bindable: false). The owning property is resolved and checked with the same rules as any other indexed name before the held map is written: an include list that leaves columns out, an exclude list that names it, and bindable: false (through the web binder) all leave [data: 'old'] as it is. Rows for each list in SimpleDataBinderSpec, and GrailsWebDataBinderSpec for bindable: false.
  • A supplied object's own binding constraints (P1). The deferred update goes through bind(target, source, listener), the entry point a nested object is bound through, so the web binder takes the include list of the supplied object's class: entries[0][verified]=true leaves verified false and entries[0][name] still binds. dataSourceAware stays with the deferred source. The errors of the supplied object are now its own, as when it is bound directly.
  • Deferred updates ignore the prefix filter (P2). A supplied object is queued only when the indexed name is inside the filter and the object has the indexed property, and the property is one the bind may change. filter='selected' with unprefixed keys leaves the object as it is, and so does missing[0] when the target has no missing.
  • Nested lists (List<List>). Yes, intended: a list the object holds at the first index takes the entries whose second index is a position, so rows[0][0]='new' over [['old']] gives [['new']], and a position past the end extends the list with nulls. A second index that is not a position is not written into a list; it is grouped as before. Only a held list or map is written into: a nested list of lists of lists is not covered.

The 91 tests of grails-databinding-core, the 28 of grails-web-databinding and the 90 of grails-test-suite-persistence pass, and codeStyle is clean. The guide says that the restrictions, the filter and the object's own constraints apply.

If another shape still writes where it should not, a failing spec in your style would show me which.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: selected[0].columns[0][data] with the filter selected[0] sets the data of the map the object already holds now. The indexers are counted in the name that is left once the filter is taken off, since a filter such as selected[0] has an indexer of its own; SimpleDataBinderSpec has the case, and it fails without the change.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@borinquenkid

Copy link
Copy Markdown
Member Author

@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 autoGrowCollectionLimit or inside what is already there, and an entry past that is ignored. I applied it to the held map as well as the held list, since both had the same hole. SimpleDataBinderSpec has six rows (an index inside the limit, far past it, at it, and the map keys); three of them fail without the change.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

@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 rows[0][0] of a List<List> that already holds ['old'] has its own rows too.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@borinquenkid
borinquenkid marked this pull request as draft November 23, 2026 01:41
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
@borinquenkid
borinquenkid changed the base branch from 7.0.x to 7.2.x November 26, 2026 02:48
@testlens-app

testlens-app Bot commented Nov 26, 2026

Copy link
Copy Markdown

🔎 No tests executed 🔎

🏷️ Commit: 67a49aa
▶️ Tests: 0 executed
⚪️ Checks: 60/60 completed


Learn more about TestLens at testlens.app/docs.

@borinquenkid

Copy link
Copy Markdown
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants