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

TypeDiscoverer uses single-level nesting for generics comparison #3051

Closed
jinganix opened this issue Feb 23, 2024 · 3 comments
Closed

TypeDiscoverer uses single-level nesting for generics comparison #3051

jinganix opened this issue Feb 23, 2024 · 3 comments
Assignees
Labels
type: regression A regression from a previous release

Comments

@jinganix
Copy link

DEMO repo: https://github.com/jinganix/spring-data-mongodb-issue

When running test in ConverterTest.java, it will throw error:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.math.BigDecimal] for value [Bar]

There is no error if we removed this line: https://github.com/jinganix/spring-data-mongodb-issue/blob/296bd2c3a801f54f35e794dec2a3af12229f6a54/src/test/java/com/example/demo/model/FooCollection.java#L13

please have a check

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 23, 2024
@myob-hankun
Copy link

Hi Team,
Do you have any solution for this issue?

@mp911de mp911de self-assigned this Feb 26, 2024
@mp911de
Copy link
Member

mp911de commented Feb 26, 2024

This looks like a Spring Data Commons issue as we're creating the entity for DataBase using a type descriptor of com.example.demo.model.DataBase<java.util.List<java.util.Map<java.lang.String, java.math.BigDecimal>>> (instead of com.example.demo.model.DataBase<?> or even com.example.demo.model.DataBase) so I'm moving the ticket into Commons.

Another aspect is that we work on an Entity level. Component types are only retained for collections and map types, but not for custom types as a type of Foo<T> or Foo<Foo<T>> refers to the type Foo and during creation of the type, we do not have access to the enclosing context anymore.

@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 26, 2024
@mp911de mp911de transferred this issue from spring-projects/spring-data-mongodb Feb 26, 2024
@mp911de mp911de changed the title org.springframework.core.convert.ConversionFailedException TypeDiscoverer uses single-level nesting for generics comparison Feb 27, 2024
@mp911de
Copy link
Member

mp911de commented Feb 27, 2024

The problem comes from TypeDiscoverer that resolves generics to a Class and then compares classes for equality. Reducing the type to Class erases the inner parameters (MyContainer<List<String>> vs MyContainer<List<Map<…>>) to List.class for both cases and therefore, the comparison falsely matches.

@mp911de mp911de added type: regression A regression from a previous release and removed type: bug A general bug labels Feb 27, 2024
mp911de added a commit that referenced this issue Feb 27, 2024
We now compare nested generics wrapped into TypeInformation to consider type equality for deeply parametrized types.

Previously, we resolved type parameters to Class so Foo<List<String>> was considered equal to Foo<List<Map>> as the type parameter of the first nesting level was erased.

Closes #3051
mp911de added a commit that referenced this issue Feb 27, 2024
We now compare nested generics wrapped into TypeInformation to consider type equality for deeply parametrized types.

Previously, we resolved type parameters to Class so Foo<List<String>> was considered equal to Foo<List<Map>> as the type parameter of the first nesting level was erased.

Closes #3051
@mp911de mp911de added this to the 3.1.10 (2023.0.10) milestone Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

4 participants