Skip to content

Commit 34257d6

Browse files
authored
Merge pull request #543 from SentryMan/genericIssue
Fix JsonB generic generation errors
1 parent bbbbb5e commit 34257d6

File tree

5 files changed

+107
-4
lines changed

5 files changed

+107
-4
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
*/
99
public class Append {
1010

11+
private static final boolean DEBUG = Boolean.getBoolean("append.debug");
12+
1113
private final Writer writer;
14+
private final StringBuilder stringBuilder = new StringBuilder();
1215

1316
public Append(Writer writer) {
1417
this.writer = writer;
@@ -17,6 +20,9 @@ public Append(Writer writer) {
1720
public Append append(String content) {
1821
try {
1922
writer.append(content);
23+
if (DEBUG) {
24+
stringBuilder.append(content);
25+
}
2026
return this;
2127
} catch (IOException e) {
2228
throw new RuntimeException(e);
@@ -35,6 +41,9 @@ public void close() {
3541
public Append eol() {
3642
try {
3743
writer.append("\n");
44+
if (DEBUG) {
45+
stringBuilder.append("\n");
46+
}
3847
return this;
3948
} catch (IOException e) {
4049
throw new RuntimeException(e);
@@ -48,4 +57,8 @@ public Append append(String format, Object... args) {
4857
return append(String.format(format, args));
4958
}
5059

60+
@Override
61+
public String toString() {
62+
return stringBuilder.toString();
63+
}
5164
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ public static void writeJsonbType(UType type, Append writer) {
8686
static void writeType(UType type, Append writer) {
8787
if (type.isGeneric()) {
8888
final var params =
89-
type.importTypes().stream()
90-
.skip(1)
89+
type.params().stream()
9190
.map(Util::shortName)
91+
.map(s -> "?".equals(s) ? "Object" : s)
9292
.collect(Collectors.joining(".class, "));
9393

94-
writer.append("Types.newParameterizedType(%s.class, %s.class))", Util.shortName(type.mainType()), params);
94+
writer.append(
95+
"Types.newParameterizedType(%s.class, %s.class))",
96+
Util.shortName(type.mainType()), params);
9597
} else {
9698
writer.append("%s.class)", Util.shortName(type.mainType()));
9799
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.avaje.http.generator.core;
22

33
import javax.lang.model.type.TypeMirror;
4+
5+
import static java.util.stream.Collectors.toList;
6+
47
import java.util.*;
58

69
public interface UType {
@@ -69,6 +72,13 @@ default UType paramRaw() {
6972
return null;
7073
}
7174

75+
/**
76+
* Return the first generic parameter.
77+
*/
78+
default List<String> params() {
79+
return List.of();
80+
}
81+
7282
/**
7383
* Return the raw type.
7484
*/
@@ -275,7 +285,7 @@ public String shortType() {
275285

276286
@Override
277287
public String shortName() {
278-
return shortName.replace(".", "$");
288+
return shortName.replace(".", "$").replace("?", "Object");
279289
}
280290

281291
@Override
@@ -293,6 +303,11 @@ public String param1() {
293303
return allTypes.size() < 3 ? null : allTypes.get(2);
294304
}
295305

306+
@Override
307+
public List<String> params() {
308+
return allTypes.stream().skip(1).collect(toList());
309+
}
310+
296311
@Override
297312
public UType paramRaw() {
298313
return rawParamType;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.avaje.http.generator.core;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.StringWriter;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class JsonBUtilTest {
10+
11+
@Test
12+
void writeType() {
13+
UType uType = UType.parse("my.pack.Foo");
14+
var sw = new StringWriter();
15+
JsonBUtil.writeType(uType, new Append(sw));
16+
17+
assertThat(sw.toString()).isEqualTo("Foo.class)");
18+
}
19+
20+
@Test
21+
void writeType_generic() {
22+
UType uType = UType.parse("my.pack.Some<java.lang.String>");
23+
var sw = new StringWriter();
24+
JsonBUtil.writeType(uType, new Append(sw));
25+
26+
assertThat(sw.toString()).isEqualTo("Types.newParameterizedType(Some.class, String.class))");
27+
}
28+
29+
@Test
30+
void writeType_genericWithWildcard() {
31+
UType uType = UType.parse("my.pack.Some<java.lang.String, ?>");
32+
var sw = new StringWriter();
33+
JsonBUtil.writeType(uType, new Append(sw));
34+
35+
assertThat(sw.toString()).isEqualTo("Types.newParameterizedType(Some.class, String.class, Object.class))");
36+
}
37+
38+
@Test
39+
void writeType_genericWithMultiple() {
40+
UType uType = UType.parse("my.pack.Some<java.lang.String, my.other.Foo>");
41+
var sw = new StringWriter();
42+
JsonBUtil.writeType(uType, new Append(sw));
43+
44+
assertThat(sw.toString()).isEqualTo("Types.newParameterizedType(Some.class, String.class, Foo.class))");
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.example.myapp.web.test;
2+
3+
import io.avaje.http.api.Controller;
4+
import io.avaje.http.api.Get;
5+
import io.avaje.http.api.Path;
6+
import io.avaje.jsonb.Json;
7+
8+
@Path("/jsonbGeneric")
9+
@Controller
10+
public class GenericController {
11+
12+
@Json
13+
public static class Data<T> {}
14+
15+
@Json
16+
public static class Data2<T, T2> {}
17+
18+
@Get("single")
19+
Data<String> getData() {
20+
return null;
21+
}
22+
23+
@Get("double")
24+
Data2<String, ?> getData2() {
25+
return null;
26+
}
27+
}

0 commit comments

Comments
 (0)