Skip to content

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

Open
borinquenkid wants to merge 14 commits into
7.0.xfrom
fix/databinder-multiple-indexers-on-7.0.x
Open

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

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

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

On 7.0.x the tests of grails-databinding-core (125, 1 skipped), grails-web-databinding (28) and grails-test-suite-persistence (93) pass, as does codeStyle. With the change to SimpleDataBinder taken out, 37 of the 83 tests of SimpleDataBinderSpec fail. The data binding guide gains a short section.

Branch policy: the automated check flags SimpleDataBinder as 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

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
Both conflicts were two additions side by side (a field and an import); both are kept.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid borinquenkid added run ci and removed no ci labels Dec 4, 2026
@borinquenkid
borinquenkid marked this pull request as ready for review December 4, 2026 13:37
Copilot AI balanced review requested due to automatic review settings December 4, 2026 13:37

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.

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)

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.

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

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.

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

Copy link
Copy Markdown
Member Author

ddcfbb625cb takes both findings and the cases around them.

A value outside the permitted scale (SimpleDataBinder.groovy:374). A name with several indexers that writes into a map or list the object already holds now checks the value first. A BigDecimal beyond the permitted scale is reported as a binding error under the name columns[0][data] and is not written, as a name with one indexer does. A value inside the scale is still written. New rows: 1e1001 and 1e-1001 into a held map, 1e1001 into a held list of lists, a bad scale nested inside a map value or a list value, 1e1000 into a held map, and a bad entry that does not stop the good entries of the same element.

A supplied bean the list did not take (:362). The entries of beans[0][name] are now bound to the element the list holds at the index once the supplied object has been bound, not to the supplied object. A list that already has an element at the index keeps it, so the entries change that element and the supplied object stays as it was. When the list took the supplied object, it is that object. I changed the existing row in A supplied object is changed only when the list or map it is named in accepts it that expected the supplied bean to change when the index was already occupied. That row encoded the behaviour you found.

grails-databinding-core (197 tests) and grails-web-databinding (28 tests) pass, and codeStyle passes. With both fixes put back to the old behaviour, 19 rows fail, including the three scale rows and the occupied-list row.

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 Set of maps and an empty or null property, with a string, null, empty, number, bad and good BigDecimal, map and list value, and the include and exclude lists. Nothing else differed. Two things are the same as before and are not changed here: a third indexer, columns[0][a][b], writes the key a[b], and a value in a map typed Map<String, Integer> is not converted, as it is not when the element is new.

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

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] 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']] leaves old intact.
  • ['columns[0][data]': 'new'] writes new despite 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.

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

Copy link
Copy Markdown
Member Author

Pushed the next commit, which answers the review of 12-04 20:16. You are right: columns[0][data] for a List<Map> wrote into the held map while the source was grouped, and the listener never saw it.

What I measured first, with a listener that records its calls, on this head, for a holder with List<Map> columns, Map<String, Map> cells and List<List> rows, with the listener returning false and true:

  • columns[0]: [data: 'new'], columns[0].data: 'new', cells[a], cells[a].data, rows[0]: [7, 8]: the held element is left as it was, and the listener is not called, whatever it returns. These forms never write into a held map or list, so the old value is not the result of a veto.
  • columns[0][data], cells[a][data] and rows[0][1]: the held element is written with beforeBinding returning false, and the listener is not called.

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 beforeBinding before it is written, as bindProperty does: the held map or list is the target, and the key, or the position for a list, is the property name. It is written only when the listener does not return false, and afterBinding follows for each key the map or list takes, written or not, as it does after a property that was vetoed. A key that the growth limit refuses is not offered. The property name is my choice, since no other form reaches the listener for a held container; it is the same target-and-property pair that the bind of a typed element passes to the listener for that element's properties. If you want the listener to see the full path, columns[0][data], say so and I will pass that.

The class, not the sample. SimpleDataBinderSpec has 21 rows over three held containers (a list of maps, a map of maps, a list of lists), four listener modes (absent, observes, vetoes every write, vetoes the second key) and one or two entries in the element. Each row asserts the container after the bind and the listener's calls, before and after for each key in order. Without the change 15 rows fail; the six with no listener pass either way.

grails-databinding-core (218 tests) and grails-web-databinding (28 tests) pass, and codeStyle is clean. I merged 7.0.x first, with no conflict. The two overflow probes you ran fail for both index forms, as you say, so I have not touched them.

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

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.

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

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

Copy link
Copy Markdown
Member Author

Pushed the next commit (6c701fb82e, and one sentence of the guide in 91554fbbef), which answers the review of 12-05 at ead22a3. The two findings are not the same kind, so I measured each one on 7.0.x before changing code.

