Skip to content

Commit

Permalink
Fix array references rewrites (#5634)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal authored Dec 2, 2024
1 parent 6459dcb commit 7d24761
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
Expand Down Expand Up @@ -132,6 +133,8 @@ public ContentHandle rewriteReferences(ContentHandle content, Map<String, String
private void rewriteIn(JsonNode node, Map<String, String> resolvedReferenceUrls) {
if (node.isObject()) {
rewriteInObject((ObjectNode) node, resolvedReferenceUrls);
} else if (node.isArray()) {
rewriteInArray((ArrayNode) node, resolvedReferenceUrls);
}
}

Expand All @@ -155,7 +158,15 @@ private void rewriteInObject(ObjectNode node, Map<String, String> resolvedRefere
JsonNode fieldValue = node.get(fieldName);
if (fieldValue.isObject()) {
rewriteInObject((ObjectNode) fieldValue, resolvedReferenceUrls);
} else if (fieldValue.isArray()) {
rewriteInArray((ArrayNode) fieldValue, resolvedReferenceUrls);
}
}
}

private void rewriteInArray(ArrayNode node, Map<String, String> resolvedReferenceUrls) {
node.forEach(innerNode -> {
rewriteIn(innerNode, resolvedReferenceUrls);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ private static void findExternalTypesIn(JsonNode schema, Set<String> externalTyp
Entry<String, JsonNode> field = fields.next();
findExternalTypesIn(field.getValue(), externalTypes);
}
} else if (schema.isArray()) {
schema.forEach(innerNode -> {
findExternalTypesIn(innerNode, externalTypes);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,33 @@ public void testMultipleRefsUseSingleFile() {
String expectedContent = resourceToString("expected-testDereference-property-level-json.json");
Assertions.assertEquals(normalizeMultiLineString(expectedContent), normalizeMultiLineString(modifiedContent.content()));
}

@Test
public void testDerefAllOf() throws Exception {
ContentHandle content = resourceToContentHandle("order.json");
JsonSchemaDereferencer dereferencer = new JsonSchemaDereferencer();

Map<String, ContentHandle> resolvedReferences = new LinkedHashMap<>();

resolvedReferences.put("customer.json",resourceToContentHandle("customer.json"));

ContentHandle modifiedContent = dereferencer.dereference(content, resolvedReferences);

String expectedContent = resourceToString("expected-order-deref.json");
Assertions.assertEquals(normalizeMultiLineString(expectedContent),
normalizeMultiLineString(modifiedContent.content()));
}

@Test
public void testRewriteAllOfReferences() {
ContentHandle content = resourceToContentHandle("order.json");
JsonSchemaDereferencer dereferencer = new JsonSchemaDereferencer();
ContentHandle modifiedContent = dereferencer.rewriteReferences(content,
Map.of("customer.json", "https://www.example.org/schemas/customer.json"));

ReferenceFinder finder = new JsonSchemaReferenceFinder();
Set<ExternalReference> externalReferences = finder.findExternalReferences(modifiedContent);
Assertions.assertTrue(externalReferences
.contains(new JsonPointerExternalReference("https://www.example.org/schemas/customer.json")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$id": "https://test/schemas/CustomerSchema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Customer",
"type": "object",
"properties": {
"customerId": {
"type": "string",
"description": "A unique identifier for the customer."
},
"name": {
"type": "string",
"description": "The full name of the customer."
},
"email": {
"type": "string",
"format": "email",
"description": "The email address of the customer."
}
},
"required": ["customerId", "name", "email"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"allOf" : [ {
"title" : "Customer",
"type" : "object",
"properties" : {
"customerId" : {
"description" : "A unique identifier for the customer.",
"type" : "string"
},
"name" : {
"description" : "The full name of the customer.",
"type" : "string"
},
"email" : {
"format" : "email",
"description" : "The email address of the customer.",
"type" : "string"
}
},
"required" : [ "customerId", "name", "email" ]
} ],
"oneOf" : [ {
"title" : "Customer",
"type" : "object",
"properties" : {
"customerId" : {
"description" : "A unique identifier for the customer.",
"type" : "string"
},
"name" : {
"description" : "The full name of the customer.",
"type" : "string"
},
"email" : {
"format" : "email",
"description" : "The email address of the customer.",
"type" : "string"
}
},
"required" : [ "customerId", "name", "email" ]
} ],
"$schema" : "http://json-schema.org/draft-07/schema#",
"anyOf" : [ {
"title" : "Customer",
"type" : "object",
"properties" : {
"customerId" : {
"description" : "A unique identifier for the customer.",
"type" : "string"
},
"name" : {
"description" : "The full name of the customer.",
"type" : "string"
},
"email" : {
"format" : "email",
"description" : "The email address of the customer.",
"type" : "string"
}
},
"required" : [ "customerId", "name", "email" ]
} ],
"title" : "Order",
"type" : "object",
"properties" : {
"orderId" : {
"description" : "A unique identifier for the order.",
"type" : "string"
},
"orderDate" : {
"format" : "date-time",
"description" : "The date when the order was placed.",
"type" : "string"
},
"orderTotal" : {
"format" : "float",
"description" : "The total amount of the order.",
"type" : "number"
}
},
"required" : [ "orderId", "customer", "orderTotal" ],
"$id" : "https://test/schemas/OrderSchema.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$id": "https://test/schemas/OrderSchema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Order",
"type": "object",
"allOf": [
{
"$ref": "customer.json"
}
],
"anyOf": [
{
"$ref": "customer.json"
}
],
"oneOf": [
{
"$ref": "customer.json"
}
],
"properties": {
"orderId": {
"type": "string",
"description": "A unique identifier for the order."
},
"orderDate": {
"type": "string",
"format": "date-time",
"description": "The date when the order was placed."
},
"orderTotal": {
"type": "number",
"format": "float",
"description": "The total amount of the order."
}
},
"required": [
"orderId",
"customer",
"orderTotal"
]
}

0 comments on commit 7d24761

Please sign in to comment.