Skip to content

Commit

Permalink
fix: support multiple nested allOf (multiple-allOf.yml)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Jul 2, 2024
1 parent 7277229 commit a85112b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/main/java/io/zenwave360/jsonrefparser/$RefParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,25 @@ private void mergeAllOf(Object value, String[] paths, URI currentFileURL) {
for (int i = 0; i < allOf.size(); i++) {
if(allOf.get(i) instanceof Map) {
Map<String, Object> item = (Map<String, Object>) allOf.get(i);
mergedAllOfObject.putAll(item);
if(item.containsKey("properties")) {
properties.putAll((Map) item.get("properties"));
}
if(item.containsKey("required")) {
required.addAll((List) item.get("required"));
if(item.keySet().size() == 1 && item.containsKey("allOf")) {
List<Map<String, Object>> items = (List) item.get("allOf");
for (Map<String, Object> innerItem : items) {
mergedAllOfObject.putAll(innerItem);
if(innerItem.containsKey("properties")) {
properties.putAll((Map) innerItem.get("properties"));
}
if(innerItem.containsKey("required")) {
required.addAll((List) innerItem.get("required"));
}
}
} else {
mergedAllOfObject.putAll(item);
if(item.containsKey("properties")) {
properties.putAll((Map) item.get("properties"));
}
if(item.containsKey("required")) {
required.addAll((List) item.get("required"));
}
}
} else {
throw new RuntimeException("Could not understand allOf: " + allOf.get(i));
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/zenwave360/jsonrefparser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;

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

Expand Down Expand Up @@ -162,6 +164,18 @@ public void testDereferenceAndMergeAllOf() throws IOException {
assertNoRefs(refs.schema());
}

@Test
public void testDereferenceAndMerge_MultipleAllOf() throws IOException {
File file = new File("src/test/resources/asyncapi/multiple-allOf.yml");
$RefParser parser = new $RefParser(file).parse();
$Refs refs = parser.dereference().mergeAllOf().getRefs();
// Assert.assertFalse(refs.circular);
// System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(refs.schema()));
assertNoRefs(refs.schema());
var properties = (Map) refs.get("$.components.schemas.Test.properties");
Assert.assertEquals(4, properties.size());
}

@Test
public void testDereference() throws IOException {
File file = new File("src/test/resources/openapi/allOf.yml");
Expand Down
44 changes: 44 additions & 0 deletions src/test/resources/asyncapi/multiple-allOf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
asyncapi: 3.0.0
info:
title: Test-API
version: '1.0'
channels:
testTopic:
messages:
test:
name: Test
payload:
$ref: '#/components/schemas/Test'
operations:
publishTest:
action: send
summary: publish events
channel:
$ref: '#/channels/testTopic'
components:
schemas:
Test1:
allOf:
- type: object
properties:
test1a:
type: string
- type: object
properties:
test1b:
type: string
Test2:
allOf:
- type: object
properties:
test2a:
type: string
- type: object
properties:
test2b:
type: string
Test:
type: object
allOf:
- $ref: '#/components/schemas/Test1'
- $ref: '#/components/schemas/Test2'

0 comments on commit a85112b

Please sign in to comment.