|
1 | 1 | package io.avaje.http.generator.jex; |
2 | 2 |
|
3 | 3 | import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
4 | 5 |
|
5 | 6 | import io.avaje.http.generator.core.Append; |
6 | 7 | import io.avaje.http.generator.core.Constants; |
|
9 | 10 | import io.avaje.http.generator.core.PlatformAdapter; |
10 | 11 | import io.avaje.http.generator.core.ProcessingContext; |
11 | 12 | import io.avaje.http.generator.core.UType; |
| 13 | +import io.avaje.http.generator.core.Util; |
12 | 14 |
|
13 | 15 | class JexAdapter implements PlatformAdapter { |
14 | 16 |
|
@@ -38,12 +40,54 @@ public String bodyAsClass(UType type) { |
38 | 40 | return "ctx.body()"; |
39 | 41 | } else if ("byte[]".equals(type.full())) { |
40 | 42 | return "ctx.bodyAsBytes()"; |
| 43 | + } else if (type.isGeneric() && ProcessingContext.useJsonb()) { |
| 44 | + return "ctx.<%s>bodyAsType(%s)".formatted(type.shortType(), writeJsonbType(type)); |
41 | 45 | } else if (ProcessingContext.useJsonb()) { |
42 | | - return type.shortName() + "JsonType.fromJson(ctx.bodyAsInputStream())"; |
| 46 | + return "ctx.bodyAsClass(%s.class)".formatted(type.shortType()); |
43 | 47 | } |
44 | 48 | return "ctx.bodyAsClass(" + type.mainType() + ".class)"; |
45 | 49 | } |
46 | 50 |
|
| 51 | + public static String writeJsonbType(UType type) { |
| 52 | + var writer = new StringBuilder(); |
| 53 | + |
| 54 | + switch (type.mainType()) { |
| 55 | + case "java.util.List": |
| 56 | + writeType(type.paramRaw(), writer); |
| 57 | + writer.append(".list()"); |
| 58 | + break; |
| 59 | + case "java.util.Set": |
| 60 | + writeType(type.paramRaw(), writer); |
| 61 | + writer.append(".set()"); |
| 62 | + break; |
| 63 | + case "java.util.Map": |
| 64 | + writeType(type.paramRaw(), writer); |
| 65 | + writer.append(".map()"); |
| 66 | + break; |
| 67 | + default: |
| 68 | + { |
| 69 | + if (type.mainType().contains("java.util")) { |
| 70 | + throw new UnsupportedOperationException( |
| 71 | + "Only java.util Map, Set and List are supported JsonB Controller Collection Types"); |
| 72 | + } |
| 73 | + writeType(type, writer); |
| 74 | + } |
| 75 | + } |
| 76 | + return writer.toString(); |
| 77 | + } |
| 78 | + |
| 79 | + static void writeType(UType type, StringBuilder writer) { |
| 80 | + final var params = |
| 81 | + type.params().stream() |
| 82 | + .map(Util::shortName) |
| 83 | + .map(s -> "?".equals(s) ? "Object" : s) |
| 84 | + .collect(Collectors.joining(".class, ")); |
| 85 | + |
| 86 | + writer.append( |
| 87 | + "Types.newParameterizedType(%s.class, %s.class)" |
| 88 | + .formatted(Util.shortName(type.mainType()), params)); |
| 89 | + } |
| 90 | + |
47 | 91 | @Override |
48 | 92 | public String indent() { |
49 | 93 | return " "; |
|
0 commit comments