Skip to content

Commit

Permalink
Fix an ugly bug in the XML to JSON conversion that would fail on any
Browse files Browse the repository at this point in the history
$ref field that was buried more than 1 level in the schema.
  • Loading branch information
metlos committed Jan 13, 2021
1 parent ed58955 commit e15fc22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
24 changes: 10 additions & 14 deletions revapi/src/main/java/org/revapi/configuration/XmlToJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public JsonNode convertXml(Xml xml) {
continue;
}

JsonNode config = convert(c, schema);
JsonNode config = convert(c, schema, schema);

ObjectNode instanceConfig = JsonNodeFactory.instance.objectNode();
instanceConfig.put("extension", extensionId);
Expand All @@ -174,10 +174,6 @@ public JsonNode convertXml(Xml xml) {
return fullConfiguration;
}

private JsonNode convert(Xml configuration, JsonNode jsonSchema) {
return convert(configuration, jsonSchema, jsonSchema);
}

private JsonNode convert(Xml configuration, JsonNode jsonSchema, JsonNode rootSchema) {
JsonNode typeNode = jsonSchema.get("type");
if (isNullOrUndefined(typeNode)) {
Expand All @@ -188,11 +184,11 @@ private JsonNode convert(Xml configuration, JsonNode jsonSchema, JsonNode rootSc
jsonSchema = findRef(rootSchema, jsonSchema.get("$ref").asText());
ret = convert(configuration, jsonSchema, rootSchema);
} else if (jsonSchema.hasNonNull("oneOf")) {
ret = convertByOneOf(configuration, jsonSchema.get("oneOf"));
ret = convertByOneOf(configuration, jsonSchema.get("oneOf"), rootSchema);
} else if (jsonSchema.hasNonNull("anyOf")) {
ret = convertByAnyOf(configuration, jsonSchema.get("anyOf"));
ret = convertByAnyOf(configuration, jsonSchema.get("anyOf"), rootSchema);
} else if (jsonSchema.hasNonNull("allOf")) {
ret = convertByAllOf(configuration, jsonSchema.get("allOf"));
ret = convertByAllOf(configuration, jsonSchema.get("allOf"), rootSchema);
}

if (ret == null) {
Expand Down Expand Up @@ -344,12 +340,12 @@ private JsonNode convertBoolean(Xml configuration) {
return JsonNodeFactory.instance.booleanNode(boolVal);
}

private JsonNode convertByOneOf(Xml configuration, Iterable<JsonNode> candidateSchemas) {
private JsonNode convertByOneOf(Xml configuration, Iterable<JsonNode> candidateSchemas, JsonNode rootSchema) {
boolean matched = false;
JsonNode parsed = null;
for (JsonNode candidateSchema : candidateSchemas) {
try {
parsed = convert(configuration, candidateSchema);
parsed = convert(configuration, candidateSchema, rootSchema);
if (matched) {
return null;
} else {
Expand All @@ -363,10 +359,10 @@ private JsonNode convertByOneOf(Xml configuration, Iterable<JsonNode> candidateS
return parsed;
}

private JsonNode convertByAnyOf(Xml configuration, Iterable<JsonNode> candidateSchemas) {
private JsonNode convertByAnyOf(Xml configuration, Iterable<JsonNode> candidateSchemas, JsonNode rootSchema) {
for (JsonNode candidateSchema : candidateSchemas) {
try {
return convert(configuration, candidateSchema);
return convert(configuration, candidateSchema, rootSchema);
} catch (IllegalArgumentException __) {
//continue
}
Expand All @@ -375,11 +371,11 @@ private JsonNode convertByAnyOf(Xml configuration, Iterable<JsonNode> candidateS
return null;
}

private JsonNode convertByAllOf(Xml configuration, Iterable<JsonNode> candidateSchemas) {
private JsonNode convertByAllOf(Xml configuration, Iterable<JsonNode> candidateSchemas, JsonNode rootSchema) {
JsonNode parsed = null;
for (JsonNode candidateSchema : candidateSchemas) {
try {
parsed = convert(configuration, candidateSchema);
parsed = convert(configuration, candidateSchema, rootSchema);
} catch (IllegalArgumentException __) {
return null;
}
Expand Down
14 changes: 14 additions & 0 deletions revapi/src/test/java/org/revapi/configuration/XmlToJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,20 @@ public void testAllOf() throws Exception {
Assert.assertEquals(1D, c.asDouble(), 0);
}

@Test
public void testDeepReferences() throws Exception {
XmlToJson<Node> converter = converter("ext", "{\"definitions\": {\"blah\": {\"type\": \"boolean\"}}, \"oneOf\": [{\"type\": \"integer\"}, {\"$ref\": \"#/definitions/blah\"}]}");
Node xml = xml("<config><ext>true</ext></config>");

JsonNode c = converter.convertXml(xml).get(0).get("configuration");

Assert.assertNotNull(c);

Assert.assertTrue(c.isBoolean());
Assert.assertTrue(c.asBoolean());

}

private static Node xml(String xml) throws IOException, ParserConfigurationException, SAXException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Expand Down

0 comments on commit e15fc22

Please sign in to comment.