Skip to content

Commit 56d4130

Browse files
author
Rahul Kumar
committed
Added shallow copy for config map
1 parent ed9658d commit 56d4130

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/main/java/org/json/XMLParserConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal
2424
*/
2525

2626
import java.util.Collections;
27+
import java.util.HashMap;
2728
import java.util.Map;
2829

2930

@@ -278,7 +279,8 @@ public Map<String, XMLXsiTypeConverter<?>> getXsiTypeMap() {
278279
*/
279280
public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap) {
280281
XMLParserConfiguration newConfig = this.clone();
281-
newConfig.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
282+
Map<String, XMLXsiTypeConverter<?>> cloneXsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>(xsiTypeMap);
283+
newConfig.xsiTypeMap = Collections.unmodifiableMap(cloneXsiTypeMap);
282284
return newConfig;
283285
}
284286
}

src/test/java/org/json/junit/XMLTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,4 +1033,39 @@ public void testToJsonWithXSITypeWhenTypeConversionEnabled() {
10331033
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
10341034
}
10351035

1036+
@Test
1037+
public void testToJsonWithXSITypeWhenTypeConversionNotEnabledOnOne() {
1038+
String originalXml = "<root><asString xsi:type=\"string\">12345</asString><asInt>54321</asInt></root>";
1039+
String expectedJsonString = "{\"root\":{\"asString\":\"12345\",\"asInt\":54321}}";
1040+
JSONObject expectedJson = new JSONObject(expectedJsonString);
1041+
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
1042+
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
1043+
@Override public String convert(final String value) {
1044+
return value;
1045+
}
1046+
});
1047+
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
1048+
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
1049+
}
1050+
1051+
@Test
1052+
public void testXSITypeMapNotModifiable() {
1053+
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
1054+
XMLParserConfiguration config = new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap);
1055+
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
1056+
@Override public String convert(final String value) {
1057+
return value;
1058+
}
1059+
});
1060+
assertEquals("Config Conversion Map size is expected to be 0", 0, config.getXsiTypeMap().size());
1061+
1062+
try {
1063+
config.getXsiTypeMap().put("boolean", new XMLXsiTypeConverter<Boolean>() {
1064+
@Override public Boolean convert(final String value) {
1065+
return Boolean.valueOf(value);
1066+
}
1067+
});
1068+
fail("Expected to be unable to modify the config");
1069+
} catch (Exception ignored) { }
1070+
}
10361071
}

0 commit comments

Comments
 (0)