Description
Joris Kuipers opened DATACMNS-583 and commented
I ran into a very peculiar issue today, that was hard to reproduce. In a Spring-MVC app that uses Spring Data JPA and has a DomainClassConverter registered I have a controller method that takes some entity as a model attribute parameter. The entity has a String ID field.
What happens now in some cases (haven't identified the exact circumstances) is that the entity instance, which is initially created and bound correctly, is ran through the conversion service, which finds the DomainClassConverter which will happily map the entity instance onto another instance of the same type by mapping the instance to a String using the standard ObjectToStringConverter and treating the result as the primary key of the entity type... This results in always getting null passed in as the parameter for the controller method.
Obviously the DomainClassConverter should not be used at all in this scenario.
Fortunately I was able to reproduce the scenario in a simple app (see attachment). It's really weird: if you remove the extra non-default constructor of the Entity class, for example, you can no longer reproduce the error. Also, if you comment out the spring.version property in the pom, thereby using 4.0.7.RELEASE instead of 4.1.1.RELEASE, you can also not reproduce the error. This might therefore be related to a recent change in Spring Core as well.
Anyways, there is an easy fix for this issue: the DomainClassConvert#matches method should first check if the source- and targetType are equal and if so, return false: I'd say that no domain class can have itself as its key type and thus it should never attempt to convert these types.
If you manage to pinpoint the exact cause you might find that there's a better solution, but this is how I worked around this issue for now
Affects: 1.8.4 (Dijkstra SR4)
Attachments:
- domainclassconverter-bug.zip (4.82 kB)
Referenced from: pull request #101
Backported to: 1.9.1 (Evans SR1), 1.8.5 (Dijkstra SR5)