Skip to content

Commit a71a3e3

Browse files
committed
Support Javalin 7
1 parent e846a2a commit a71a3e3

File tree

6 files changed

+111
-100
lines changed

6 files changed

+111
-100
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/ControllerReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,6 @@ public static String sanitizeImports(String type) {
418418
}
419419

420420
private static String trimArrayBrackets(String type) {
421-
return type.replaceAll("[^\\n\\r\\t $;\\w.]", "");
421+
return type.replaceAll("[^\\n\\r\\t $;\\w.*]", "");
422422
}
423423
}

http-generator-core/src/main/java/io/avaje/http/generator/core/ProcessingContext.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ private static final class Ctx {
5555
private final boolean instrumentAllMethods;
5656
private final boolean disableDirectWrites;
5757
private final boolean disableJsonB;
58-
private final boolean javalin6;
5958
private final Set<String> clientFQN = new HashSet<>();
6059

6160
Ctx(ProcessingEnvironment env, PlatformAdapter adapter, boolean generateOpenAPI) {
@@ -90,7 +89,6 @@ private static final class Ctx {
9089
} else {
9190
useJavax = (javax);
9291
}
93-
this.javalin6 = elementUtils.getTypeElement("io.javalin.config.RouterConfig") != null;
9492
}
9593
}
9694

@@ -220,10 +218,6 @@ public static boolean disabledDirectWrites() {
220218
return CTX.get().disableDirectWrites;
221219
}
222220

223-
public static boolean javalin6() {
224-
return CTX.get().javalin6;
225-
}
226-
227221
public static Filer filer() {
228222
return CTX.get().filer;
229223
}

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/ControllerWriter.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import java.io.IOException;
66
import java.util.Map;
77

8+
import io.avaje.http.generator.core.APContext;
89
import io.avaje.http.generator.core.BaseControllerWriter;
910
import io.avaje.http.generator.core.Constants;
1011
import io.avaje.http.generator.core.ControllerReader;
1112
import io.avaje.http.generator.core.JsonBUtil;
1213
import io.avaje.http.generator.core.MethodReader;
1314
import io.avaje.http.generator.core.PrimitiveUtil;
14-
import io.avaje.http.generator.core.ProcessingContext;
1515
import io.avaje.http.generator.core.UType;
1616

