Skip to content

Commit 5734ef6

Browse files
committed
Restore JacksonUtils.asMap to returning mutable maps.
This was the behavior before commit 97eeb08 and apparently several json-schema-core tests rely on this behavior. Added a test.
1 parent 5f6c1a5 commit 5734ef6

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/main/java/com/github/fge/jackson/JacksonUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
import java.io.IOException;
3434
import java.io.StringWriter;
35-
import java.util.Collections;
3635
import java.util.HashMap;
3736
import java.util.Iterator;
3837
import java.util.Map;
@@ -95,11 +94,11 @@ public static JsonNodeFactory nodeFactory()
9594
*/
9695
public static Map<String, JsonNode> asMap(final JsonNode node)
9796
{
97+
final Map<String, JsonNode> ret = new HashMap<>();
9898
if (!node.isObject())
99-
return Collections.emptyMap();
99+
return ret;
100100

101101
final Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
102-
final Map<String, JsonNode> ret = new HashMap<>();
103102

104103
Map.Entry<String, JsonNode> entry;
105104

@@ -108,7 +107,7 @@ public static Map<String, JsonNode> asMap(final JsonNode node)
108107
ret.put(entry.getKey(), entry.getValue());
109108
}
110109

111-
return Collections.unmodifiableMap(ret);
110+
return ret;
112111
}
113112

114113
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
3+
*
4+
* This software is dual-licensed under:
5+
*
6+
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
7+
* later version;
8+
* - the Apache Software License (ASL) version 2.0.
9+
*
10+
* The text of this file and of both licenses is available at the root of this
11+
* project or, if you have the jar distribution, in directory META-INF/, under
12+
* the names LGPL-3.0.txt and ASL-2.0.txt respectively.
13+
*
14+
* Direct link to the sources:
15+
*
16+
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
17+
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
18+
*/
19+
20+
package com.github.fge.jackson;
21+
22+
import com.fasterxml.jackson.databind.JsonNode;
23+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
24+
import org.testng.annotations.BeforeClass;
25+
import org.testng.annotations.Test;
26+
27+
import java.io.IOException;
28+
import java.util.Map;
29+
30+
import static org.testng.Assert.*;
31+
32+
public final class JacksonUtilsTest
33+
{
34+
private JsonNode testData;
35+
36+
@BeforeClass
37+
public void initData()
38+
throws IOException
39+
{
40+
testData = JsonLoader.fromResource("/testfile.json");
41+
}
42+
43+
@Test()
44+
public void asMapIsMutable() {
45+
Map<String, JsonNode> map = JacksonUtils.asMap(testData.required(0));
46+
map.remove("reference");
47+
}
48+
}

0 commit comments

Comments
 (0)