Skip to content

Commit

Permalink
Revise commit #52d068 with corrected test
Browse files Browse the repository at this point in the history
Update test detecting RouterFunction beans in parent contexts to use
different bean names and avoid shadowing. Changed the fix accordingly
given that BeanProvider does detect beans in parent contexts.

See gh-28595
  • Loading branch information
rstoyanchev committed Jun 14, 2022
1 parent d28d603 commit 8c77711
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
Expand Up @@ -154,20 +154,19 @@ public void afterPropertiesSet() throws Exception {
* current application context.
*/
private void initRouterFunctions() {
List<RouterFunction<?>> routerFunctions = new ArrayList<>();
detectRouterFunctions(obtainApplicationContext(), routerFunctions);
this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null);
logRouterFunctions(routerFunctions);
}

private void detectRouterFunctions(ApplicationContext context, List<RouterFunction<?>> routerFunctions) {
if (this.detectHandlerFunctionsInAncestorContexts && context.getParent() != null) {
detectRouterFunctions(context.getParent(), routerFunctions);
}
context.getBeanProvider(RouterFunction.class)
List<RouterFunction<?>> routerFunctions = obtainApplicationContext()
.getBeanProvider(RouterFunction.class)
.orderedStream()
.map(router -> (RouterFunction<?>) router)
.collect(Collectors.toCollection(() -> routerFunctions));
.collect(Collectors.toList());

ApplicationContext parentContext = obtainApplicationContext().getParent();
if (parentContext != null && !this.detectHandlerFunctionsInAncestorContexts) {
parentContext.getBeanProvider(RouterFunction.class).stream().forEach(routerFunctions::remove);
}

this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null);
logRouterFunctions(routerFunctions);
}

private void logRouterFunctions(List<RouterFunction<?>> routerFunctions) {
Expand Down
Expand Up @@ -98,16 +98,16 @@ void detectHandlerFunctionsInAncestorContexts(boolean detect) throws Exception {
HandlerFunction<ServerResponse> function3 = request -> ServerResponse.ok().build();

AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext();
context1.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn1", function1).build());
context1.registerBean("fn1", RouterFunction.class, () -> RouterFunctions.route().GET("/fn1", function1).build());
context1.refresh();

AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext();
context2.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn2", function2).build());
context2.registerBean("fn2", RouterFunction.class, () -> RouterFunctions.route().GET("/fn2", function2).build());
context2.setParent(context1);
context2.refresh();

AnnotationConfigApplicationContext context3 = new AnnotationConfigApplicationContext();
context3.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn3", function3).build());
context3.registerBean("fn3", RouterFunction.class, () -> RouterFunctions.route().GET("/fn3", function3).build());
context3.setParent(context2);
context3.refresh();

Expand Down

0 comments on commit 8c77711

Please sign in to comment.