Skip to content

Commit

Permalink
Add test for json schema object and property level reference (#5087)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal authored Aug 28, 2024
1 parent d759d6b commit d926060
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ void testAvroApicurioConfluent() throws Exception {

//very important
.withProducerProperty(SerdeConfig.ENABLE_HEADERS, "false")

.withProducerProperty(SerdeConfig.ENABLE_CONFLUENT_ID_HANDLER, "true")
.withProducerProperty(SerdeConfig.USE_ID, IdOption.contentId.name())
.withDeserializer(KafkaAvroDeserializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public void testRewriteReferences() {
}

@Test
public void testDereference() {
ContentHandle content = resourceToContentHandle("json-schema-to-deref.json");
public void testDereferenceObjectLevel() {
ContentHandle content = resourceToContentHandle("json-schema-to-deref-object-level.json");
JsonSchemaDereferencer dereferencer = new JsonSchemaDereferencer();
// Note: order is important. The JSON schema dereferencer needs to convert the ContentHandle Map
// to a JSONSchema map. So it *must* resolve the leaves of the dependency tree before the branches.
Expand All @@ -62,8 +62,24 @@ public void testDereference() {
resolvedReferences.put("types/all-types.json#/definitions/City", resourceToContentHandle("types/all-types.json"));
resolvedReferences.put("types/all-types.json#/definitions/Identifier", resourceToContentHandle("types/all-types.json"));
ContentHandle modifiedContent = dereferencer.dereference(content, resolvedReferences);
String expectedContent = resourceToString("expected-testDereference-json.json");
String expectedContent = resourceToString("expected-testDereference-object-level-json.json");
Assertions.assertEquals(normalizeMultiLineString(expectedContent), normalizeMultiLineString(modifiedContent.content()));
}

@Test
public void testDereferencePropertyLevel() {
ContentHandle content = resourceToContentHandle("json-schema-to-deref-property-level.json");
JsonSchemaDereferencer dereferencer = new JsonSchemaDereferencer();
// Note: order is important. The JSON schema dereferencer needs to convert the ContentHandle Map
// to a JSONSchema map. So it *must* resolve the leaves of the dependency tree before the branches.
Map<String, ContentHandle> resolvedReferences = new LinkedHashMap<>();
resolvedReferences.put("types/city/qualification.json", resourceToContentHandle("types/city/qualification.json"));
resolvedReferences.put("city/qualification.json", resourceToContentHandle("types/city/qualification.json"));
resolvedReferences.put("identifier/qualification.json", resourceToContentHandle("types/identifier/qualification.json"));
resolvedReferences.put("types/all-types.json#/definitions/City/properties/name", resourceToContentHandle("types/all-types.json"));
resolvedReferences.put("types/all-types.json#/definitions/Identifier", resourceToContentHandle("types/all-types.json"));
ContentHandle modifiedContent = dereferencer.dereference(content, resolvedReferences);
String expectedContent = resourceToString("expected-testDereference-property-level-json.json");
Assertions.assertEquals(normalizeMultiLineString(expectedContent), normalizeMultiLineString(modifiedContent.content()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"$schema" : "http://json-schema.org/draft-07/schema#",
"title" : "Citizen",
"type" : "object",
"properties" : {
"firstName" : {
"description" : "The citizen's first name.",
"type" : "string"
},
"lastName" : {
"description" : "The citizen's last name.",
"type" : "string"
},
"identifier" : {
"title" : "Identifier",
"type" : "object",
"properties" : {
"identifier" : {
"description" : "The citizen identifier.",
"type" : "integer",
"minimum" : 0
},
"qualification" : {
"title" : "Qualification",
"type" : "object",
"properties" : {
"qualification" : {
"description" : "The identifier qualification",
"type" : "integer",
"minimum" : 20
},
"name" : {
"description" : "The subject's name",
"type" : "string"
}
}
}
}
},
"qualifications" : {
"type" : "array",
"items" : {
"title" : "Qualification",
"type" : "object",
"properties" : {
"qualification" : {
"description" : "The city qualification",
"type" : "integer",
"minimum" : 10
},
"name" : {
"description" : "The subject's name",
"type" : "string"
}
}
}
},
"city" : {
"description" : "The city's name.",
"type" : "string"
},
"age" : {
"description" : "Age in years which must be equal to or greater than zero.",
"type" : "integer",
"minimum" : 0
}
},
"required" : [ "city" ],
"$id" : "https://example.com/citizen.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$id": "https://example.com/citizen.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Citizen",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The citizen's first name."
},
"lastName": {
"type": "string",
"description": "The citizen's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
},
"city": {
"$ref": "types/all-types.json#/definitions/City/properties/name"
},
"identifier": {
"$ref": "types/all-types.json#/definitions/Identifier"
},
"qualifications": {
"type": "array",
"items": {
"$ref": "types/city/qualification.json"
}
}
},
"required": [
"city"
]
}

0 comments on commit d926060

Please sign in to comment.