1. A name with two indexers into an empty map throws. This is a regression of this pull request. I bound x[a][data], x[a][more] and other through GrailsWebDataBinder into ten containers, on 7.0.x and on the head of the review:

Container 7.0.x head of the review
Map<String, Map> other bound, name left out throws Could not find matching constructor for java.util.Map(LinkedHashMap)
Map<String, List> same throws, same message for List
Map<String, Object>, Map<String, String> same MissingPropertyException: No such property: data
List<List> same NoSuchMethodException: java.util.List.<init>()
Map<String, Bean>, List<Bean>, List<Map>, raw Map, raw List same element built, other bound

So the class is "an element type that cannot be built from the entries", not only Map. The grouping now asks one question before it builds an element: does the declared type take the entries? No declared type, or a type that a map is (Map, Object), takes them as a map (a copy, so the request's own map is not shared); a class of the application with a constructor of no arguments is bound property by property; any other type (String, List) leaves the name out as it was on 7.0.x, and the other fields bind. One chokepoint, one rule, and the guide says it.

GrailsWebDataBinderSpec has the table: the ten containers, the key quoted and unquoted, and the other field present and absent, 40 rows. With the binder of the previous commit 20 of them fail. It is in the web binder spec because the web binder is where the exception reaches the caller; the core binder records the same failure as a binding error and carries on. grails-databinding-core (218 tests) and grails-test-suite-persistence (133 tests) pass and codeStyle is clean.

2. A listener registered for Map is not asked when columns[0][data] writes into a held map. I measured it and I did not change code, because the fix is new machinery. With three listeners registered (one for the parent class, one for Map, one for a child class):

  • A nested bean, child: [name: 'x']: the parent's listener is asked for child, and the child's listener is asked for name with the child as target.
  • A held map, held[a][data]: the parent's listener is asked, with the map as the target of the write. The listener for Map is not asked.

So you are right that the two paths differ: a bean that the binder reaches goes through doBind, which adds the registered listeners that support its class, and a held container does not. The held-map offer of this pull request is new behavior (7.0.x wrote nothing there and asked nobody), so this is a defect in the new code and in scope.

The fix I would make is one protected method in SimpleDataBinder, listenerFor(target, listener), that returns the given listener, and an override in GrailsWebDataBinder that returns the multicast listener plus the registered listeners that support the held object's class. That is a new hook on a public class of a patch line plus a method on DataBindingEventMulticastListener, so I would rather ask than add it: do you want it in this pull request, or is it better as a follow-up for 8.1.x, with this one keeping the listener it has now? I will do it the way you answer.

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

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

Copy link
Copy Markdown
Member Author

@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 toInteger(), so a text that an int does not hold aborted the bind with a NumberFormatException, and so did abc, an empty index and -1, where the name was ignored before several indexers were grouped. It is now read as digits, not converted: the zeros in front are dropped and more than ten digits are more than an int has, so a text of any length is read the same way. An index that is not a position in a list is a binding error on that parameter, and the other fields still bind. A position past autoGrowCollectionLimit is left out as before, for a name with one index too. The path of a name with one index has the same failure on 7.0.x and is not changed here: #17341.

The case found on the way: columns[1][data] together with columns[01][other] lost other, because the two spellings were gathered as two elements and the second replaced the first. For a list they are now one element, whichever comes first. For a map they stay two keys, as "1" and "01" are two keys there.

A value that is already of the declared map type is stored as it is. Map<String, TreeMap> given a TreeMap keeps the TreeMap, and a LinkedHashMap given for a TreeMap is copied into one.

Table: 62 rows over the index text (positions, leading zeros, the growth limit, the largest int and above, the largest long and above, 30 and 3000 digits, negative, letters, empty, decimal), with and without a listener, the spellings of one position in either order, a list against a map, and five declared map element types by how the element arrives. 31 fail on the previous head. grails-databinding-core (280 tests), grails-web-databinding (28) and grails-test-suite-persistence (133) pass, and so does codeStyle. The guide says what an index that is not a position does.

One thing it does not change: a name with two indexers whose declared element type is a map that a LinkedHashMap is not, such as TreeMap or Hashtable, is left out, as the guide says for a type that cannot be built from the entries. Say if you want those built from the entries too.

@testlens-app

testlens-app Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ CI / Hibernate5 Functional Tests (25)

🏷️ Commit: cdacd07
▶️ Tests: 52591 executed
🟡 Checks: 51/54 completed

Rerun Controls

Click the checkbox to trigger a rerun:

🔲 Rerun jobs


Learn more about TestLens at testlens.app/docs.

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants