This commit is contained in:
committed by
Oliver Gierke
parent
23e78a7418
commit
693faae0fe
@@ -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<Object> currentValue = embeddeds.get(singleRel);
|
||||
|
||||
|
||||
@@ -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.<List<Object>> 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<String, List<Object>> setUpBuilder(Object... values) {
|
||||
|
||||
HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider);
|
||||
|
||||
Reference in New Issue
Block a user