From 5dfa60ba0adc1243d90fb87517a6cc236154db83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=8A=B9=ED=98=84?= Date: Thu, 1 Oct 2026 16:55:38 +0900 Subject: [PATCH] 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 --- src/main/java/org/json/JSONML.java | 2 +- src/test/java/org/json/junit/JSONMLTest.java | 57 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/json/JSONML.java b/src/main/java/org/json/JSONML.java index 6ec9970..27fe2d5 100644 --- a/src/main/java/org/json/JSONML.java +++ b/src/main/java/org/json/JSONML.java @@ -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(); diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 93a6821..c83f73e 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -1052,4 +1052,61 @@ public class JSONMLTest { } } + /** + * Tests that the childNodes attribute is reserved in object-form parsing. + */ + @Test + public void testToJSONObjectRejectsReservedChildNodesAttribute() { + String xmlStr = "

text

"; + 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 = "

text

"; + 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 = "

text

"; + 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 = "

text

"; + 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)); + } + } + }