-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Stefan Gheorghiu opened SPR-10887 and commented
As of 3.2.4, the architecture of converters and related environment is very Java-centric and as such doesn't allow extending it with support of other JVM-based languages with different class hierarchies, such as Scala. Scala support provided by the Spring Scala project is rather limited and does not cover many use cases and native idiomatic concepts. Particularly, it is not applicable for binding web request data to controller/form parameters declared as Scala classes. This is not about the mismatch of the bean definitions which can be eliminated by annotations, but mostly about binding to Scala collections and monadic classes like Option.
The problem is caused by the fact that the hierarchy of classes descending from org.springframework.core.convert.AbstractDescriptor is designed in a way that much of intermediate information obtained from the type system with reflection is lost and, due to package-level access scope of classes, there is no "legal" way to capture it somewhere so to later use in custom converters.
Nevertheless, I managed to overcome these design flaws with a minimum intervention into descriptor classes. The main idea was to introduce descriptor extensions ‒— classes globally registered in a static collection of the TypeDescriptor class. When a certain descriptor implementation creates the TypeDescriptor instance, extensions get invoked with access to raw reflection data and can store additional data in a new TypeDesctiptor field of type Map<String,Object>. Custom converters that rely on this additional data can later extract it directly from source/target descriptors passed to their method convert().
The mentioned changes allowed me to implement many problematic scenarios like initialization of Option[Option[Int]] from a String by introducing a descriptor extension that digs deeply into type definition using Scala reflection API and stores this data as a custom attribute of TypeDescriptor. The algorithm is implemented lazily and is only executed when some converter demands the appropriate data.
The improvements can be found in the attachment as a diff based on version 3.2.4. So please consider incorporating them into some future version or at least learn my approach and make something more suitable.
Please don’t ignore this request as it can make Spring Framework more open to Scala adepts who might otherwise choose other frameworks which are more friendly to their language of choice.
Affects: 3.2.4
Attachments:
- descriptor-extensions.diff (3.74 kB)
Referenced from: commits e80b7d1