Skip to content

fix(config): bind nested settings maps under Spring 7 - #16160

Merged
jdaugherty merged 2 commits into
8.0.xfrom
fix/spring7-nested-map-conversion
Sep 23, 2026
Merged

jdaugherty merged 2 commits into
8.0.xfrom
fix/spring7-nested-map-conversion

Conversation

@jamesfredley

@jamesfredley jamesfredley commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #16159.

The problem

Spring Framework 7 no longer converts a configuration Map into a type annotated @Builder(builderStrategy = SimpleStrategy). ConfigurationBuilder depends on that conversion to bind nested settings from application.yml / application.groovy, so those settings now fail with ConverterNotFoundException at configuration-binding time - that is, at application startup.

Grails 8 is the first affected version, because it is the first on Spring 7: 8.0.x pins Spring Boot 4.1.0, which resolves spring-core 7.0.8.

This targets 8.0.x so the fix flows up the maintenance chain (8.0.x -> 8.1.x -> 9.0.x) instead of landing only on the newest line. Both branches pin the same Spring Boot and Groovy versions, and ConfigurationBuilder.groovy plus its spec are byte-identical on each, so one fix covers both.

The gap is demonstrable rather than theoretical: with the previous ConfigurationBuilder and only this PR's spec applied, six scenarios fail with

Expected exception of type 'ConfigurationException',
  but got 'org.springframework.core.convert.ConverterNotFoundException'

The fix

ConfigurationBuilder now instantiates the target type and populates it from the Map when, and only when, Spring genuinely has no converter.

Two independent reviewers examined the handling over five rounds and found nine distinct defects. Every guard below exists because removing it produced an observable failure, and each has a regression test:

Guard Failure without it
Engage only when the cause chain contains ConverterNotFoundException A converter that deliberately rejects a Map could be bypassed
Never suppress ConfigurationException Unknown-key and malformed-value failures were masked by the original exception
Throw on raw-lookup failure instead of falling back Configuration whose lookup had failed was silently accepted
Inherit from the fallback before applying overrides Overriding one field discarded every unspecified field
Pass each property's fallback child into nested conversion Inheritance worked only at the first level; deeper children were reset
Convert values to the target property type multiTenancy.mode: database was rejected where DATABASE worked
Resolve Class entries via the thread context class loader hibernate.configClass and other application classes were left unbound, because that converter resolves against the framework class loader
Let Map-backed types keep arbitrary entries HibernateSettings extends LinkedHashMap exists to carry keys such as hibernate.hbm2ddl.auto; strict binding rejected them
Bind flattened descendant keys through their parent The resolver flattens nested config, so anything nested more than one level failed to build
Invoke setters with an explicit single-element argument array The Java null-varargs pitfall meant an explicit null could not clear an inherited value

Strictness is preserved where it belongs: a dotted key whose first segment is unknown is still rejected, and non-Map types still reject unknown keys. Both are covered by guard specs so a future change cannot quietly relax them.

Testing

ConfigurationBuilderSpec grows from 10 to 22 specs; the module total goes from 108 to 120, all passing.

:grails-datastore-core:test --rerun-tasks   BUILD SUCCESSFUL, 46 tasks executed
tests=120 failures=0 errors=0
ConfigurationBuilderSpec tests=22 failures=0

Verified on the 8.0.x base, not only on the branch this was originally written against.

Some specs use a narrow PropertyResolver proxy to reproduce the Spring 7 failure boundary, because the real resolver auto-converts the top-level bean and would bypass the handler entirely. Raw-map shape and dotted lookups still go through the real DatastoreUtils.createPropertyResolver.

Known limitation

A PropertyResolver that exposes only an aggregate map, without its entries addressable as dotted properties, can still yield null for a configured scalar. Grails' own DatastoreUtils.createPropertyResolver flattens and is unaffected. Binding the raw value unconditionally was considered and rejected because it would bypass the type conversion and case-insensitive enum handling above; both reviewers agreed that would be a net regression. A follow-up could use the raw value only when it is already assignable.

