mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 08:24:13 +00:00
AbstractNestablePropertyAccessor resolves a nested property path recursively, one recursive call per path segment, and there was previously no limit on the nesting depth. Consequently, a sufficiently deeply nested property path -- for example, against a self-referential type -- could exhaust the current thread's call stack, resulting in a StackOverflowError which lacks useful diagnostics for developers attempting to assess what went wrong. Note that the existing autoGrowCollectionLimit bounds array and collection growth, not path depth. The same is true for constructor binding via DataBinder.construct(), which constructs a nested constructor argument recursively through a nested property path. Path segments are constrained to declared constructor parameters there, but a self-referential type nonetheless permits an arbitrarily deep path. With this commit, each property accessor tracks the number of nested properties traversed to reach the object that it wraps, and an InvalidPropertyException is thrown once the configured (or default) maxNestedPathDepth limit is exceeded, with a message that reports the configured limit. The limit applies regardless of autoGrowNestedPaths, since resolving an existing deep object graph recurses in the same manner as auto-growing one. Tracking the depth per property accessor rather than threading it through the recursion allows the recursion to dispatch through the protected getPropertyAccessorForPropertyPath(String) method, which subclasses may override, and avoids deriving the depth from the nested path, which would require rescanning an ever longer path prefix at each level. Constructor binding likewise tracks the nesting depth while constructing nested objects as well as indexed and mapped elements, and throws the same InvalidPropertyException once the limit is exceeded. The maxNestedPathDepth (which defaults to 100) can be configured on a per-use-case basis via ConfigurablePropertyAccessor or DataBinder, which applies it to constructor binding directly and supplies it to the property accessor via its binding result. In contrast to the auto-grow collection limit, which is unlimited on a plain accessor, the nesting depth is bounded by default even for programmatic property access, since a large array or collection can be perfectly legitimate whereas a deeply nested property path effectively never is. Specifying zero for the maxNestedPathDepth disables support for nested property paths altogether while continuing to allow simple, indexed, and mapped property access, which is a reasonable way to constrain data binding for a target object that is not intended to be traversed (such as a flat DTO). However, negative values for maxNestedPathDepth are always rejected. Closes gh-37252