spring-projects / spring-data-commons

Spring Data Commons. Interfaces and code shared between the various datastore specific implementations.

Home Page:https://spring.io/projects/spring-data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`TypeDiscoverer` uses single-level nesting for generics comparison

jinganix opened this issue · comments

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

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

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.

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.