spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.5k stars 38.1k forks source link

Support conversion from multiple source objects to a single target object in Converter API #24798

Closed ssgtcookie closed 4 years ago

ssgtcookie commented 4 years ago

Spring Boot 2.2.5.RELEASE

Interfaces:

Currently the Spring converter only supports the signature <S, T> so you could do something like this with it:

Convert A to C

But.. What if we need another object, say B, to convert to C. In my case I need this:

Convert A and B to C

This is not possible but it would be very nice if it was flexible enough to also handle this requirement. In this feature request I would like to request support for the ConversionService to handle more objects if needed so the Converter is more flexible.

I found this post on stackoverflow where some others also seeking for this flexibility.

sbrannen commented 4 years ago

Team Decision: This is beyond the scope of Spring's core conversion support. In light of that, I am closing this issue.

As discussed in the linked Stack Overflow answer, there are alternatives for achieving something similar for advanced use cases.

Thanks for the proposal in any case.

ssgtcookie commented 4 years ago

Ok thanks for the quick response @sbrannen.

I do not fully agree with the team that there are "good" alternatives for this problem. The solutions offered in the stackoverflow post are indeed all workarounds for the problem, but they all have their shortcomings so there is no good alternative.

Let's take this example: Convert Apples, Flour, Sugar, Salt, Butter, Cinnamon, Eggs to ApplePie.

Now one of the solutions in the stack overflow post is to create Pairs. So let's do that: Convert Pair<Apple, Pair<Flour, Pair<Sugar, Pair<Salt, Pair<Butter, Cinnamon>>>> to ApplePie

Well okay, I don't know what you guys think about this solution but.. let me tell you how I think about it: it's bad. Another solution is to use a state object:

public class Ingredients {
private Flour;
private Sugar;
private Salt;
private Butter;
private Cinnamon;
}

Now we can do: Conveter Ingredients to ApplePie.

Which looks better than the Pair solution, but you would only need to create this class to patch the shortcomings of the Spring interface. It would also mean more code maintenance since this class needs to be updated, unit testing etc. and it's only there to solve the interface problem in the spring framework, it has no other purpose.

So last but not least there are some "not spring" solutions in the stack overflow page offered with plain java. Everything spring does can be done in plain java so yes, I can create my own converter besides Spring. However, I don't think that the goal of the spring converter is that people write their own pojo converters. I would say that supporting multiple objects in the converter is in scope and I think that the fact that is does not support it right now, is a shortcoming in the implementation of the converter and how it was made. It should have supported multiple objects from the beginning to make it easy for the developer and fully reach the purpose it has. But it's not too late.

Of course the final decision is up to you guys, just sharing my thoughts on the decision. Thanks! :D