-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Improve configuration API of ClientCodecConfigurer.CustomCodecs #24124
Comments
There is no one contract for codecs to implement for setting these properties and it would be awkward to introduce one, and we'd likely need multiple since some properties apply to some codecs only. That said what we could do is to add In addition we could add an overloaded How does that sound? |
That'll cover the particular usecase that we have in Spring HATEOAS and sounds helpful in general. Would that also add the given decoder to the
Renaming is appreciated. I have to admit I got a bit lost in all the methods that take a callback as you end up declaring a callback that then calls a method taking the next callback. While this can be explored, it's not easy to keep a mental model of what's applied when and the resulting code looks verbose and is not easy to understand I think. See this example: // Given a custom decoder instance
AbstractJackson2Decoder decoder = …;
// I currently have to write this to get the default applied
….exchangeStrategies(strategies -> strategies.codecs(codecs -> {
codecs.customCodecs().withDefaultCodecConfig(config -> {
Integer maxInMemorySize = config.maxInMemorySize();
if (maxInMemorySize != null) {
decoder.setMaxInMemorySize(maxInMemorySize);
}
});
}); That's quite verbose I think. |
I suppose we could make it a register-and-apply but I'd rather keep the 4 existing registration methods and then have two |
Thanks, Rossen. Happy to rewrite that bit of code in case I have overseen anything that could make this shorter. At least to me the presence of the |
Sorry, it sounded like the renaming + the new apply method would be sufficient. It should now be:
I don't quite follow the comment about prematurely triggering anything. If you could elaborate I can make sure it's properly reflected in the Javadoc, or is it more than that? I can do a pass soon as well so we can be looking something more concrete. |
Whenever I see an overload taking a With That said, it still takes (me) quite a bit of getting used to to these verb-less methods that do something to the underlying instance as they don't communicate what's happening: " Something along the lines of: WebClient.builder()
.exchangeStrategies(builder -> builder.registerWithDefaultConfig(decoder))
.build(); would feel more use case oriented rather than exposing all the internal structure of the configuration and letting the user dig down the abstractions step by step. |
Not really. In this case it's simply to let you apply changes to a common
Maybe we need to be more explicit about this but you shouldn't have to guess. Always use the
My concern is that For the nesting I agree it's unfortunate to go 3 levels but WebClient.builder()
.codecs(codecs -> {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
codecs.customCodecs().registerWithDefaultConfig(decoder, encoder);
})
.build(); |
That looks nice, much better than before. Thanks! |
The new register methods replace the now deprecated encoder, decoder, reader, and writer methods, and also offer a choice to opt into default properties such maxInMemorySize, if configured. See gh-24124
The 5.2.2 release includes new API to allow custom decoders to be registered to use the default configuration for decoders. There are a few things that could be improved about that.
withDefaultConfig(…)
and takes aConsumer
. Wither methods usually return a new instance of the object being called on taking the argument into account. This one here simply provides a configuration abstraction I now have to use to configure my own decoder.DefaultCodecConfig
'smaxInMemorySize()
returns a nullableInteger
.setMaxInMemorySize(…)
takes anint
. Looks like everyone trying to use those methods will now have to repeat the samenull
check before copying the value over.DefaultCodecConfig
. As it's user code that is responsible to copy properties from left to right, new options will not automatically propagated unless the developer finds out about that new property and adds the necessary line of copying.Have you though about a
CustomCodecs.decoderWithDefaultConfig(…)
that register the givenDecoder
applying all applicable defaults? That especially would free developers from having to play catch up with new options added and also resolve thenull
checks needed for every client.The text was updated successfully, but these errors were encountered: