Skip to content

Commit 877af49

Browse files
committed
fixes merging allOf when allOf is mixed with normal properties
fixes ZenWave360/zenwave-sdk#37
1 parent 0bde6bd commit 877af49

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

src/main/java/io/zenwave360/jsonrefparser/$RefParser.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,12 @@ private void mergeAllOf(Object value, String[] paths, URI currentFileURL) {
146146
// visited.add(value);
147147
if(paths.length > 0 && "allOf".equals(paths[paths.length -1])) {
148148
List allOf = (List) value;
149-
// List<String> required = new ArrayList<>();
150-
// Map<String, Object> properties = new LinkedHashMap<>();
151-
// Map<String, Object> mergedAllOfObject = new LinkedHashMap<>();
149+
String[] jsonPaths = Arrays.copyOf(paths, paths.length -1);
150+
String jsonPath = jsonPath(jsonPaths);
151+
Map<String, Object> originalAllOfRoot = refs.jsonContext.read(jsonPath);
152+
152153
AllOfObject allOfObject = new AllOfObject();
154+
merge(allOfObject, originalAllOfRoot);
153155
for (int i = 0; i < allOf.size(); i++) {
154156
if(allOf.get(i) instanceof Map) {
155157
Map<String, Object> item = (Map<String, Object>) allOf.get(i);
@@ -158,14 +160,7 @@ private void mergeAllOf(Object value, String[] paths, URI currentFileURL) {
158160
throw new RuntimeException("Could not understand allOf: " + allOf.get(i));
159161
}
160162
}
161-
// if(!required.isEmpty()) {
162-
// mergedAllOfObject.put("required", required);
163-
// }
164-
// if(!properties.isEmpty()) {
165-
// mergedAllOfObject.put("properties", properties);
166-
// }
167-
String[] jsonPaths = Arrays.copyOf(paths, paths.length -1);
168-
String jsonPath = jsonPath(jsonPaths);
163+
169164
try {
170165
var mergedAllOfObject = allOfObject.buildAllOfObject();
171166
refs.jsonContext.set(jsonPath, mergedAllOfObject);
@@ -199,7 +194,13 @@ private void merge(AllOfObject allOfObject, Map<String, Object> item) {
199194
List<Map<String, Object>> items = (List) item.get("allOf");
200195
merge(allOfObject, items);
201196
} else {
202-
allOfObject.allOf.putAll(item);
197+
for (Map.Entry<String, Object> entry : item.entrySet()) {
198+
if(entry.getKey().equals("allOf")) {
199+
merge(allOfObject, (List) item.get("allOf"));
200+
} else {
201+
allOfObject.allOf.put(entry.getKey(), entry.getValue());
202+
}
203+
}
203204
if(item.containsKey("properties")) {
204205
allOfObject.properties.putAll((Map) item.get("properties"));
205206
}
@@ -214,6 +215,7 @@ private static class AllOfObject {
214215
Map<String, Object> properties = new HashMap<>();
215216
List<String> required = new ArrayList<>();
216217

218+
217219
Map<String, Object> buildAllOfObject() {
218220
Map<String, Object> allOfObject = new LinkedHashMap<>(allOf);
219221
if(!required.isEmpty()) {

src/test/java/io/zenwave360/jsonrefparser/ParserTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ private void assertNoRefs(Object object) throws JsonProcessingException {
3838
Assert.assertFalse(hasRefs);
3939
}
4040

41+
42+
4143
private void assertNoAllOfs(Object object) throws JsonProcessingException {
4244
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
4345
boolean hasRefs = json.contains("allOf");
@@ -186,6 +188,15 @@ public void testDereferenceAndMerge_MultipleAllOf2() throws IOException {
186188
Assert.assertEquals(5, properties.size());
187189
}
188190

191+
@Test
192+
public void testDereferenceAndMergeChainedAllOf() throws IOException {
193+
File file = new File("src/test/resources/asyncapi/car-engine_chained_allOf.yml");
194+
$RefParser parser = new $RefParser(file).parse();
195+
$Refs refs = parser.dereference().mergeAllOf().getRefs();
196+
// Assert.assertFalse(refs.circular);
197+
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(refs.schema()));
198+
assertNoAllOfs(refs.schema());
199+
}
189200

190201
@Test
191202
public void testDereference() throws IOException {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
asyncapi: '2.6.0'
2+
info:
3+
title: Account Service
4+
version: 1.0.0
5+
description: This service is in charge of processing user signups
6+
channels:
7+
exampleOp:
8+
subscribe:
9+
operationId: exampleOp
10+
message:
11+
$ref: '#/components/messages/ExampleMessage'
12+
components:
13+
messages:
14+
ExampleMessage:
15+
payload:
16+
$ref: '#/components/schemas/Car'
17+
schemas:
18+
Base:
19+
type: object
20+
additionalProperties: false
21+
title: Base
22+
properties:
23+
reference:
24+
type: string
25+
Engine:
26+
title: Engine
27+
allOf:
28+
- $ref: '#/components/schemas/Base'
29+
- type: object
30+
title: Engine
31+
additionalProperties: false
32+
properties:
33+
mileage:
34+
type: string
35+
Car:
36+
title: Car
37+
allOf:
38+
- $ref: '#/components/schemas/Engine'
39+
- type: object
40+
title: Car
41+
additionalProperties: false
42+
properties:
43+
make:
44+
type: string
45+

0 commit comments

Comments
 (0)