Skip to content
This repository has been archived by the owner on Dec 4, 2020. It is now read-only.

Converter Map conversion algorithm is unclear when keys and values may be null #2988

Open
bjhargrave opened this issue Jun 5, 2019 · 2 comments
Assignees

Comments

@bjhargrave
Copy link
Member

Original bug ID: BZ#3120
From: @timothyjward
Reported version: R7

@bjhargrave
Copy link
Member Author

Comment author: @timothyjward

The algorithm for converting a Map-like type is defined in section 707.4.2.6 (https://osgi.org/specification/osgi.cmpn/7.0.0/util.converter.html#util.converter-special.cases.map.entry)

The issue with this is that it is not clear exactly what to do when a null key and/or value is encountered.

Clearly null does not have a type, and therefore step 1 does not apply, but in step 2 this is not the case:

If one of the key or value type is assignable to the target type, then this is used. If both are assignable the key is used.

The "assignability" of null is unclear. Any object reference may be set to null, so in some senses a null key should "win" at this stage. On the other hand an implementation which uses targetClass.isInstance(key) will be told that null is not an instance that can be assigned. Furthermore it isn't possible to use targetClass.isAssignableFrom(...) because there is no source class for null.

From practical experience with the converter (and discussions with others) I would strongly recommend an erratum indicating that null is not considered assignable in step 2. This prevents odd cases like the following:

Map<Integer, Short> map = Collections.singletonMap(null, 5);
Number number = standardConverter.convert(map).to(Number.class);

// This will print null if null is considered assignable, or 5 otherwise
System.out.println(number);

Note that it is now, and still will be, possible for Map -> List conversions to end up with a mixture of keys and values. From a slack discussion with David Bosschaert:

What if you have a map with 3 key-value pairs. (1,“hi”), (2,null),
(3,“ho”) and you convert that to a list of String.

Answer: ["hi", "2", "ho"]

Because null is bad and will cause you pain sometimes!

It is possible to construct examples with non-null keys and values that also cause mixed lists/arrays. The main guarantee is that the list/array will have the same size as the map, and will fail if any conversion fails.

@bjhargrave
Copy link
Member Author

Comment author: @bjhargrave

CPEG call: Assigned to David to investigate.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants