Skip to content

Commit 5263d0d

Browse files
committed
0.1.8b5 - stackoverflow in OpenApiUtil.classToSchema / OpenApiUtil.typeMirrorToSchema
1 parent 948db88 commit 5263d0d

File tree

12 files changed

+98
-32
lines changed

12 files changed

+98
-32
lines changed

README.MD

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Make sure you include the <a href="https://jitpack.io/">repository</a> as well.
2222
<dependency>
2323
<groupId>com.github.qlsolutions.JavalinFly</groupId>
2424
<artifactId>javalinfly-core</artifactId>
25-
<version>0.1.8b4</version>
25+
<version>0.1.8b5</version>
2626
</dependency>
2727
```
2828

@@ -37,7 +37,7 @@ Make sure you include the <a href="https://jitpack.io/">repository</a> as well.
3737
<annotationProcessorPath>
3838
<groupId>com.github.qlsolutions.JavalinFly</groupId>
3939
<artifactId>javalinfly-core</artifactId>
40-
<version>0.1.8b4</version>
40+
<version>0.1.8b5</version>
4141
</annotationProcessorPath>
4242
</annotationProcessorPaths>
4343
</configuration>
@@ -51,7 +51,7 @@ Make sure you include the <a href="https://jitpack.io/">repository</a> as well.
5151
<dependency>
5252
<groupId>com.github.qlsolutions.JavalinFly</groupId>
5353
<artifactId>javalinfly-kotlin</artifactId>
54-
<version>0.1.8b4</version>
54+
<version>0.1.8b5</version>
5555
</dependency>
5656
```
5757

@@ -69,20 +69,20 @@ Make sure you include the <a href="https://jitpack.io/">repository</a> as well.
6969
- Dependency
7070

7171
```groovy
72-
implementation 'com.github.qlsolutions.JavalinFly:javalinfly-core:0.1.8b4'
72+
implementation 'com.github.qlsolutions.JavalinFly:javalinfly-core:0.1.8b5'
7373
```
7474

7575
- Annotation processor
7676

7777
```groovy
78-
annotationProcessor 'com.github.qlsolutions.JavalinFly:javalinfly-core:0.1.8b4'
78+
annotationProcessor 'com.github.qlsolutions.JavalinFly:javalinfly-core:0.1.8b5'
7979
```
8080

8181
- <details>
8282
<summary>Optionally also the kotlin module</summary>
8383

8484
```groovy
85-
implementation 'com.github.qlsolutions.JavalinFly:javalinfly-kotlin:0.1.8b4'
85+
implementation 'com.github.qlsolutions.JavalinFly:javalinfly-kotlin:0.1.8b5'
8686
```
8787

8888
</details>
@@ -93,19 +93,19 @@ Make sure you include the <a href="https://jitpack.io/">repository</a> as well.
9393

9494
- Dependency
9595
```groovy
96-
implementation("com.github.qlsolutions.JavalinFly:javalinfly-core:0.1.8b4")
96+
implementation("com.github.qlsolutions.JavalinFly:javalinfly-core:0.1.8b5")
9797
```
9898

9999
- Annotation processor
100100
```groovy
101-
annotationProcessor("com.github.qlsolutions.JavalinFly:javalinfly-core:0.1.8b4")
101+
annotationProcessor("com.github.qlsolutions.JavalinFly:javalinfly-core:0.1.8b5")
102102
```
103103

104104
- <details>
105105
<summary>Optionally also the kotlin module</summary>
106106

107107
```groovy
108-
implementation("com.github.qlsolutions.JavalinFly:javalinfly-kotlin:0.1.8b4")
108+
implementation("com.github.qlsolutions.JavalinFly:javalinfly-kotlin:0.1.8b5")
109109
```
110110

111111
</details>

javalinfly-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.unldenis</groupId>
88
<artifactId>JavalinFly</artifactId>
9-
<version>0.1.8b4</version>
9+
<version>0.1.8b5</version>
1010
</parent>
1111

1212
<artifactId>javalinfly-core</artifactId>

javalinfly-core/src/main/java/com/quicklink/javalinfly/Vars.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
package com.quicklink.javalinfly;
22

