Skip to content

AutoGrow not applied for nested map values #32154

Open
@ejnarvala

Description

@ejnarvala

Affects: 6.0.8


Language: Kotlin
Spring Version: 6.0.8

I am using data binding for parsing form data into a form object. When using a singly nested map (Map<String, Map<String, String>), I find that using the key property[map1key][map2key] works as expected to set the nested value, but when adding another layer, e.g. Map<String, Map<String, Map<String, String>>> with key property[map1key][map2key][map3key] this fails with an exception cannot access indexed value of property referenced in indexed property path nested map.

Looking into the source code, I believe I tracked it down to the AbstractNestablePropertyAccessor.getPropertyValue(PropertyTokenHolder tokens) method which seems to only set the default value on the first token which explains why this works for a singly nested map but not a double or greater one. I suspect this affects any type of auto growing collection as well.

relevant snippet:

			if (tokens.keys != null) {
				if (value == null) {
					if (isAutoGrowNestedPaths()) {
						value = setDefaultValue(new PropertyTokenHolder(tokens.actualName));
					}
					else {
						throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
								"Cannot access indexed value of property referenced in indexed " +
										"property path '" + propertyName + "': returned null");
					}
				}
				StringBuilder indexedPropertyName = new StringBuilder(tokens.actualName);
				// apply indexes and map keys
				for (int i = 0; i < tokens.keys.length; i++) {
					String key = tokens.keys[i];
					if (value == null) {
						throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
								"Cannot access indexed value of property referenced in indexed " +
										"property path '" + propertyName + "': returned null");
					}

As a workaround, I'm using a single map with a custom key that I deserialize manually and then convert to a nested map in application code, but it would be nice if this was supported as part of the data binding.

Activity

self-assigned this
on Jan 30, 2024
added
in: coreIssues in core modules (aop, beans, core, context, expression)
on Jan 30, 2024
jhoeller

jhoeller commented on Jan 30, 2024

@jhoeller
Contributor

As far as I was able to reproduce this, the case only applies to multi-nested Map values. There is a dedicated code path for auto-growing map values in getPropertyHoldingValue which currently assumes one level only.

changed the title [-]AutoGrow not applied for nested collections[/-] [+]AutoGrow not applied for nested map values[/+] on Jan 30, 2024
added this to the 6.1.4 milestone on Jan 30, 2024
modified the milestones: 6.1.4, 6.2.x on Feb 6, 2024
removed their assignment
on Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jhoeller@spring-projects-issues@ejnarvala

        Issue actions

          AutoGrow not applied for nested map values · Issue #32154 · spring-projects/spring-framework