Skip to content

Commit

Permalink
Added shallow copy for config map
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Kumar committed Sep 6, 2020
1 parent ed9658d commit 56d4130
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/org/json/XMLParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal
*/

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


Expand Down Expand Up @@ -278,7 +279,8 @@ public Map<String, XMLXsiTypeConverter<?>> getXsiTypeMap() {
*/
public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap) {
XMLParserConfiguration newConfig = this.clone();
newConfig.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
Map<String, XMLXsiTypeConverter<?>> cloneXsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>(xsiTypeMap);
newConfig.xsiTypeMap = Collections.unmodifiableMap(cloneXsiTypeMap);
return newConfig;
}
}
35 changes: 35 additions & 0 deletions src/test/java/org/json/junit/XMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1033,4 +1033,39 @@ public void testToJsonWithXSITypeWhenTypeConversionEnabled() {
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
}

@Test
public void testToJsonWithXSITypeWhenTypeConversionNotEnabledOnOne() {
String originalXml = "<root><asString xsi:type=\"string\">12345</asString><asInt>54321</asInt></root>";
String expectedJsonString = "{\"root\":{\"asString\":\"12345\",\"asInt\":54321}}";
JSONObject expectedJson = new JSONObject(expectedJsonString);
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
@Override public String convert(final String value) {
return value;
}
});
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
}

@Test
public void testXSITypeMapNotModifiable() {
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
XMLParserConfiguration config = new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap);
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
@Override public String convert(final String value) {
return value;
}
});
assertEquals("Config Conversion Map size is expected to be 0", 0, config.getXsiTypeMap().size());

try {
config.getXsiTypeMap().put("boolean", new XMLXsiTypeConverter<Boolean>() {
@Override public Boolean convert(final String value) {
return Boolean.valueOf(value);
}
});
fail("Expected to be unable to modify the config");
} catch (Exception ignored) { }
}
}

0 comments on commit 56d4130

Please sign in to comment.