Skip to content

Commit

Permalink
Fix #44 for 2.2.x branch
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 5, 2013
1 parent 86712f7 commit ab631b7
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.hibernate.collection.PersistentCollection;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.*;
Expand Down Expand Up @@ -56,14 +55,18 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer<?> ser = _serializer;
if (ser instanceof ContextualSerializer) {
ser = ((ContextualSerializer) ser).createContextual(provider, property);
}
// If we use eager loading, or force it, can just return underlying serializer as is
if (_forceLazyLoading || !usesLazyLoading(property)) {
if (_serializer instanceof ContextualSerializer) {
return ((ContextualSerializer) _serializer).createContextual(provider, property);
}
return _serializer;
return ser;
}
// Otherwise this instance is to be used
if (ser != _serializer) {
return new PersistentCollectionSerializer(_forceLazyLoading, ser);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.datatype.hibernate3;

import java.util.*;

import javax.persistence.OneToMany;

import com.fasterxml.jackson.databind.ObjectMapper;

public class OneToManyTest extends BaseTest
{
static final String EXPECTED_JSON = "{\"m\":{\"A\":\"A\"}}";

static final class X {
@OneToMany
public final Map<String, String> m = new LinkedHashMap<String, String>();
}

static final class Y {
public final Map<String, String> m = new LinkedHashMap<String, String>();
}

public void testMap() throws Exception {
Y object = new Y();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

public void testMapWithOneToMany() throws Exception {
X object = new X();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

private String mapWithHibernateModule(Object object) throws Exception {
return new ObjectMapper().registerModule(new Hibernate3Module()).writeValueAsString(object);
}

private String mapWithoutHibernateModule(Object object) throws Exception {
return new ObjectMapper().writeValueAsString(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer<?> ser = _serializer;
if (ser instanceof ContextualSerializer) {
ser = ((ContextualSerializer) ser).createContextual(provider, property);
}
// If we use eager loading, or force it, can just return underlying serializer as is
if (_forceLazyLoading || !usesLazyLoading(property)) {
if (_serializer instanceof ContextualSerializer) {
return ((ContextualSerializer) _serializer).createContextual(provider, property);
}
return _serializer;
return ser;
}
// Otherwise this instance is to be used
if (ser != _serializer) {
return new PersistentCollectionSerializer(_forceLazyLoading, ser);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.datatype.hibernate4;

import java.util.*;

import javax.persistence.OneToMany;

import com.fasterxml.jackson.databind.ObjectMapper;

public class OneToManyTest extends BaseTest
{
static final String EXPECTED_JSON = "{\"m\":{\"A\":\"A\"}}";

static final class X {
@OneToMany
public final Map<String, String> m = new LinkedHashMap<String, String>();
}

static final class Y {
public final Map<String, String> m = new LinkedHashMap<String, String>();
}

public void testMap() throws Exception {
Y object = new Y();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

public void testMapWithOneToMany() throws Exception {
X object = new X();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

private String mapWithHibernateModule(Object object) throws Exception {
return new ObjectMapper().registerModule(new Hibernate4Module()).writeValueAsString(object);
}

private String mapWithoutHibernateModule(Object object) throws Exception {
return new ObjectMapper().writeValueAsString(object);
}
}
11 changes: 8 additions & 3 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
Project: jackson-datatype-hibernate
Jackson module to support datatypes of Hibernate 3.6 / 4.x, based on Jackson 2.x

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

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

------------------------------------------------------------------------
=== History: ===
------------------------------------------------------------------------

2.2.3 (23-Aug-2013)

#39: Avoid NullPointerException when serializing ManyToOne proxies
(contributed by drvdijk@github)

2.2.2 (28-May-2013)
2.2.1 (04-May-2013)

Expand Down

0 comments on commit ab631b7

Please sign in to comment.