Skip to content

Commit ab631b7

Browse files
committed
Fix #44 for 2.2.x branch
1 parent 86712f7 commit ab631b7

File tree

5 files changed

+114
-12
lines changed

5 files changed

+114
-12
lines changed

hibernate3/src/main/java/com/fasterxml/jackson/datatype/hibernate3/PersistentCollectionSerializer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.hibernate.collection.PersistentCollection;
88

99
import com.fasterxml.jackson.core.*;
10-
1110
import com.fasterxml.jackson.databind.*;
1211
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
1312
import com.fasterxml.jackson.databind.ser.*;
@@ -56,14 +55,18 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
5655
BeanProperty property)
5756
throws JsonMappingException
5857
{
58+
JsonSerializer<?> ser = _serializer;
59+
if (ser instanceof ContextualSerializer) {
60+
ser = ((ContextualSerializer) ser).createContextual(provider, property);
61+
}
5962
// If we use eager loading, or force it, can just return underlying serializer as is
6063
if (_forceLazyLoading || !usesLazyLoading(property)) {
61-
if (_serializer instanceof ContextualSerializer) {
62-
return ((ContextualSerializer) _serializer).createContextual(provider, property);
63-
}
64-
return _serializer;
64+
return ser;
6565
}
6666
// Otherwise this instance is to be used
67+
if (ser != _serializer) {
68+
return new PersistentCollectionSerializer(_forceLazyLoading, ser);
69+
}
6770
return this;
6871
}
6972

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fasterxml.jackson.datatype.hibernate3;
2+
3+
import java.util.*;
4+
5+
import javax.persistence.OneToMany;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
public class OneToManyTest extends BaseTest
10+
{
11+
static final String EXPECTED_JSON = "{\"m\":{\"A\":\"A\"}}";
12+
13+
static final class X {
14+
@OneToMany
15+
public final Map<String, String> m = new LinkedHashMap<String, String>();
16+
}
17+
18+
static final class Y {
19+
public final Map<String, String> m = new LinkedHashMap<String, String>();
20+
}
21+
22+
public void testMap() throws Exception {
23+
Y object = new Y();
24+
object.m.put("A", "A");
25+
26+
assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
27+
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
28+
}
29+
30+
public void testMapWithOneToMany() throws Exception {
31+
X object = new X();
32+
object.m.put("A", "A");
33+
34+
assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
35+
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
36+
}
37+
38+
private String mapWithHibernateModule(Object object) throws Exception {
39+
return new ObjectMapper().registerModule(new Hibernate3Module()).writeValueAsString(object);
40+
}
41+
42+
private String mapWithoutHibernateModule(Object object) throws Exception {
43+
return new ObjectMapper().writeValueAsString(object);
44+
}
45+
}

hibernate4/src/main/java/com/fasterxml/jackson/datatype/hibernate4/PersistentCollectionSerializer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
6161
BeanProperty property)
6262
throws JsonMappingException
6363
{
64+
JsonSerializer<?> ser = _serializer;
65+
if (ser instanceof ContextualSerializer) {
66+
ser = ((ContextualSerializer) ser).createContextual(provider, property);
67+
}
6468
// If we use eager loading, or force it, can just return underlying serializer as is
6569
if (_forceLazyLoading || !usesLazyLoading(property)) {
66-
if (_serializer instanceof ContextualSerializer) {
67-
return ((ContextualSerializer) _serializer).createContextual(provider, property);
68-
}
69-
return _serializer;
70+
return ser;
7071
}
7172
// Otherwise this instance is to be used
73+
if (ser != _serializer) {
74+
return new PersistentCollectionSerializer(_forceLazyLoading, ser);
75+
}
7276
return this;
7377
}
7478

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fasterxml.jackson.datatype.hibernate4;
2+
3+
import java.util.*;
4+
5+
import javax.persistence.OneToMany;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
public class OneToManyTest extends BaseTest
10+
{
11+
static final String EXPECTED_JSON = "{\"m\":{\"A\":\"A\"}}";
12+
13+
static final class X {
14+
@OneToMany
15+
public final Map<String, String> m = new LinkedHashMap<String, String>();
16+
}
17+
18+
static final class Y {
19+
public final Map<String, String> m = new LinkedHashMap<String, String>();
20+
}
21+
22+
public void testMap() throws Exception {
23+
Y object = new Y();
24+
object.m.put("A", "A");
25+
26+
assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
27+
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
28+
}
29+
30+
public void testMapWithOneToMany() throws Exception {
31+
X object = new X();
32+
object.m.put("A", "A");
33+
34+
assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
35+
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
36+
}
37+
38+
private String mapWithHibernateModule(Object object) throws Exception {
39+
return new ObjectMapper().registerModule(new Hibernate4Module()).writeValueAsString(object);
40+
}
41+
42+
private String mapWithoutHibernateModule(Object object) throws Exception {
43+
return new ObjectMapper().writeValueAsString(object);
44+
}
45+
}

release-notes/VERSION

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
Project: jackson-datatype-hibernate
22
Jackson module to support datatypes of Hibernate 3.6 / 4.x, based on Jackson 2.x
33

4-
Version: 2.2.3 (23-Aug-2013)
4+
Version: 2.2.4 (xx-xxx-2013)
55

6-
#39: Avoid NullPointerException when serializing ManyToOne proxies
7-
(contributed by drvdijk@github)
6+
#44: NullPointerException when @OneToMany map is encountered
7+
(reported by Patrick H)
88

99
------------------------------------------------------------------------
1010
=== History: ===
1111
------------------------------------------------------------------------
1212

13+
2.2.3 (23-Aug-2013)
14+
15+
#39: Avoid NullPointerException when serializing ManyToOne proxies
16+
(contributed by drvdijk@github)
17+
1318
2.2.2 (28-May-2013)
1419
2.2.1 (04-May-2013)
1520

0 commit comments

Comments
 (0)