Skip to content

Commit a18e301

Browse files
authored
refactor: Extract util for finding all classes used in openapi.json (#1172)
Removes the Aot processor that finds endpoint classes as they are also detected from openapi.json
1 parent 7622f2c commit a18e301

File tree

9 files changed

+72
-39
lines changed

9 files changed

+72
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dev.hilla;
2+
3+
import java.io.IOException;
4+
import java.util.Collections;
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.node.ArrayNode;
11+
import com.fasterxml.jackson.databind.node.ObjectNode;
12+
13+
/**
14+
* Utilities for interacting with the generated openapi.json.
15+
*/
16+
public class OpenAPIUtil {
17+
18+
/**
19+
* Parses the given open api and finds all used classes (endpoints,
20+
* parameter and return types).
21+
*
22+
* @param openApiAsText
23+
* the open api JSON as text
24+
* @return a set of classes used
25+
* @throws IOException
26+
* if parsing fails
27+
*/
28+
public static Set<String> findOpenApiClasses(String openApiAsText)
29+
throws IOException {
30+
JsonNode openApi = new ObjectMapper().readTree(openApiAsText);
31+
if (!openApi.has("components")) {
32+
return Collections.emptySet();
33+
}
34+
35+
Set<String> types = new HashSet<>();
36+
37+
// Endpoints
38+
ArrayNode tags = (ArrayNode) openApi.get("tags");
39+
40+
if (tags != null) {
41+
tags.forEach(nameAndClass -> {
42+
types.add(nameAndClass.get("x-class-name").asText());
43+
});
44+
}
45+
46+
// Parameters and return types
47+
ObjectNode schemas = (ObjectNode) openApi.get("components")
48+
.get("schemas");
49+
if (schemas != null) {
50+
schemas.fieldNames().forEachRemaining(type -> {
51+
types.add(type);
52+
});
53+
}
54+
55+
return types;
56+
57+
}
58+
}

packages/java/endpoint/src/main/java/dev/hilla/springnative/HillaHintsRegistrar.java

+3-32
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import com.fasterxml.jackson.databind.JsonNode;
2323
import com.fasterxml.jackson.databind.ObjectMapper;
2424
import com.fasterxml.jackson.databind.node.ObjectNode;
25-
25+
import dev.hilla.EndpointCodeGenerator;
26+
import dev.hilla.OpenAPIUtil;
2627
import dev.hilla.engine.EngineConfiguration;
2728
import dev.hilla.push.PushEndpoint;
2829
import dev.hilla.push.messages.fromclient.AbstractServerMessage;
@@ -65,7 +66,7 @@ private void registerEndpointTypes(RuntimeHints hints) {
6566
new InputStreamReader(resource.openStream()));
6667
String openApiAsText = reader.lines()
6768
.collect(Collectors.joining("\n"));
68-
Set<String> types = parseOpenApi(openApiAsText);
69+
Set<String> types = OpenAPIUtil.findOpenApiClasses(openApiAsText);
6970
for (String type : types) {
7071
hints.reflection().registerType(TypeReference.of(type),
7172
MemberCategory.values());
@@ -77,36 +78,6 @@ private void registerEndpointTypes(RuntimeHints hints) {
7778
hints.resources().registerPattern(EngineConfiguration.OPEN_API_PATH);
7879
}
7980

80-
/**
81-
* Parses the given open api and finds the used custom types.
82-
*
83-
* @param openApiAsText
84-
* the open api JSON as text
85-
* @return a set of custom types used
86-
* @throws IOException
87-
* if parsing fails
88-
*/
89-
public static Set<String> parseOpenApi(String openApiAsText)
90-
throws IOException {
91-
JsonNode openApi = new ObjectMapper().readTree(openApiAsText);
92-
if (!openApi.has("components")) {
93-
return Collections.emptySet();
94-
}
95-
ObjectNode schemas = (ObjectNode) openApi.get("components")
96-
.get("schemas");
97-
98-
Set<String> types = new HashSet<>();
99-
if (schemas != null) {
100-
101-
schemas.fieldNames().forEachRemaining(type -> {
102-
types.add(type);
103-
});
104-
}
105-
106-
return types;
107-
108-
}
109-
11081
private Collection<Class<?>> getMessageTypes(Class<?> cls) {
11182
List<Class<?>> classes = new ArrayList<>();
11283
classes.add(cls);
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=dev.hilla.EndpointInitializationAotProcessor
21
org.springframework.aot.hint.RuntimeHintsRegistrar=dev.hilla.springnative.HillaHintsRegistrar

packages/java/endpoint/src/test/java/dev/hilla/springnative/HillaHintsRegistrarTest.java packages/java/endpoint/src/test/java/dev/hilla/OpenAPIUtilTest.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.hilla.springnative;
1+
package dev.hilla;
22

33
import java.io.IOException;
44
import java.nio.charset.StandardCharsets;
@@ -9,7 +9,7 @@
99
import org.junit.Assert;
1010
import org.junit.Test;
1111

12-
public class HillaHintsRegistrarTest {
12+
public class OpenAPIUtilTest {
1313

1414
@Test
1515
public void emptySchemaReturnsNoComponents() throws IOException {
@@ -19,14 +19,16 @@ public void emptySchemaReturnsNoComponents() throws IOException {
1919

2020
@Test
2121
public void singleType() throws IOException {
22-
Assert.assertEquals(Set
23-
.of("com.example.application.endpoints.helloreact.MyOtherType"),
22+
Assert.assertEquals(Set.of(
23+
"com.example.application.endpoints.helloreact.HelloReactEndpoint",
24+
"com.example.application.endpoints.helloreact.MyOtherType"),
2425
parse("openapi-customtype.json"));
2526
}
2627

2728
@Test
2829
public void referringTypes() throws IOException {
2930
Assert.assertEquals(Set.of(
31+
"com.example.application.endpoints.helloreact.HelloReactEndpoint",
3032
"com.example.application.endpoints.helloreact.MyType",
3133
"com.example.application.endpoints.helloreact.MyOtherType"),
3234
parse("openapi-referring-customtypes.json"));
@@ -35,6 +37,7 @@ public void referringTypes() throws IOException {
3537
@Test
3638
public void nestedType() throws IOException {
3739
Assert.assertEquals(Set.of(
40+
"com.example.application.endpoints.helloreact.HelloReactEndpoint",
3841
"com.example.application.endpoints.helloreact.HelloReactEndpoint$MyInnerType"),
3942
parse("openapi-innertype.json"));
4043
}
@@ -43,7 +46,7 @@ private Set<String> parse(String openapiFilename) throws IOException {
4346
String openApi = IOUtils.toString(
4447
getClass().getResourceAsStream(openapiFilename),
4548
StandardCharsets.UTF_8);
46-
return HillaHintsRegistrar.parseOpenApi(openApi);
49+
return OpenAPIUtil.findOpenApiClasses(openApi);
4750
}
4851

4952
}

packages/java/engine-core/src/main/java/dev/hilla/engine/EngineConfiguration.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.stream.Collectors;
1111

1212
import com.fasterxml.jackson.annotation.JsonAutoDetect;
13+
import com.fasterxml.jackson.annotation.JsonIgnore;
1314
import com.fasterxml.jackson.annotation.PropertyAccessor;
1415
import com.fasterxml.jackson.databind.ObjectMapper;
1516
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@@ -135,7 +136,8 @@ public void store(File file) throws IOException {
135136
MAPPER.writeValue(file, this);
136137
}
137138

138-
Path getOpenAPIFile() {
139+
@JsonIgnore
140+
public Path getOpenAPIFile() {
139141
return classesDir.resolve(OPEN_API_PATH);
140142
}
141143

0 commit comments

Comments
 (0)