3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.io.UncheckedIOException;
8+
import java.net.URISyntaxException;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
12+
import java.util.Objects;
13+
314
public class Vars {
415

16+
public static final String RESOURCE_FILE_SPEC = "openapi-spec.json";
517

6-
private static String OPEN_API_SPEC = null;
718

819
public static String openApiSpec() {
9-
return OPEN_API_SPEC;
10-
}
20+
try {
21+
var path = Paths.get(
22+
Objects.requireNonNull(Vars.class.getClassLoader().getResource(RESOURCE_FILE_SPEC)).toURI());
23+
24+
var content = Files.readString(path);
1125

12-
public static void openApiSpec(String openApiSpec) {
13-
OPEN_API_SPEC = openApiSpec;
26+
return content;
27+
28+
} catch (IOException e) {
29+
throw new UncheckedIOException(e);
30+
} catch (URISyntaxException e) {
31+
throw new RuntimeException(e);
32+
}
1433
}
1534

1635

36+
1737
private static String SWAGGER_UI = null;
1838

1939
public static String swaggerUi() {

javalinfly-core/src/main/java/com/quicklink/javalinfly/openapi/OpenApiTranslator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void decodeSchemas(String schemasEncoded) {
5252
TypeReference<LinkedHashMap<String, Schema>> typeRef = new TypeReference<>() {
5353
};
5454

55-
schemas = MAPPER.readValue(new String(Base64.getDecoder().decode(schemasEncoded)), typeRef);
55+
schemas = MAPPER.readValue(schemasEncoded, typeRef);
5656
} catch (JsonProcessingException e) {
5757
throw new UncheckedIOException(e);
5858
}

javalinfly-core/src/main/java/com/quicklink/javalinfly/openapi/OpenApiUtil.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ public OpenApiUtil(Types typeUtils, Elements elementUtils, MessagerRound message
3838
public Schema classToSchema(Map<String, Schema> schemas, TypeMirror typeMirror, String path, boolean request, boolean createSchema) {
3939
messager.warning("Analyzing schema '%s' of path '%s'", typeMirror.toString(), path);
4040

41+
//
42+
// if(ProcessorUtil.getClassNameWithoutAnnotations(typeMirror).startsWith("java.")) {
43+
// return Schema.builder().type("string").example("Class: " + typeMirror.toString()).build();
44+
// }
4145

42-
if(ProcessorUtil.getClassNameWithoutAnnotations(typeMirror).startsWith("java.")) {
43-
return Schema.builder().type("string").example("Class: " + typeMirror.toString()).build();
44-
}
4546

4647

4748
ProcessorUtil.asTypeElement(this.typeUtils, typeMirror);
@@ -64,7 +65,11 @@ public Schema classToSchema(Map<String, Schema> schemas, TypeMirror typeMirror,
6465

6566
schema.description = "A JSON object containing a generic class information";
6667
TypeMirror keyType;
67-
if (this.isCollection(classElement)) {
68+
if(this.isObject(classElement)) {
69+
schema.type = "object";
70+
schema.description = "Unknown type";
71+
return schema;
72+
} else if (this.isCollection(classElement)) {
6873
schema.type = "array";
6974
keyType = this.getGenericType(declaredType, 0);
7075
if (keyType != null) {
@@ -192,6 +197,11 @@ private boolean isMap(TypeElement classElement) {
192197
elementUtils.getTypeElement("java.util.Map").asType());
193198
}
194199

200+
private boolean isObject(TypeElement classElement) {
201+
return typeUtils.isSameType(classElement.asType(),
202+
elementUtils.getTypeElement("java.lang.Object").asType());
203+
}
204+
195205
private boolean isEnum(TypeElement classElement) {
196206
return classElement.getKind() == ElementKind.ENUM;
197207
}

javalinfly-core/src/main/java/com/quicklink/javalinfly/processor/round/ControllersRound.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonInclude.Include;
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.SerializationFeature;
67
import com.quicklink.javalinfly.annotation.Body;
78
import com.quicklink.javalinfly.annotation.Controller;
89
import com.quicklink.javalinfly.annotation.Delete;
@@ -63,6 +64,8 @@ public class ControllersRound extends Round {
6364
public final List<String> openApiStatements = new ArrayList<>();
6465

6566

67+
public String schemasEncoded;
68+
6669
public ControllersRound(Types typeUtils, Elements elementUtils, MessagerRound messager,
6770
RoundEnvironment roundEnv,
6871
TypeMirror rolesTypeMirror,
@@ -460,10 +463,12 @@ else if(returnTypeElement.getQualifiedName().toString().equals(SuccessResponse.c
460463
if(injector.generateDocumentation()) {
461464
var MAPPER = new ObjectMapper();
462465
MAPPER.setSerializationInclusion(Include.NON_NULL);
466+
MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
467+
463468
try {
464-
String schemasEncoded = Base64.getEncoder()
465-
.encodeToString(MAPPER.writeValueAsString(schemaMap).getBytes());
466-
openApiStatements.add(0, " openApiTranslator.decodeSchemas(\"" + schemasEncoded + "\");\n");
469+
schemasEncoded = MAPPER.writeValueAsString(schemaMap);
470+
// openApiStatements.add(0, " openApiTranslator.decodeSchemas(\"" + schemasEncoded + "\");\n");
471+
openApiStatements.add(0, " openApiTranslator.decodeSchemas(Vars.openApiSpec());\n");
467472
} catch (JsonProcessingException e) {
468473
throw new RuntimeException(e);
469474
}

javalinfly-core/src/main/java/com/quicklink/javalinfly/processor/round/GeneratorRound.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.util.stream.Collectors;
2020
import javax.annotation.processing.Filer;
2121
import javax.lang.model.element.Element;
22+
import javax.tools.FileObject;
2223
import javax.tools.JavaFileObject;
24+
import javax.tools.StandardLocation;
2325

2426
public class GeneratorRound extends Round {
2527

@@ -78,15 +80,29 @@ private void generateClass(Element element) {
7880
" OpenApi openApi = openApiTranslator.build();\n" +
7981
" config.openapi.edit(openApi);\n" +
8082
" String openApiSpec = openApiTranslator.asString(openApi);\n" +
81-
" Vars.openApiSpec(openApiSpec);\n" +
82-
" Vars.swaggerUi(SwaggerUIHtmlGenerator.generateSwaggerUIHtml(Vars.openApiSpec()));\n"
83+
// " Vars.openApiSpec(openApiSpec);\n" +
84+
" Vars.swaggerUi(SwaggerUIHtmlGenerator.generateSwaggerUIHtml(openApiSpec));\n"
8385
+
8486
" }\n" +
8587
" {\n" +
8688
" javalin.addHandler(HandlerType.GET, \"/openapi\", ctx -> {\n" +
8789
" ctx.html(Vars.swaggerUi());\n" +
8890
" }, new RouteRole[]{%s});\n".formatted(allRoles) +
8991
" }\n";
92+
93+
94+
95+
96+
try {
97+
FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", Vars.RESOURCE_FILE_SPEC);
98+
try (Writer writer = file.openWriter()) {
99+
writer.write(controllersRound.schemasEncoded);
100+
}
101+
} catch (IOException e) {
102+
messager.error(element, "Error generating resource %s: %s", Vars.RESOURCE_FILE_SPEC , e.getMessage());
103+
104+
}
105+
90106
}
91107

92108
String source = "package " + PACKAGE_NAME + ";\n\n" +
@@ -122,6 +138,8 @@ private void generateClass(Element element) {
122138

123139
Javalin app;
124140

141+
142+
125143
try {
126144
JavaFileObject sourceFile = filer.createSourceFile(FULL_CLASS, element);
127145
try (Writer writer = sourceFile.openWriter()) {
@@ -131,6 +149,7 @@ private void generateClass(Element element) {
131149
} catch (IOException e) {
132150
messager.error(element, "Error generating class %s: %s", FULL_CLASS, e.getMessage());
133151
}
152+
134153
}
135154

136155

javalinfly-example-kotlin/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.unldenis</groupId>
88
<artifactId>JavalinFly</artifactId>
9-
<version>0.1.8b4</version>
9+
<version>0.1.8b5</version>
1010
</parent>
1111

1212
<artifactId>javalinfly-example-kotlin</artifactId>

javalinfly-example-kotlin/src/main/kotlin/Main.kt

+15-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ enum class MyRoles : RouteRole {
2020
generateDocumentation = true
2121
)
2222
fun main() {
23-
val app = Javalin.create()
24-
.get("/") { ctx -> ctx.result("Hello World") }
25-
.start(7070)
23+
val app = Javalin.create { c ->
24+
25+
c.accessManager { handler, ctx, routeRoles ->
26+
val role: MyRoles = MyRoles.GUEST;
27+
if (routeRoles.contains(role)) {
28+
handler.handle(ctx)
29+
} else {
30+
ctx.status(401).result("Unauthorized")
31+
}
32+
}
33+
}
2634

35+
app.get("/") { ctx -> ctx.result("Hello World") }
36+
app.start(7070)
2737

2838
JavalinFly.inject(app) {
2939

@@ -41,5 +51,7 @@ fun main() {
4151
)
4252
)
4353
}
54+
55+
4456
}
4557
} // https://stackoverflow.com/questions/38926255/maven-annotation-processing-processor-not-found

javalinfly-example/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.unldenis</groupId>
88
<artifactId>JavalinFly</artifactId>
9-
<version>0.1.8b4</version>
9+
<version>0.1.8b5</version>
1010
</parent>
1111

1212
<artifactId>javalinfly-example</artifactId>

javalinfly-kotlin/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.unldenis</groupId>
88
<artifactId>JavalinFly</artifactId>
9-
<version>0.1.8b4</version>
9+
<version>0.1.8b5</version>
1010
</parent>
1111

1212
<artifactId>javalinfly-kotlin</artifactId>

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.unldenis</groupId>
88
<artifactId>JavalinFly</artifactId>
9-
<version>0.1.8b4</version>
9+
<version>0.1.8b5</version>
1010
<packaging>pom</packaging>
1111

1212
<modules>

0 commit comments

Comments
 (0)