Provenance

This was first written on the Groovy 6 canary branch (#15558), which is simply where the failure was noticed. It is not Groovy-version-specific - both branches are on Groovy 5.0.8 and the code carries its own note to that effect - so it belongs here. Once this reaches 9.0.x through the merge chain, the same change is dropped from #15558.

Copilot AI lite review requested due to automatic review settings August 16, 2026 20:22

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.

Pull request overview

This PR fixes configuration binding for nested settings maps under Spring Framework 7, where Spring no longer auto-converts Map values into Groovy @Builder(builderStrategy = SimpleStrategy) types (leading to ConverterNotFoundException during settings binding).

Changes:

  • Adds a defensive fallback in ConfigurationBuilder to instantiate and populate target types from Map when (and only when) Spring genuinely has no converter.
  • Preserves strictness and inheritance semantics in the fallback path (unknown keys, malformed values, explicit nulls, map-backed settings, flattened descendant keys).
  • Expands ConfigurationBuilderSpec with targeted regression coverage for Spring 7 conversion failure boundaries and the new fallback behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy Implements Spring 7 Map-to-builder-type fallback binding and related conversion/strictness handling.
grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy Adds extensive regression tests covering nested-map fallback binding, strictness, inheritance, and flattened key behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bito-code-review

Copy link
Copy Markdown

The observation is correct. In the handleConverterNotFoundException method, the code iterates over the raw map entries and uses the original key object directly when putting it into the map-backed instance:

((Map) instance).put(key, val)

If the key is not a String (e.g., a GString), this can lead to issues where subsequent lookups using String keys fail, even though the code earlier in the loop correctly normalizes the key to a String for property matching (String propertyName = key.toString()). To ensure consistency and compatibility with String-based lookups, the code should use the normalized propertyName instead of the original key when populating the map.

grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy

if (mapBacked) {
                        ((Map) instance).put(propertyName, val)
                        return
                    }

@codecov

codecov Bot commented Aug 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.44444% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.2664%. Comparing base (54ea208) to head (8aee638).
⚠️ Report is 1991 commits behind head on 8.0.x.

Files with missing lines Patch % Lines
...tastore/mapping/config/ConfigurationBuilder.groovy 69.4444% 19 Missing and 14 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #16160        +/-   ##
==================================================
+ Coverage     52.3252%   57.2664%   +4.9412%     
- Complexity      18536      22453      +3917     
==================================================
  Files            2039       2131        +92     
  Lines           97498     103820      +6322     
  Branches        17138      18633      +1495     
==================================================
+ Hits            51016      59454      +8438     
+ Misses          38998      36092      -2906     
- Partials         7484       8274       +790     
Files with missing lines Coverage Δ
...tastore/mapping/config/ConfigurationBuilder.groovy 68.7500% <69.4444%> (+0.2771%) ⬆️

... and 574 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Spring Framework 7 no longer converts a configuration Map into a type
annotated @builder(builderStrategy = SimpleStrategy), so nested settings
failed to bind with ConverterNotFoundException. ConfigurationBuilder now
instantiates the target type and populates it from the Map.

The gap is demonstrable on this branch: with the previous ConfigurationBuilder
and only the new spec applied, six scenarios fail with "Expected exception of
type 'ConfigurationException', but got 'ConverterNotFoundException'". 9.0.x
resolves spring-core 7.0.8 via Spring Boot 4.1.0.

The fallback is deliberately narrow, and every guard below exists because
removing it produced an observable failure:

- It engages only when the cause chain contains ConverterNotFoundException,
  so a converter that deliberately rejects a Map is not bypassed.
- ConfigurationException is never suppressed, so unknown-key and
  malformed-value failures still surface instead of being masked by the
  original conversion exception.
- A failure while resolving the raw value throws rather than silently
  falling back, so configuration whose lookup failed is not quietly accepted.
- The instance inherits from the fallback before overrides are applied, and
  each nested level receives its own fallback child, so overriding one field
  does not discard the rest.
- Values are converted to the target property type, including the
  case-insensitive enum path, so multiTenancy.mode: database still binds.
- Class-typed entries resolve through the thread context class loader, the
  same route the top-level Class handling uses, because the resolver's
  converter resolves against the framework class loader and would leave an
  application class such as hibernate.configClass unbound.
- Types that are themselves a Map keep arbitrary entries. HibernateSettings
  extends LinkedHashMap precisely to carry keys like hibernate.hbm2ddl.auto,
  which strict property-only binding would have rejected.
- Flattened descendant keys are bound once through their parent rather than
  rejected, since the resolver flattens nested configuration; a dotted key
  whose first segment is unknown is still rejected.
- Setters are invoked with an explicit single-element argument array so an
  explicit null clears an inherited value.

ConfigurationBuilderSpec grows from 10 to 22 specs covering each of the above.

Known limitation: a PropertyResolver that exposes only an aggregate map, and
not its entries as dotted properties, can still yield null for a configured
scalar. Grails' own DatastoreUtils.createPropertyResolver flattens and is
unaffected. Binding the raw value unconditionally was rejected as a fix
because it would bypass the type conversion above.

Assisted-by: claude-code:claude-opus-5
@jamesfredley
jamesfredley force-pushed the fix/spring7-nested-map-conversion branch from 5c99b41 to 7d5e684 Compare August 17, 2026 12:51
@jamesfredley
jamesfredley changed the base branch from 9.0.x to 8.0.x August 17, 2026 12:51
@testlens-app

This comment has been minimized.

// top-level Class handling above, because the resolver's String->Class converter
// resolves against the framework class loader and silently leaves an
// application-defined class (hibernate.configClass, for example) unbound.
if (propertyType == Class) {

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.

This branch and resolveClassValue aren't exercised by the new specs: MapBackedSettings.configClass is declared as String, and no test settings type has a Class-typed property, so the guard listed in the PR table ("Resolve Class entries via the thread context class loader") has no regression test. Could you add coverage for a nested Class-typed property — a Class literal value, a String class name, and an invalid class name?

While adding that, worth pinning the fallback behavior: the top-level argType == Class handling falls back to fallBackValue when no usable value is found, but resolveClassValue returns null, so a value that is neither a Class nor a CharSequence would silently clear an inherited fallback through the setter instead of retaining it (or erroring).

The Map fallback re-read every nested entry from the property resolver by
dotted path and discarded the value it already held. The resolver exposes
the aggregate Map for a level without always exposing what sits below it,
so a configured value was silently dropped - hibernate.hibernateEventListeners
built an empty bean instead of failing loudly as it did before. Bind the
entry in hand when the resolver has nothing for the path and the value is
already assignable, so declared conversion still runs where it applies.

Also:

- Read the raw value only when the typed lookup found nothing, saving a
  resolver pass per nested scalar.
- Resolve a Class entry from the value in hand and keep an inherited class
  when the configured value names none, matching the top-level handling.
- Store map-backed entries under the normalized String key so they stay
  addressable by String.
- Resolve a flattened descendant key from its path rather than from the
  descendant's value, which belongs to a deeper property.
- Report a setter that rejects a value against the property's own path
  instead of the parent's, with the type that was expected.
- Correct the comments: a type carrying a runtime @builder annotation is
  built by the recursion above and never reaches this branch.

Covers the Class-typed, map-backed key and resolver-addressing paths with
specs, including the Hibernate settings tree that reproduces the failure.
@jdaugherty

Copy link
Copy Markdown
Contributor

Pushed 8aee638. It addresses the four open review comments and a defect they surfaced.

Which shipped configuration reproduces this

hibernate.hibernateEventListeners. Configured as

hibernate:
  hibernateEventListeners:
    listenerMap:
      pre-load: someListener

Spring 7 throws at binding time:

ConverterNotFoundException: No converter found capable of converting from type
[java.util.LinkedHashMap<?, ?>] to type [org.grails.orm.hibernate.HibernateEventListeners]

HibernateEventListeners is a plain bean with no runtime @Builder, which is why it reaches
this branch. The review comment on the @Builder retention is correct — javap on
groovy-5.0.8.jar confirms groovy.transform.builder.Builder is RUNTIME-retained, so
argType.getAnnotation(Builder) intercepts every SimpleStrategy type in the recursion above
and those never reach the new handler. The Hibernate and connection-source trees named in
#16159 bind through the recursion, not here. The code comment and javadoc are corrected to
describe plain settings beans instead.

The fallback did not bind it

With the previous revision applied, that configuration produced a HibernateEventListeners
with a null listenerMap — a loud startup exception became silent data loss.

This is the documented "known limitation", and the claim that
DatastoreUtils.createPropertyResolver is unaffected by it does not hold.
createFlatConfig only emits dotted keys for ConfigObject values; a plain nested Map is
stored as a leaf. So getProperty('hibernate.hibernateEventListeners.listenerMap', Map)
returns null while the value sits in the very map resolveMapValue was handed and discarded.

resolveMapValue now binds the entry in hand when the typed lookup finds nothing at the path
and the value is already assignable — the follow-up proposed in the description. Values needing
conversion still route through the enum, Class and nested-map branches, so the case-insensitive
enum handling is untouched.

Review comments addressed

Comment Change
Comment and javadoc name @Builder(SimpleStrategy) as the case handled Reworded to plain settings beans; the recursion handles annotated types
Class branch and resolveClassValue have no regression test Specs for a class literal, a class name
resolveClassValue returns null where the top-level handling falls back Returns the fallback when no class, with a spec
Raw lookup runs even when the typed lookup succeeded Moved inside the value == null branch, one ralar
Map-backed entries stored under the original key object Stored under the normalized String, with a spec using a GString key

Two further defects found while verifying:

  • A flattened descendant key passed the descendant's value as the parent property's raw
    value. Not reachable through the real resolver's key ordering, but it would bind a
    grandchild map onto its parent. The descendant is now resolved from its path.
  • A setter rejecting a value reported the parent's path. It now reports the property's own
    path and the expected type, rather than surfacing argument type mismatch.

Testing

ConfigurationBuilderSpec goes from 22 to 29. Five of the seven new specs fail against the
previous revision; the two Class-name specs pass on both and exist to close the coverage gap
rather than pin new behaviour. HibernateConnectionSourceSettingsBuilderSpec gains the real
shipped-configuration case above.

:grails-datastore-core:test                                  SUCCESS
:grails-datamapping-core:test                                SUCCESS
:grails-data-hibernate7-core:test + :grails-data-hibernate5-core:test
                                    3036 tests, 0 failures, 24 skipped
:grails-data-mongodb-core:test  --tests *MongoConnectionSource*  SUCCESS
:grails-datastore-core:codeStyle :grails-data-hibernate7-core:codeStyle  SUCCESS

Neo4j is not in settings.gradle on 8.0.x, so Neo4jDriverConfigBuilder was not exercised.
PMD and SpotBugs tasks are not registered on this branch, so codeStyle (CodeNarc + Checkstyle)
is the analysis gate.

Description needs an update

The "Known limitation" paragraph is now wrong on two counts — the flattening resolver is
affected, and the limitation is fixed rather than deferred. The impact table in #16159 also
still attributes the failure to the @Builder settings trees. Both want rewording before merge.

@jdaugherty

Copy link
Copy Markdown
Contributor

@jamesfredley I'm going to go ahead and merge so we can clear some of these back log PRs. Lets open a PR if you feel we need to iterate on this.

@jdaugherty
jdaugherty merged commit 7ab5673 into 8.0.x Sep 23, 2026
82 checks passed
@jdaugherty
jdaugherty deleted the fix/spring7-nested-map-conversion branch September 23, 2026 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

ConfigurationBuilder fails to bind nested settings under Spring 7

3 participants