Skip to content

LinkedCaseInsensitiveMap#putIfAbsent does not honor the case where the key is associated with a null value #26868

Closed
@metaruslan

Description

@metaruslan

Seems to be a bug that LinkedCaseInsensitiveMap#putIfAbsent does not honor the case where the key is associated with a null value.

Here is the javadoc inherited from java.util.Map.

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

Here is the code showing the difference with the normal map:

        Map<String, String> normalMap = new HashMap<>();
        Map<String, String> caseInsensitiveMap = new LinkedCaseInsensitiveMap<>();
        normalMap.put("key", null);
        System.out.println("normal map:" + normalMap);
        normalMap.putIfAbsent("key", "value");
        System.out.println("normal map:" + normalMap);

        caseInsensitiveMap.put("key", null);
        System.out.println("case insensitive map" + caseInsensitiveMap);
        caseInsensitiveMap.putIfAbsent("key", "value");
        System.out.println("case insensitive map" + caseInsensitiveMap);

Output:

normal map:{key=null}
normal map:{key=value}
case insensitive map{key=null}
case insensitive map{key=null}

According to the javadoc it should return null and make the change, but the code only returns null. The line: return this.targetMap.get(oldKey).

The implementation:

        @Override
	@Nullable
	public V putIfAbsent(String key, @Nullable V value) {
		String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
		if (oldKey != null) {
			return this.targetMap.get(oldKey);
		}
		return this.targetMap.putIfAbsent(key, value);
	}

Spring version: 5.3.5

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions