diff --git a/src/main/java/org/springframework/hateoas/hal/HalEmbeddedBuilder.java b/src/main/java/org/springframework/hateoas/hal/HalEmbeddedBuilder.java index 8fea5533..f540314f 100644 --- a/src/main/java/org/springframework/hateoas/hal/HalEmbeddedBuilder.java +++ b/src/main/java/org/springframework/hateoas/hal/HalEmbeddedBuilder.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; import org.springframework.hateoas.RelProvider; +import org.springframework.hateoas.Resource; import org.springframework.hateoas.core.ObjectUtils; /** @@ -29,6 +30,7 @@ import org.springframework.hateoas.core.ObjectUtils; * single resource relation to the collection one, once more than one object of the same type is added. * * @author Oliver Gierke + * @author Dietrich Schulten */ class HalEmbeddedBuilder { @@ -47,13 +49,19 @@ class HalEmbeddedBuilder { } /** - * Adds the given value to the embeddeds. + * Adds the given value to the embeddeds. Will skip doing so if the value is {@literal null} or the content of a + * {@link Resource} is {@literal null}. * * @param value */ public void add(Object value) { Class type = ObjectUtils.getResourceType(value); + + if (type == null) { + return; + } + String singleRel = getDefaultedRelFor(type, false); List currentValue = embeddeds.get(singleRel); diff --git a/src/test/java/org/springframework/hateoas/hal/HalEmbeddedBuilderUnitTests.java b/src/test/java/org/springframework/hateoas/hal/HalEmbeddedBuilderUnitTests.java index 8c4af518..fc3c12d1 100644 --- a/src/test/java/org/springframework/hateoas/hal/HalEmbeddedBuilderUnitTests.java +++ b/src/test/java/org/springframework/hateoas/hal/HalEmbeddedBuilderUnitTests.java @@ -24,13 +24,16 @@ import java.util.Map; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.BeanUtils; import org.springframework.hateoas.RelProvider; +import org.springframework.hateoas.Resource; import org.springframework.hateoas.core.EvoInflectorRelProvider; /** * Unit tests for {@link HalEmbeddedBuilder}. * * @author Oliver Gierke + * @author Dietrich Schulten */ public class HalEmbeddedBuilderUnitTests { @@ -60,6 +63,19 @@ public class HalEmbeddedBuilderUnitTests { assertThat(map.get("long"), Matchers.> allOf(hasSize(1), hasItem(1L))); } + /** + * @see #81, #83 + */ + @Test + public void addsNoEmbeddedsForResourceWithoutContent() { + + Resource resource = BeanUtils.instantiateClass(Resource.class); + HalEmbeddedBuilder halEmbeddedBuilder = new HalEmbeddedBuilder(provider); + halEmbeddedBuilder.add(resource); + + assertThat(halEmbeddedBuilder.asMap().isEmpty(), is(true)); + } + private Map> setUpBuilder(Object... values) { HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider);