Skip to content

Commit 82286bd

Browse files
committed
support generic jsonb types
1 parent b9b49b2 commit 82286bd

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.avaje.http.generator.core;
22

3+
import java.util.Collection;
34
import java.util.LinkedHashMap;
45
import java.util.Map;
56
import java.util.function.Consumer;
7+
import java.util.stream.Collectors;
68

79
public class JsonBUtil {
810
private JsonBUtil() {}
@@ -53,8 +55,20 @@ public static void writeJsonbType(UType type, Append writer) {
5355
writer.append("%s.class).map()", type.param1());
5456
break;
5557
default:
56-
throw new UnsupportedOperationException(
57-
"Only java.util Map, Set and List are supported JsonB Controller Collection Types");
58+
{
59+
try {
60+
if (Collection.class.isAssignableFrom(Class.forName(type.mainType())))
61+
throw new UnsupportedOperationException(
62+
"Only java.util Map, Set and List are supported JsonB Controller Collection Types");
63+
} catch (final ClassNotFoundException e) {
64+
throw new UnsupportedOperationException(
65+
"Only java.util Map, Set and List are supported JsonB Controller Collection Types");
66+
}
67+
final var params =
68+
type.allTypes().stream().skip(1).collect(Collectors.joining(".class, ")) + ".class";
69+
70+
writer.append("Types.newParameterizedType(%s.class, %s))", type.mainType(), params);
71+
}
5872
}
5973
}
6074
writer.append(";").eol();

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ static UType parse(TypeMirror type) {
4040
default String param0() {
4141
return null;
4242
}
43+
/**
44+
* Return all types associated with this Utype.
45+
*/
46+
default List<String> allTypes() {
47+
return null;
48+
}
4349

4450
/**
4551
* Return the second generic parameter.
@@ -123,6 +129,11 @@ public String shortName() {
123129
public String mainType() {
124130
return rawType;
125131
}
132+
133+
@Override
134+
public List<String> allTypes() {
135+
return List.of(rawType);
136+
}
126137
}
127138

128139
/**
@@ -201,6 +212,11 @@ public String mainType() {
201212
return allTypes.isEmpty() ? null : allTypes.get(0);
202213
}
203214

215+
@Override
216+
public List<String> allTypes() {
217+
return allTypes;
218+
}
219+
204220
@Override
205221
public String param0() {
206222
return allTypes.size() < 2 ? null : allTypes.get(1);

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package io.avaje.http.generator.javalin;
22

3-
import io.avaje.http.generator.core.*;
4-
53
import java.io.IOException;
64
import java.util.Map;
75

6+
import io.avaje.http.generator.core.BaseControllerWriter;
7+
import io.avaje.http.generator.core.Constants;
8+
import io.avaje.http.generator.core.ControllerReader;
9+
import io.avaje.http.generator.core.JsonBUtil;
10+
import io.avaje.http.generator.core.MethodReader;
11+
import io.avaje.http.generator.core.PrimitiveUtil;
12+
import io.avaje.http.generator.core.ProcessingContext;
13+
import io.avaje.http.generator.core.UType;
14+
815
/**
916
* Write Javalin specific Controller WebRoute handling adapter.
1017
*/
@@ -21,6 +28,7 @@ class ControllerWriter extends BaseControllerWriter {
2128
if (useJsonB) {
2229
reader.addImportType("io.avaje.jsonb.Jsonb");
2330
reader.addImportType("io.avaje.jsonb.JsonType");
31+
reader.addImportType("io.avaje.jsonb.Types");
2432
this.jsonTypes = JsonBUtil.jsonTypes(reader);
2533
} else {
2634
this.jsonTypes = Map.of();

0 commit comments

Comments
 (0)