Skip to content

AutoGrow not applied for nested map values #32154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ejnarvala opened this issue Jan 29, 2024 · 1 comment
Open

AutoGrow not applied for nested map values #32154

ejnarvala opened this issue Jan 29, 2024 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement

Comments

@ejnarvala
Copy link

ejnarvala commented Jan 29, 2024

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 29, 2024
@jhoeller jhoeller self-assigned this Jan 30, 2024
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Jan 30, 2024
@jhoeller
Copy link
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.

@jhoeller jhoeller changed the title AutoGrow not applied for nested collections AutoGrow not applied for nested map values Jan 30, 2024
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 30, 2024
@jhoeller jhoeller added this to the 6.1.4 milestone Jan 30, 2024
@jhoeller jhoeller modified the milestones: 6.1.4, 6.2.x Feb 6, 2024
@jhoeller jhoeller modified the milestones: 6.2.x, General Backlog Sep 25, 2024
@jhoeller jhoeller removed their assignment Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants