Skip to content

ConversionService cannot convert primitive array to Object[] #33212

Closed
@sbrannen

Description

@sbrannen

Overview

The DefaultConversionService currently cannot convert a primitive array to Object[]. This is due to the use of GenericConversionService.canBypassConvert(...) in ArrayToArrayConverter which assumes a conversion from int[] (or any other primitive array) to Object[] is unnecessary because "an int (or other primitive type) can be converted to an Object by returning the source object unchanged," although the latter is not true for primitives.

Thus an attempt to convert from int[] to Object[] returns the source int[] array unchanged and incompatible with Object[].

This strikes me as a bit strange since the following conversions (and other similar conversions) are all supported. See the various convert*() test methods in DefaultConversionServiceTests.

  • int[] --> Integer[]
  • int[] --> String[]
  • int[] --> float[]
  • Object[] --> Integer[]
  • Object[] --> int[]

The latter makes it clear that you can convert from Object[] to int[] but not from int[] to Object[].

Note that this also affects our varargs support in SpEL expressions. See the various @Disabled tests in ae5dd54 for examples.

We should decide if we want to support conversions from primitive arrays to Object[] and otherwise consider documenting this as a known limitation.

Related Issues

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions