-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Create extension point for ArgumentConverter #853
Comments
Please take a look at I'm not sure it makes sense to register However, I think registering additional implicit argument converters makes sense. Can you please provide an API proposal before jumping into the actual coding? |
Thanks for the hint, that worked fine. I was so stuck in the extension model thinking that I applied the custom annotation to the method, not the argument. But my point still stands, then: I have to apply that annotation to every single parameter that wants to use that converter.
Unless I'm missing something that would only be the case if the
Sure but I'm going to focus on my initial idea (extension point for arbitrary converters) as opposed to your suggestion to look into additional implicit argument converters. I think the former can be a superset of the latter. First of all, as soon as converters can be registered in several places, the question of priority and collisions arises. In the end this can be designed any way you want but I think the following order makes sense:
As a consequence the API for converters would be twofold
In both cases I would consider adding the Regarding the implementation, it looks like |
A great addition would be to support the registration of implicit (a.k.a default) converters project wide (similar to extensions) to cut down on test boilerplate. Some examples of common beneficial customizations include Jackson |
Tentatively slated for 5.6 M2 solely for the purpose of team discussion |
Team decision: Waiting for additional interest from the community. |
This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. Thank you for your contribution. |
This issue has been automatically closed due to inactivity. If you have a good use case for this feature, please feel free to reopen the issue. |
I'm also interested in a solution to register certain
It would be great if repetitive argument converters could be used implicitly without polluting the code with annotations. |
@marcphilipp while reworking #3410, I wondered if the extension point mentioned in this issue could be a better way forward for what I reported at #3141, especially considering the reasoning at #3141 (comment). If an argument converter extension point were available, I could globally register a custom argument converter for BCP 47 values without changing JUnit's internals or exposing a new configuration property. The only disadvantage is that each JUnit user affected by #3141 would need to implement their argument converter, but I consider this straightforward. What would be your preference? Should I invest in #3410, or would it be better to develop a proposal for this issue? @nipafx was willing to contribute some years ago, but I'm also happy to look into it if he is no longer available. |
Instead of introducing a new interface, I was thinking how to adapt What if there was a new default method in default boolean canConvert(Object source, ParameterContext context) {
return false;
} The method might be overridden down the hierarchy for better user experience: For example, I imagine Then, Would such a direction make sense? |
Wouldn't returning We could introduce the method in a subinterface, though, e.g. |
My rough idea was to return As I don't have it clear yet where to wire the lookup via What I didn't mention in my previous comment is that I am still wondering if my direction is "too smart" and if it would be better to have the Should I sketch a solution based on the |
I think that would be a good next step. 👍 |
We talked about this issue in our recent team call. Since we introduced Probably something along these lines: public interface ConversionService {
boolean canConvert(Object source, Class<?> targetType, ConversionContext context);
Object convert(Object source, Class<?> targetType, ConversionContext context);
interface ConversionContext {
Optional<AnnotatedElement> getAnnotatedElement();
ClassLoader getClassLoader();
}
} In addition, we'd introduce a new method in public static <T> T convert(Object source, Class<T> targetType, ClassLoader classLoader) {
// ...
} It would first check if there's a @scordio A draft PR for a solution based on the |
Thanks for the update, @marcphilipp! I got sidetracked a bit but I'll raise something by the end of the week. |
As it stands argument converters need to be applied (with
@ConvertWith
) either to a custom annotation (at least it looks like that; couldn't make it work) or to the argument. When the former is not an option (for example because you can't make it work 😉), the latter quickly becomes repetitive.An extension point that allows registering argument converters as the class and method level (much like parameter resolvers) would remedy this problem.
Beyond ease of use, this extension point would also enable creating more wholesome extensions which, applied to a test class, can register parameter resolvers, argument converters, post instance extensions, and more all in one annotation. It would also make it possible to do something like
@ConvertByCalling("fromString")
(either on class, method, or parameter), which would try to call a static methodfromString(String)
on the target type.If you think this is a valuable feature, I'd like to take a shot at implementing it.
The text was updated successfully, but these errors were encountered: