Skip to content

Commit 1103e02

Browse files
committed
Fixed #44
1 parent a2a769a commit 1103e02

File tree

6 files changed

+103
-17
lines changed

6 files changed

+103
-17
lines changed

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

Lines changed: 6 additions & 6 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,15 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
5655
BeanProperty property)
5756
throws JsonMappingException
5857
{
58+
JsonSerializer<?> ser = provider.handleContextualization(_serializer, property);
59+
5960
// If we use eager loading, or force it, can just return underlying serializer as is
6061
if (_forceLazyLoading || !usesLazyLoading(property)) {
61-
if (_serializer instanceof ContextualSerializer) {
62-
return ((ContextualSerializer) _serializer).createContextual(provider, property);
63-
}
64-
return _serializer;
62+
return ser;
63+
}
64+
if (ser != _serializer) {
65+
return new PersistentCollectionSerializer(_forceLazyLoading, ser);
6566
}
66-
// Otherwise this instance is to be used
6767
return this;
6868
}
6969

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+
}

hibernate3/src/test/java/com/fasterxml/jackson/datatype/hibernate3/TestVersions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TestVersions extends BaseTest
1010
* Not a good to do this, but has to do, for now...
1111
*/
1212
private final static int MAJOR_VERSION = 2;
13-
private final static int MINOR_VERSION = 2;
13+
private final static int MINOR_VERSION = 3;
1414

1515
// could inject using Maven filters as well...
1616
private final static String GROUP_ID = "com.fasterxml.jackson.datatype";

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
6161
BeanProperty property)
6262
throws JsonMappingException
6363
{
64+
JsonSerializer<?> ser = provider.handleContextualization(_serializer, property);
65+
6466
// If we use eager loading, or force it, can just return underlying serializer as is
6567
if (_forceLazyLoading || !usesLazyLoading(property)) {
66-
if (_serializer instanceof ContextualSerializer) {
67-
return ((ContextualSerializer) _serializer).createContextual(provider, property);
68-
}
69-
return _serializer;
68+
return ser;
69+
}
70+
if (ser != _serializer) {
71+
return new PersistentCollectionSerializer(_forceLazyLoading, ser);
7072
}
71-
// Otherwise this instance is to be used
7273
return this;
7374
}
7475

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+
}

hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/TestVersions.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ public void testMapperVersions()
1111
assertVersion(new Hibernate4Module());
1212
}
1313

14-
/*
15-
/**********************************************************
16-
/* Helper methods
17-
/**********************************************************
18-
*/
1914
private void assertVersion(Versioned vers)
2015
{
2116
Version v = vers.version();

0 commit comments

Comments
 (0)