Fix reserved childNodes attribute check in JSONML
JSONML object-form parsing uses "childNodes" to store child elements, but the reserved-attribute check incorrectly used "childNode". This rejected ordinary "childNode" attributes while accepting "childNodes" attributes, whose values could then be overwritten by the element's children. Correct the reserved-attribute check to use "childNodes" and add JUnit 4 regression tests covering: - Rejection of reserved "childNodes" attributes in object-form parsing - Preservation of ordinary "childNode" attributes and child content - Continued rejection of reserved "tagName" attributes - Preservation of both attribute names in array-form parsing Verify each scenario with keepStrings enabled and disabled. Validation: - All 40 JSONML tests pass - Full test suite: 807 passed, 6 skipped on Java 11 with Gradle 8.13 - Both bug regression tests fail against the original implementation Fixes #1080
This commit is contained in:
@@ -205,7 +205,7 @@ public class JSONML {
|
||||
// attribute = value
|
||||
|
||||
attribute = (String)token;
|
||||
if (!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) {
|
||||
if (!arrayForm && ("tagName".equals(attribute) || "childNodes".equals(attribute))) {
|
||||
throw x.syntaxError("Reserved attribute.");
|
||||
}
|
||||
token = x.nextToken();
|
||||
|
||||
@@ -1052,4 +1052,61 @@ public class JSONMLTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the childNodes attribute is reserved in object-form parsing.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONObjectRejectsReservedChildNodesAttribute() {
|
||||
String xmlStr = "<p childNodes=\"metadata\">text</p>";
|
||||
for (boolean keepStrings : new boolean[] {false, true}) {
|
||||
JSONException exception = assertThrows(JSONException.class,
|
||||
() -> JSONML.toJSONObject(xmlStr, keepStrings));
|
||||
assertTrue(exception.getMessage().startsWith("Reserved attribute."));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the ordinary childNode attribute is preserved in object-form parsing.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONObjectPreservesChildNodeAttribute() {
|
||||
String xmlStr = "<p childNode=\"metadata\">text</p>";
|
||||
for (boolean keepStrings : new boolean[] {false, true}) {
|
||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr, keepStrings);
|
||||
assertEquals("p", jsonObject.getString("tagName"));
|
||||
assertEquals("metadata", jsonObject.getString("childNode"));
|
||||
assertEquals("text", jsonObject.getJSONArray("childNodes").getString(0));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the tagName attribute remains reserved in object-form parsing.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONObjectRejectsReservedTagNameAttribute() {
|
||||
String xmlStr = "<p tagName=\"metadata\">text</p>";
|
||||
for (boolean keepStrings : new boolean[] {false, true}) {
|
||||
JSONException exception = assertThrows(JSONException.class,
|
||||
() -> JSONML.toJSONObject(xmlStr, keepStrings));
|
||||
assertTrue(exception.getMessage().startsWith("Reserved attribute."));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that childNode and childNodes attributes are preserved in array-form parsing.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArrayPreservesChildNodeAttributes() {
|
||||
String xmlStr = "<p childNode=\"singular\" childNodes=\"plural\">text</p>";
|
||||
for (boolean keepStrings : new boolean[] {false, true}) {
|
||||
JSONArray jsonArray = JSONML.toJSONArray(xmlStr, keepStrings);
|
||||
assertEquals(3, jsonArray.length());
|
||||
assertEquals("p", jsonArray.getString(0));
|
||||
JSONObject attributes = jsonArray.getJSONObject(1);
|
||||
assertEquals("singular", attributes.getString("childNode"));
|
||||
assertEquals("plural", attributes.getString("childNodes"));
|
||||
assertEquals("text", jsonArray.getString(2));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user