Skip to content

Commit 254c451

Browse files
committed
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: spring-projects#219.
1 parent 1c70b91 commit 254c451

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

spring-data-rest-tests/spring-data-rest-tests-mongodb/src/main/java/org/springframework/data/rest/tests/mongodb/User.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
*/
1616
package org.springframework.data.rest.tests.mongodb;
1717

18+
import lombok.Value;
19+
1820
import java.math.BigInteger;
1921
import java.time.LocalDateTime;
22+
import java.util.HashMap;
2023
import java.util.List;
24+
import java.util.Map;
2125
import java.util.Set;
2226

2327
import org.springframework.data.annotation.ReadOnlyProperty;
@@ -47,6 +51,7 @@ public static enum Gender {
4751
public org.joda.time.LocalDateTime jodaDateTime;
4852
public TypeWithPattern pattern;
4953
public @DBRef(lazy = true) List<User> colleagues;
54+
public Map<String, Nested> colleaguesMap = new HashMap<String, Nested>();
5055

5156
public static class EmailAddress {
5257

@@ -67,4 +72,10 @@ public String toString() {
6772
}
6873

6974
public static class TypeWithPattern {}
75+
76+
@Value
77+
public static class Nested {
78+
public @DBRef(lazy = true) User user;
79+
public String foo = "foo";
80+
}
7081
}

spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.hamcrest.Matchers.*;
2020

2121
import java.util.Arrays;
22+
import java.util.HashMap;
2223

2324
import org.junit.Before;
2425
import org.junit.Test;
@@ -36,6 +37,8 @@
3637
import org.springframework.data.rest.tests.mongodb.MongoDbRepositoryConfig;
3738
import org.springframework.data.rest.tests.mongodb.User;
3839
import org.springframework.data.rest.tests.mongodb.User.Gender;
40+
import org.springframework.data.rest.tests.mongodb.User.Nested;
41+
import org.springframework.data.rest.tests.mongodb.UserRepository;
3942
import org.springframework.data.rest.webmvc.PersistentEntityResource;
4043
import org.springframework.hateoas.Link;
4144
import org.springframework.hateoas.LinkDiscoverer;
@@ -65,6 +68,7 @@ public class PersistentEntitySerializationTests {
6568

6669
@Autowired ObjectMapper mapper;
6770
@Autowired Repositories repositories;
71+
@Autowired UserRepository users;
6872

6973
@Configuration
7074
static class TestConfig extends RepositoryTestsConfig {
@@ -122,4 +126,21 @@ public void serializesEmbeddedReferencesCorrectly() throws Exception {
122126
public void deserializesTranslatedEnumProperty() throws Exception {
123127
assertThat(mapper.readValue("{ \"gender\" : \"Male\" }", User.class).gender, is(Gender.MALE));
124128
}
129+
130+
/**
131+
* @see DATAREST-864
132+
*/
133+
@Test
134+
public void createsNestedResourceForMap() throws Exception {
135+
136+
User dave = users.save(new User());
137+
dave.colleaguesMap = new HashMap<String, Nested>();
138+
dave.colleaguesMap.put("carter", new Nested(users.save(new User())));
139+
140+
PersistentEntityResource resource = PersistentEntityResource
141+
.build(dave, repositories.getPersistentEntity(User.class)).build();
142+
143+
assertThat(JsonPath.parse(mapper.writeValueAsString(resource)).read("$.colleaguesMap.carter._links.user.href",
144+
String.class), is(notNullValue()));
145+
}
125146
}

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Collection;
2525
import java.util.Iterator;
2626
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Map.Entry;
2729

2830
import org.slf4j.Logger;
2931
import org.slf4j.LoggerFactory;
@@ -351,6 +353,17 @@ public void serialize(Object value, JsonGenerator gen, SerializerProvider provid
351353

352354
provider.defaultSerializeValue(resources, gen);
353355

356+
} else if (value instanceof Map) {
357+
358+
Map<?, ?> source = (Map<?, ?>) value;
359+
Map<Object, Object> resources = CollectionFactory.createMap(value.getClass(), source.size());
360+
361+
for (Entry<?, ?> entry : source.entrySet()) {
362+
resources.put(entry.getKey(), toResource(entry.getValue()));
363+
}
364+
365+
provider.defaultSerializeValue(resources, gen);
366+
354367
} else {
355368
provider.defaultSerializeValue(toResource(value), gen);
356369
}

0 commit comments

Comments
 (0)