Add registerReactiveTypeOverride method to ReactiveAdapterRegistry

Closes gh-31047

(cherry picked from commit 389238f622)
This commit is contained in:
Juergen Hoeller
2023-08-14 15:14:22 +02:00
parent f7d4bd176e
commit b9be40ccd2
2 changed files with 66 additions and 6 deletions

View File

@@ -135,16 +135,45 @@ public class ReactiveAdapterRegistry {
* Register a reactive type along with functions to adapt to and from a
* Reactive Streams {@link Publisher}. The function arguments assume that
* their input is neither {@code null} nor {@link Optional}.
* <p>This variant registers the new adapter after existing adapters.
* It will be matched for the exact reactive type if no earlier adapter was
* registered for the specific type, and it will be matched for assignability
* in a second pass if no earlier adapter had an assignable type before.
* @see #registerReactiveTypeOverride
* @see #getAdapter
*/
public void registerReactiveType(ReactiveTypeDescriptor descriptor,
Function<Object, Publisher<?>> toAdapter, Function<Publisher<?>, Object> fromAdapter) {
if (reactorPresent) {
this.adapters.add(new ReactorAdapter(descriptor, toAdapter, fromAdapter));
}
else {
this.adapters.add(new ReactiveAdapter(descriptor, toAdapter, fromAdapter));
}
this.adapters.add(buildAdapter(descriptor, toAdapter, fromAdapter));
}
/**
* Register a reactive type along with functions to adapt to and from a
* Reactive Streams {@link Publisher}. The function arguments assume that
* their input is neither {@code null} nor {@link Optional}.
* <p>This variant registers the new adapter first, effectively overriding
* any previously registered adapters for the same reactive type. This allows
* for overriding existing adapters, in particular default adapters.
* <p>Note that existing adapters for specific types will still match before
* an assignability match with the new adapter. In order to override all
* existing matches, a new reactive type adapter needs to be registered
* for every specific type, not relying on subtype assignability matches.
* @since 5.3.30
* @see #registerReactiveType
* @see #getAdapter
*/
public void registerReactiveTypeOverride(ReactiveTypeDescriptor descriptor,
Function<Object, Publisher<?>> toAdapter, Function<Publisher<?>, Object> fromAdapter) {
this.adapters.add(0, buildAdapter(descriptor, toAdapter, fromAdapter));
}
private ReactiveAdapter buildAdapter(ReactiveTypeDescriptor descriptor,
Function<Object, Publisher<?>> toAdapter, Function<Publisher<?>, Object> fromAdapter) {
return (reactorPresent ? new ReactorAdapter(descriptor, toAdapter, fromAdapter) :
new ReactiveAdapter(descriptor, toAdapter, fromAdapter));
}
/**