Skip to content

Commit 5f30de5

Browse files
authored
Merge pull request #258 from SentryMan/client-vararg
Sanitize Generated Imports
2 parents 256bf8b + b15b4f3 commit 5f30de5

File tree

7 files changed

+49
-31
lines changed

7 files changed

+49
-31
lines changed

README.md

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Use source code generation to adapt annotated REST controllers `@Path, @Get, @Po
4545
</dependency>
4646
```
4747

48-
## Define a Controller (These APT processors work with both Java and Kotlin.)
48+
## Define a Controller (These APT processors work with Java and Kotlin.)
4949
```java
5050
package org.example.hello;
5151

@@ -74,7 +74,7 @@ public class WidgetController {
7474
}
7575
```
7676
## DI Usage
77-
The annotation processor will generate controller adapters that can register routes to Javalin/Helidon. The natural way to use the generated adapters is to get a DI library to find and wire them. This is what the below examples do and they use [Avaje-Inject](https://avaje.io/inject/) to do this. The AP will automatically detect the presence of avaje-inject and generate the class to use avaje-inject's `@Component` as the DI annotation.
77+
The annotation processor will generate controller adapters to register routes to Javalin/Helidon. The natural way to use the generated adapters is to get a DI library to find and wire them. This is what the examples below do; they use [Avaje-Inject](https://avaje.io/inject/) to do this. The AP will automatically detect the presence of avaje-inject and generate the class to use avaje-inject's `@Component` as the DI annotation.
7878

7979
There isn't a hard requirement to use Avaje for dependency injection. In the absence of avaje-inject, the generated class will use `@jakarta.inject.Singleton` or `@javax.inject.Singleton` depending on what's on the classpath. Any DI library that can find and wire the generated @Singleton beans can be used. You can even use Dagger2 or Guice to wire the controllers if you so desire.
8080

@@ -93,15 +93,13 @@ To force the AP to generate with `@javax.inject.Singleton`(in the case where you
9393

9494
### Usage with Javalin
9595

96-
The annotation processor will generate controller classes implementing the WebRoutes interface, which means we can
96+
The annotation processor will generate controller classes implementing the Javalin `Plugin` interface, which means we can
9797
get all the WebRoutes and register them with Javalin using:
9898

9999
```java
100-
List<WebRoutes> routes = BeanScope.builder().build().list(WebRoutes.class);
100+
List<Plugin> routes = BeanScope.builder().build().list(Plugin.class);
101101

102-
Javalin.create()
103-
.routes(() -> routes.forEach(WebRoutes::registerRoutes))
104-
.start();
102+
Javalin.create(cfg -> routes.forEach(cfg.plugins::register));
105103
```
106104

107105
### Usage with Helidon Nima
@@ -110,12 +108,10 @@ The annotation processor will generate controller classes implementing the Helid
110108
get all the services and register them with the Helidon `HttpRouting`.
111109

112110
```java
113-
List<HttpService> routes = BeanScope.builder().build().list(HttpService.class);
111+
List<HttpFeature> routes = BeanScope.builder().build().list(HttpFeature.class);
114112
final var builder = HttpRouting.builder();
115113

116-
for (final HttpService httpService : routes) {
117-
httpService.routing(builder);
118-
}
114+
routes.forEach(builder::register);
119115

120116
WebServer.builder()
121117
.addRouting(builder.build())
@@ -129,7 +125,7 @@ WebServer.builder()
129125
```java
130126
@Generated("avaje-javalin-generator")
131127
@Singleton
132-
public class WidgetController$Route implements WebRoutes {
128+
public class WidgetController$Route implements Plugin {
133129

134130
private final WidgetController controller;
135131

@@ -138,16 +134,16 @@ public class WidgetController$Route implements WebRoutes {
138134
}
139135

140136
@Override
141-
public void registerRoutes() {
137+
public void apply(Javalin app) {
142138

143-
ApiBuilder.get("/widgets/{id}", ctx -> {
139+
app.get("/widgets/{id}", ctx -> {
144140
ctx.status(200);
145141
var id = asInt(ctx.pathParam("id"));
146142
var result = controller.getById(id);
147143
ctx.json(result);
148144
});
149145

150-
ApiBuilder.get("/widgets", ctx -> {
146+
app.get("/widgets", ctx -> {
151147
ctx.status(200);
152148
var result = controller.getAll();
153149
ctx.json(result);
@@ -162,17 +158,17 @@ public class WidgetController$Route implements WebRoutes {
162158
```java
163159
@Generated("avaje-helidon-nima-generator")
164160
@Singleton
165-
public class WidgetController$Route implements HttpService {
161+
public class WidgetController$Route implements HttpFeature {
166162

167163
private final WidgetController controller;
168164
public WidgetController$Route(WidgetController controller) {
169165
this.controller = controller;
170166
}
171167

172168
@Override
173-
public void routing(HttpRules rules) throws Exception {
174-
rules.get("/widgets/{id}", this::_getById);
175-
rules.get("/widgets", this::_getAll);
169+
public void setup(HttpRouting.Builder routing) {
170+
routing.get("/widgets/{id}", this::_getById);
171+
routing.get("/widgets", this::_getAll);
176172
}
177173

178174
private void _getById(ServerRequest req, ServerResponse res) throws Exception {
@@ -197,7 +193,7 @@ If [Avaje-Jsonb](https://github.com/avaje/avaje-jsonb) is detected, http generat
197193
```java
198194
@Generated("avaje-javalin-generator")
199195
@Component
200-
public class WidgetController$Route implements WebRoutes {
196+
public class WidgetController$Route implements Plugin {
201197

202198
private final WidgetController controller;
203199
private final JsonType<List<Widget>> listWidgetJsonType;
@@ -210,16 +206,16 @@ public class WidgetController$Route implements WebRoutes {
210206
}
211207

212208
@Override
213-
public void registerRoutes() {
209+
public void apply(Javalin app) {
214210

215-
ApiBuilder.get("/widgets/{id}", ctx -> {
211+
app.get("/widgets/{id}", ctx -> {
216212
ctx.status(200);
217213
var id = asInt(ctx.pathParam("id"));
218214
var result = controller.getById(id);
219215
widgetJsonType.toJson(result, ctx.contentType("application/json").outputStream());
220216
});
221217

222-
ApiBuilder.get("/widgets", ctx -> {
218+
app.get("/widgets", ctx -> {
223219
ctx.status(200);
224220
var result = controller.getAll();
225221
listWidgetJsonType.toJson(result, ctx.contentType("application/json").outputStream());
@@ -234,8 +230,7 @@ public class WidgetController$Route implements WebRoutes {
234230
```java
235231
@Generated("avaje-helidon-nima-generator")
236232
@Component
237-
public class WidgetController$Route implements HttpService {
238-
233+
public class WidgetController$Route implements HttpFeature {
239234

240235
private final WidgetController controller;
241236
private final JsonType<Widget> widgetJsonType;
@@ -248,9 +243,9 @@ public class WidgetController$Route implements HttpService {
248243
}
249244

250245
@Override
251-
public void routing(HttpRules rules) {
252-
rules.get("/widgets/{id}", this::_getById);
253-
rules.get("/widgets", this::_getAll);
246+
public void setup(HttpRouting.Builder routing) {
247+
routing.get("/widgets/{id}", this::_getById);
248+
routing.get("/widgets", this::_getAll);
254249
}
255250

256251
private void _getById(ServerRequest req, ServerResponse res) throws Exception {

http-generator-client/src/test/java/io/avaje/http/generator/client/ClientProcessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import javax.tools.JavaCompiler.CompilationTask;
1919
import javax.tools.JavaFileObject;
2020
import javax.tools.JavaFileObject.Kind;
21-
import javax.tools.StandardJavaFileManager;
2221
import javax.tools.StandardLocation;
2322
import javax.tools.ToolProvider;
2423

@@ -38,6 +37,7 @@ void deleteGeneratedFiles() throws IOException {
3837
.sorted(Comparator.reverseOrder())
3938
.map(Path::toFile)
4039
.forEach(File::delete);
40+
4141
} catch (final Exception e) {
4242
}
4343
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package io.avaje.http.generator.client.clients;
2+
3+
public class Body {}

http-generator-client/src/test/java/io/avaje/http/generator/client/clients/UserClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public interface UserClient {
1111
@Post("/users")
1212
String createUser(@BodyString String body);
1313

14+
@Post("/body")
15+
String bodies(Body... bodies);
16+
1417
@Get("/users/{userId}")
1518
String getUserById(String userId);
1619
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public String path() {
328328

329329
public void addImportType(String rawType) {
330330
if (rawType.indexOf('.') > 0) {
331-
importTypes.add(rawType);
331+
importTypes.add(sanitizeImports(rawType));
332332
}
333333
}
334334

@@ -353,4 +353,17 @@ public Set<String> importTypes() {
353353
public boolean hasInstrument() {
354354
return hasInstrument;
355355
}
356+
357+
public static String sanitizeImports(String type) {
358+
final int pos = type.indexOf("@");
359+
if (pos == -1) {
360+
return trimArrayBrackets(type);
361+
}
362+
final var start = pos == 0 ? type.substring(0, pos) : "";
363+
return start + trimArrayBrackets(type.substring(type.lastIndexOf(' ') + 1));
364+
}
365+
366+
private static String trimArrayBrackets(String type) {
367+
return type.replaceAll("[^\\n\\r\\t $;\\w.]", "");
368+
}
356369
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static io.avaje.http.generator.core.ParamType.RESPONSE_HANDLER;
44
import static io.avaje.http.generator.core.ProcessingContext.platform;
55
import static io.avaje.http.generator.core.ProcessingContext.typeElement;
6-
import static java.util.function.Predicate.not;
76

87
import java.util.ArrayList;
98
import java.util.HashSet;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,9 @@ public UType utype() {
8383
public void setResponseHandler() {
8484
elementParam.setResponseHandler();
8585
}
86+
87+
@Override
88+
public String toString() {
89+
return elementParam.toString();
90+
}
8691
}

0 commit comments

Comments
 (0)