Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a85112b

Browse files
committedJul 2, 2024··
fix: support multiple nested allOf (multiple-allOf.yml)
1 parent 7277229 commit a85112b

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed
 

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,25 @@ private void mergeAllOf(Object value, String[] paths, URI currentFileURL) {
154154
for (int i = 0; i < allOf.size(); i++) {
155155
if(allOf.get(i) instanceof Map) {
156156
Map<String, Object> item = (Map<String, Object>) allOf.get(i);
157-
mergedAllOfObject.putAll(item);
158-
if(item.containsKey("properties")) {
159-
properties.putAll((Map) item.get("properties"));
160-
}
161-
if(item.containsKey("required")) {
162-
required.addAll((List) item.get("required"));
157+
if(item.keySet().size() == 1 && item.containsKey("allOf")) {
158+
List<Map<String, Object>> items = (List) item.get("allOf");
159+
for (Map<String, Object> innerItem : items) {
160+
mergedAllOfObject.putAll(innerItem);
161+
if(innerItem.containsKey("properties")) {
162+
properties.putAll((Map) innerItem.get("properties"));
163+
}
164+
if(innerItem.containsKey("required")) {
165+
required.addAll((List) innerItem.get("required"));
166+
}
167+
}
168+
} else {
169+
mergedAllOfObject.putAll(item);
170+
if(item.containsKey("properties")) {
171+
properties.putAll((Map) item.get("properties"));
172+
}
173+
if(item.containsKey("required")) {
174+
required.addAll((List) item.get("required"));
175+
}
163176
}
164177
} else {
165178
throw new RuntimeException("Could not understand allOf: " + allOf.get(i));

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.io.File;
1717
import java.io.IOException;
1818
import java.net.URI;
19+
import java.util.List;
20+
import java.util.Map;
1921

2022
import static io.zenwave360.jsonrefparser.$RefParserOptions.OnCircular.FAIL;
2123

@@ -162,6 +164,18 @@ public void testDereferenceAndMergeAllOf() throws IOException {
162164
assertNoRefs(refs.schema());
163165
}
164166

167+
@Test
168+
public void testDereferenceAndMerge_MultipleAllOf() throws IOException {
169+
File file = new File("src/test/resources/asyncapi/multiple-allOf.yml");
170+
$RefParser parser = new $RefParser(file).parse();
171+
$Refs refs = parser.dereference().mergeAllOf().getRefs();
172+
// Assert.assertFalse(refs.circular);
173+
// System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(refs.schema()));
174+
assertNoRefs(refs.schema());
175+
var properties = (Map) refs.get("$.components.schemas.Test.properties");
176+
Assert.assertEquals(4, properties.size());
177+
}
178+
165179
@Test
166180
public void testDereference() throws IOException {
167181
File file = new File("src/test/resources/openapi/allOf.yml");
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
asyncapi: 3.0.0
2+
info:
3+
title: Test-API
4+
version: '1.0'
5+
channels:
6+
testTopic:
7+
messages:
8+
test:
9+
name: Test
10+
payload:
11+
$ref: '#/components/schemas/Test'
12+
operations:
13+
publishTest:
14+
action: send
15+
summary: publish events
16+
channel:
17+
$ref: '#/channels/testTopic'
18+
components:
19+
schemas:
20+
Test1:
21+
allOf:
22+
- type: object
23+
properties:
24+
test1a:
25+
type: string
26+
- type: object
27+
properties:
28+
test1b:
29+
type: string
30+
Test2:
31+
allOf:
32+
- type: object
33+
properties:
34+
test2a:
35+
type: string
36+
- type: object
37+
properties:
38+
test2b:
39+
type: string
40+
Test:
41+
type: object
42+
allOf:
43+
- $ref: '#/components/schemas/Test1'
44+
- $ref: '#/components/schemas/Test2'

0 commit comments

Comments
 (0)
Please sign in to comment.