1717
/**
@@ -22,7 +22,8 @@ class ControllerWriter extends BaseControllerWriter {
2222
private static final String AT_GENERATED = "@Generated(\"avaje-javalin-generator\")";
2323
private final boolean useJsonB;
2424
private final Map<String, UType> jsonTypes;
25-
private final boolean javalin6 = ProcessingContext.javalin6();
25+
private static final boolean javalin7 = APContext.typeElement("io.javalin.config.JavalinState") != null;
26+
private static final boolean javalin6 = APContext.typeElement("io.javalin.config.RouterConfig") != null;
2627

2728
ControllerWriter(ControllerReader reader, boolean jsonb) throws IOException {
2829
super(reader);
@@ -31,9 +32,9 @@ class ControllerWriter extends BaseControllerWriter {
3132
this.jsonTypes = detectJsonB.jsonTypes();
3233

3334
reader.addImportType("io.javalin.plugin.Plugin");
34-
if (javalin6) {
35-
reader.addImportType("io.javalin.config.JavalinConfig");
36-
reader.addImportType("io.javalin.router.JavalinDefaultRouting");
35+
if (javalin6 || javalin7) {
36+
reader.addImportType("io.javalin.config.*");
37+
reader.addImportType("io.javalin.router.*");
3738
reader.addImportType("io.avaje.http.api.AvajeJavalinPlugin");
3839
} else {
3940
reader.addImportType("io.javalin.Javalin");
@@ -50,7 +51,13 @@ void write() {
5051

5152
private void writeAddRoutes() {
5253
writer.append(" @Override").eol();
53-
if (javalin6) {
54+
55+
if (javalin7) {
56+
writer.append(" public void onStart(JavalinState state) {").eol();
57+
writer.append(" routes(state.routes);").eol();
58+
writer.append(" }").eol().eol();
59+
writer.append(" private void routes(RoutesConfig app) {").eol().eol();
60+
} else if (javalin6) {
5461
writer.append(" public void onStart(JavalinConfig cfg) {").eol();
5562
writer.append(" cfg.router.mount(this::routes);").eol();
5663
writer.append(" }").eol().eol();

tests/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
<assertj.version>3.27.6</assertj.version>
1717
<jackson.version>2.20.1</jackson.version>
1818
<jex.version>3.3</jex.version>
19-
<avaje-inject.version>12.2</avaje-inject.version>
19+
<avaje-inject.version>12.3-RC1</avaje-inject.version>
2020
<helidon.version>4.3.3</helidon.version>
21-
<javalin.version>6.7.0</javalin.version>
21+
<javalin.version>7.0.0-beta.1</javalin.version>
2222
</properties>
2323

2424
<modules>
@@ -48,19 +48,19 @@
4848
<dependency>
4949
<groupId>io.avaje</groupId>
5050
<artifactId>avaje-validator</artifactId>
51-
<version>2.15</version>
51+
<version>2.16-RC2</version>
5252
</dependency>
5353

5454
<dependency>
5555
<groupId>io.avaje</groupId>
5656
<artifactId>avaje-validator-constraints</artifactId>
57-
<version>2.15</version>
57+
<version>2.16-RC2</version>
5858
</dependency>
5959

6060
<dependency>
6161
<groupId>io.avaje</groupId>
6262
<artifactId>avaje-validator-generator</artifactId>
63-
<version>2.15</version>
63+
<version>2.16-RC2</version>
6464
</dependency>
6565

6666
</dependencies>

tests/test-javalin-jsonb/src/main/java/org/example/myapp/Main.java

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.example.myapp;
22

33
import java.util.LinkedHashMap;
4-
import java.util.List;
54
import java.util.Map;
65

76
import org.slf4j.Logger;
@@ -14,20 +13,27 @@
1413
import io.avaje.http.api.Validator;
1514
import io.avaje.inject.BeanScope;
1615
import io.avaje.inject.InjectModule;
17-
import io.avaje.inject.spi.GenericType;
1816
import io.javalin.Javalin;
19-
import io.javalin.config.JavalinConfig;
2017
import io.javalin.http.staticfiles.Location;
21-
import io.javalin.plugin.Plugin;
2218
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
2319
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
2420
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
2521
import io.swagger.v3.oas.annotations.info.Info;
2622
import io.swagger.v3.oas.annotations.security.SecurityScheme;
2723

2824
@InjectModule(name = "app", requires = Validator.class)
29-
@OpenAPIDefinition(info = @Info(title = "Example service", description = "Example Javalin controllers with Java and Maven"))
30-
@SecurityScheme(type = SecuritySchemeType.APIKEY, in = SecuritySchemeIn.QUERY, name = "JWT", paramName = "access_token", description = "JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.")
25+
@OpenAPIDefinition(
26+
info =
27+
@Info(
28+
title = "Example service",
29+
description = "Example Javalin controllers with Java and Maven"))
30+
@SecurityScheme(
31+
type = SecuritySchemeType.APIKEY,
32+
in = SecuritySchemeIn.QUERY,
33+
name = "JWT",
34+
paramName = "access_token",
35+
description =
36+
"JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.")
3137
public class Main {
3238

3339
private static final Logger log = LoggerFactory.getLogger(Main.class);
@@ -45,47 +51,50 @@ public static Javalin start(int port) {
4551
final var app =
4652
Javalin.create(
4753
config -> {
48-
config.showJavalinBanner = false;
4954
config.staticFiles.add("public", Location.CLASSPATH);
5055
// config.accessManager(
5156
// (handler, ctx, permittedRoles) -> {
5257
// log.debug("allow access ...");
5358
// handler.handle(ctx);
5459
// });
5560
webRoutes.forEach(config::registerPlugin);
56-
});
57-
58-
app.exception(ValidationException.class, (exception, ctx) -> {
59-
60-
final Map<String, Object> map = new LinkedHashMap<>();
61-
map.put("message", exception.getMessage());
62-
map.put("errors", exception.getErrors());
63-
ctx.json(map);
64-
ctx.status(exception.getStatus());
65-
});
66-
67-
app.exception(InvalidTypeArgumentException.class, (exception, ctx) -> {
68-
69-
final Map<String, String> map = new LinkedHashMap<>();
70-
map.put("path", ctx.path());
71-
map.put("message", "invalid type argument");
72-
ctx.json(map);
73-
ctx.status(400);
74-
});
7561

76-
app.exception(InvalidPathArgumentException.class, (exception, ctx) -> {
77-
78-
final Map<String, String> map = new LinkedHashMap<>();
79-
map.put("path", ctx.path());
80-
map.put("message", "invalid path argument");
81-
ctx.json(map);
82-
ctx.status(404);
83-
});
84-
85-
86-
app.get("/", ctx -> {
87-
ctx.result("Hello World");
88-
});
62+
config.routes.exception(
63+
ValidationException.class,
64+
(exception, ctx) -> {
65+
final Map<String, Object> map = new LinkedHashMap<>();
66+
map.put("message", exception.getMessage());
67+
map.put("errors", exception.getErrors());
68+
ctx.json(map);
69+
ctx.status(exception.getStatus());
70+
});
71+
72+
config.routes.exception(
73+
InvalidTypeArgumentException.class,
74+
(exception, ctx) -> {
75+
final Map<String, String> map = new LinkedHashMap<>();
76+
map.put("path", ctx.path());
77+
map.put("message", "invalid type argument");
78+
ctx.json(map);
79+
ctx.status(400);
80+
});
81+
82+
config.routes.exception(
83+
InvalidPathArgumentException.class,
84+
(exception, ctx) -> {
85+
final Map<String, String> map = new LinkedHashMap<>();
86+
map.put("path", ctx.path());
87+
map.put("message", "invalid path argument");
88+
ctx.json(map);
89+
ctx.status(404);
90+
});
91+
92+
config.routes.get(
93+
"/",
94+
ctx -> {
95+
ctx.result("Hello World");
96+
});
97+
});
8998

9099
app.start(port);
91100
return app;

tests/test-javalin/src/main/java/org/example/myapp/Main.java

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.example.myapp;
22

33
import java.util.LinkedHashMap;
4-
import java.util.List;
54
import java.util.Map;
65

76
import org.slf4j.Logger;
@@ -11,7 +10,6 @@
1110
import io.avaje.http.api.InvalidPathArgumentException;
1211
import io.avaje.http.api.InvalidTypeArgumentException;
1312
import io.avaje.http.api.ValidationException;
14-
import io.avaje.http.api.Validator;
1513
import io.avaje.inject.BeanScope;
1614
import io.avaje.inject.InjectModule;
1715
import io.javalin.Javalin;
@@ -20,7 +18,11 @@
2018
import io.swagger.v3.oas.annotations.info.Info;
2119

2220
@InjectModule(name = "app")
23-
@OpenAPIDefinition(info = @Info(title = "Example service", description = "Example Javalin controllers with Java and Maven"))
21+
@OpenAPIDefinition(
22+
info =
23+
@Info(
24+
title = "Example service",
25+
description = "Example Javalin controllers with Java and Maven"))
2426
public class Main {
2527

2628
private static final Logger log = LoggerFactory.getLogger(Main.class);
@@ -38,51 +40,50 @@ public static Javalin start(int port) {
3840
final var app =
3941
Javalin.create(
4042
config -> {
41-
config.showJavalinBanner = false;
4243
config.staticFiles.add("public", Location.CLASSPATH);
43-
// config.accessManager(
44-
// (handler, ctx, permittedRoles) -> {
45-
// log.debug("allow access ...");
46-
// handler.handle(ctx);
47-
// });
44+
// config.accessManager(
45+
// (handler, ctx, permittedRoles) -> {
46+
// log.debug("allow access ...");
47+
// handler.handle(ctx);
48+
// });
4849
webRoutes.forEach(config::registerPlugin);
49-
});
50-
51-
app.exception(
52-
ValidationException.class,
53-
(exception, ctx) -> {
54-
final Map<String, Object> map = new LinkedHashMap<>();
55-
map.put("message", exception.getMessage());
56-
map.put("errors", exception.getErrors());
57-
ctx.json(map);
58-
ctx.status(exception.getStatus());
59-
});
50+
var routes = config.routes;
51+
routes.exception(
52+
ValidationException.class,
53+
(exception, ctx) -> {
54+
final Map<String, Object> map = new LinkedHashMap<>();
55+
map.put("message", exception.getMessage());
56+
map.put("errors", exception.getErrors());
57+
ctx.json(map);
58+
ctx.status(exception.getStatus());
59+
});
6060

61-
app.exception(
62-
InvalidTypeArgumentException.class,
63-
(exception, ctx) -> {
64-
final Map<String, String> map = new LinkedHashMap<>();
65-
map.put("path", ctx.path());
66-
map.put("message", "invalid type argument");
67-
ctx.json(map);
68-
ctx.status(400);
69-
});
61+
routes.exception(
62+
InvalidTypeArgumentException.class,
63+
(exception, ctx) -> {
64+
final Map<String, String> map = new LinkedHashMap<>();
65+
map.put("path", ctx.path());
66+
map.put("message", "invalid type argument");
67+
ctx.json(map);
68+
ctx.status(400);
69+
});
7070

71-
app.exception(
72-
InvalidPathArgumentException.class,
73-
(exception, ctx) -> {
74-
final Map<String, String> map = new LinkedHashMap<>();
75-
map.put("path", ctx.path());
76-
map.put("message", "invalid path argument");
77-
ctx.json(map);
78-
ctx.status(404);
79-
});
71+
routes.exception(
72+
InvalidPathArgumentException.class,
73+
(exception, ctx) -> {
74+
final Map<String, String> map = new LinkedHashMap<>();
75+
map.put("path", ctx.path());
76+
map.put("message", "invalid path argument");
77+
ctx.json(map);
78+
ctx.status(404);
79+
});
8080

81-
app.get(
82-
"/",
83-
ctx -> {
84-
ctx.result("Hello World");
85-
});
81+
routes.get(
82+
"/",
83+
ctx -> {
84+
ctx.result("Hello World");
85+
});
86+
});
8687

8788
app.start(port);
8889
return app;

0 commit comments

Comments
 (0)