Skip to content

Commit edea67f

Browse files
committed
Context is not available through dependency injection
-fix #3764
1 parent 5a850fc commit edea67f

File tree

4 files changed

+77
-9
lines changed

4 files changed

+77
-9
lines changed

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,8 @@ public Jooby setStartupSummary(List<StartupSummary> startupSummary) {
905905

906906
joobyRunHook(getClass().getClassLoader(), server);
907907

908+
router.initialize();
909+
908910
for (Extension extension : lateExtensions) {
909911
try {
910912
extension.install(this);

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ private void configureTrustProxy(boolean trustProxy) {
246246
}
247247
}
248248

249+
private void configureContextAsService(boolean contextAsService) {
250+
if (contextAsService) {
251+
addPostDispatchInitializer(ContextAsServiceInitializer.INSTANCE);
252+
getServices().put(Context.class, ContextAsServiceInitializer.INSTANCE);
253+
} else {
254+
removePostDispatchInitializer(ContextAsServiceInitializer.INSTANCE);
255+
}
256+
}
257+
249258
@NonNull @Override
250259
public Route.Set domain(@NonNull String domain, @NonNull Runnable body) {
251260
return mount(domainPredicate(domain), body);
@@ -534,10 +543,13 @@ private void pureAscii(String pattern, Consumer<String> consumer) {
534543
}
535544
}
536545

537-
@NonNull public Router start(@NonNull Jooby app, @NonNull Server server) {
538-
started = true;
546+
public void initialize() {
539547
configureTrustProxy(routerOptions.isTrustProxy());
540548
configureContextAsService(routerOptions.isContextAsService());
549+
}
550+
551+
@NonNull public Router start(@NonNull Jooby app, @NonNull Server server) {
552+
started = true;
541553
var globalErrHandler = defineGlobalErrorHandler(app);
542554
if (err == null) {
543555
err = globalErrHandler;
@@ -610,13 +622,6 @@ private void pureAscii(String pattern, Consumer<String> consumer) {
610622
return this;
611623
}
612624

613-
private void configureContextAsService(boolean contextAsService) {
614-
if (contextAsService) {
615-
addPostDispatchInitializer(ContextAsServiceInitializer.INSTANCE);
616-
getServices().put(Context.class, ContextAsServiceInitializer.INSTANCE);
617-
}
618-
}
619-
620625
/**
621626
* Define the global error handler. If ProblemDetails is enabled the {@link ProblemDetailsHandler}
622627
* instantiated from configuration settings and returned. Otherwise, {@link DefaultErrorHandler}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3764;
7+
8+
import io.jooby.Context;
9+
import io.jooby.annotation.GET;
10+
import jakarta.inject.Inject;
11+
12+
public class C3764 {
13+
14+
private final Context ctx;
15+
16+
@Inject
17+
public C3764(Context ctx) {
18+
this.ctx = ctx;
19+
}
20+
21+
@GET("/3764")
22+
public String path() {
23+
return ctx.getRequestPath();
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3764;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
import io.jooby.RouterOptions;
11+
import io.jooby.guice.GuiceModule;
12+
import io.jooby.junit.ServerTest;
13+
import io.jooby.junit.ServerTestRunner;
14+
15+
public class Issue3764 {
16+
@ServerTest
17+
public void shouldGetContextAsService(ServerTestRunner runner) {
18+
runner
19+
.define(
20+
app -> {
21+
app.setRouterOptions(new RouterOptions().setContextAsService(true));
22+
23+
app.install(new GuiceModule());
24+
25+
app.mvc(new C3764_());
26+
})
27+
.ready(
28+
http -> {
29+
http.get(
30+
"/3764",
31+
rsp -> {
32+
assertEquals("/3764", rsp.body().string());
33+
});
34+
});
35+
}
36+
}

0 commit comments

Comments
 (0)