DATAREST-864 - NestedEntitySerializer now handles Maps correctly.
Previously, NestedEntitySerializer failed to handle Maps correctly as the logic to convert the found values to nested resources tried to handle the values as is, not explicitly looking at the values instead. We now use an explicit code path to turn the values into resources so that links pointing to other resources are rendered correctly. Original pull request: #219.
This commit is contained in:
@@ -15,9 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.rest.tests.mongodb;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.annotation.ReadOnlyProperty;
|
||||
@@ -47,6 +51,7 @@ public class User {
|
||||
public org.joda.time.LocalDateTime jodaDateTime;
|
||||
public TypeWithPattern pattern;
|
||||
public @DBRef(lazy = true) List<User> colleagues;
|
||||
public Map<String, Nested> colleaguesMap = new HashMap<String, Nested>();
|
||||
|
||||
public static class EmailAddress {
|
||||
|
||||
@@ -67,4 +72,10 @@ public class User {
|
||||
}
|
||||
|
||||
public static class TypeWithPattern {}
|
||||
|
||||
@Value
|
||||
public static class Nested {
|
||||
public @DBRef(lazy = true) User user;
|
||||
public String foo = "foo";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -36,6 +37,8 @@ import org.springframework.data.rest.tests.mongodb.Address;
|
||||
import org.springframework.data.rest.tests.mongodb.MongoDbRepositoryConfig;
|
||||
import org.springframework.data.rest.tests.mongodb.User;
|
||||
import org.springframework.data.rest.tests.mongodb.User.Gender;
|
||||
import org.springframework.data.rest.tests.mongodb.User.Nested;
|
||||
import org.springframework.data.rest.tests.mongodb.UserRepository;
|
||||
import org.springframework.data.rest.webmvc.PersistentEntityResource;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkDiscoverer;
|
||||
@@ -65,6 +68,7 @@ public class PersistentEntitySerializationTests {
|
||||
|
||||
@Autowired ObjectMapper mapper;
|
||||
@Autowired Repositories repositories;
|
||||
@Autowired UserRepository users;
|
||||
|
||||
@Configuration
|
||||
static class TestConfig extends RepositoryTestsConfig {
|
||||
@@ -122,4 +126,21 @@ public class PersistentEntitySerializationTests {
|
||||
public void deserializesTranslatedEnumProperty() throws Exception {
|
||||
assertThat(mapper.readValue("{ \"gender\" : \"Male\" }", User.class).gender, is(Gender.MALE));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-864
|
||||
*/
|
||||
@Test
|
||||
public void createsNestedResourceForMap() throws Exception {
|
||||
|
||||
User dave = users.save(new User());
|
||||
dave.colleaguesMap = new HashMap<String, Nested>();
|
||||
dave.colleaguesMap.put("carter", new Nested(users.save(new User())));
|
||||
|
||||
PersistentEntityResource resource = PersistentEntityResource
|
||||
.build(dave, repositories.getPersistentEntity(User.class)).build();
|
||||
|
||||
assertThat(JsonPath.parse(mapper.writeValueAsString(resource)).read("$.colleaguesMap.carter._links.user.href",
|
||||
String.class), is(notNullValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -352,6 +354,17 @@ public class PersistentEntityJackson2Module extends SimpleModule {
|
||||
|
||||
provider.defaultSerializeValue(resources, gen);
|
||||
|
||||
} else if (value instanceof Map) {
|
||||
|
||||
Map<?, ?> source = (Map<?, ?>) value;
|
||||
Map<Object, Object> resources = CollectionFactory.createMap(value.getClass(), source.size());
|
||||
|
||||
for (Entry<?, ?> entry : source.entrySet()) {
|
||||
resources.put(entry.getKey(), toResource(entry.getValue()));
|
||||
}
|
||||
|
||||
provider.defaultSerializeValue(resources, gen);
|
||||
|
||||
} else {
|
||||
provider.defaultSerializeValue(toResource(value), gen);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user