Skip to content
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

ConversionService cannot convert primitive array to Object[] #33212

Closed
sbrannen opened this issue Jul 13, 2024 · 1 comment
Closed

ConversionService cannot convert primitive array to Object[] #33212

sbrannen opened this issue Jul 13, 2024 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Jul 13, 2024

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

@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Jul 13, 2024
@sbrannen sbrannen added this to the 6.2.0-M6 milestone Jul 13, 2024
@sbrannen sbrannen self-assigned this Jul 13, 2024
@snicoll snicoll added the type: bug A general bug label Jul 14, 2024
@snicoll snicoll modified the milestones: 6.2.0-M6, 6.2.0-M7 Jul 18, 2024
mcvayc pushed a commit to mcvayc/spring-framework that referenced this issue Jul 31, 2024
Convert if source and target types are not same primitive or non-primitive. Closes spring-projects#33212.
@snicoll
Copy link
Member

snicoll commented Jul 31, 2024

@sbrannen FTR, there was a PR reported against this that I've closed as you were already assigned. In case you haven't started anything and the PR addresses the issue, we can reconsider.

@sbrannen sbrannen modified the milestones: 6.2.0-M7, 6.1.12 Aug 4, 2024
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.0.x labels Aug 4, 2024
sbrannen added a commit that referenced this issue Aug 5, 2024
Prior to this commit, the ConversionService failed to convert a primitive
array (such as int[]) to an Object[] due to an error in the logic in
ArrayToArrayConverter.

This commit addresses this by augmenting the "can bypass conversion"
check in ArrayToArrayConverter to ensure that the supplied source object
is an instance of the target type (i.e., that the source array can be
cast to the target type array without conversion).

Closes gh-33212

(cherry picked from commit cb6a5ba)
sbrannen added a commit that referenced this issue Aug 5, 2024
Prior to this commit, the ConversionService failed to convert a primitive
array (such as int[]) to an Object[] due to an error in the logic in
ArrayToArrayConverter.

This commit addresses this by augmenting the "can bypass conversion"
check in ArrayToArrayConverter to ensure that the supplied source object
is an instance of the target type (i.e., that the source array can be
cast to the target type array without conversion).

Closes gh-33212

(cherry picked from commit cb6a5ba)
(cherry picked from commit 3e73724)
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) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants