osgi / bugzilla-archive

Archive of OSGi Alliance Specification Bugzilla bugs. The Specification Bugzilla system was decommissioned with the move to GitHub. The issues in this repository are imported from the Specification Bugzilla system for archival purposes.
0 stars 1 forks source link

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

Open bjhargrave opened 4 years ago

bjhargrave commented 4 years ago

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

bjhargrave commented 4 years ago

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 commented 4 years ago

Comment author: @bjhargrave

CPEG call: Assigned to David to investigate.