Skip to content

Commit cd3f9cb

Browse files
committed
Adapt to renamed properties by using MappedProperties in association deserialization.
Revert the changes that employed manual annotation lookup as that would cause invalid associations of fields and accessor methods for properties shadow renamed. Instead, we now use MappedProperties that already contains a mapping between the Jackson field names and Spring Data property names. Fixes: #2165 $ Conflicts: $ spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java $ spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2ModuleUnitTests.java
1 parent 4ca2691 commit cd3f9cb

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ public static MappedProperties forSerialization(PersistentEntity<?, ?> entity, O
151151
return new MappedProperties(entity, description);
152152
}
153153

154+
public static MappedProperties forDescription(PersistentEntity<?, ?> entity, BeanDescription description) {
155+
return new MappedProperties(entity, description);
156+
}
157+
154158
public static MappedProperties none() {
155159
return new MappedProperties(Collections.emptyMap(), Collections.emptyMap(), Collections.emptySet(),
156160
Collections.emptySet(), false);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,12 @@ public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanD
441441

442442
entities.getPersistentEntity(beanDesc.getBeanClass()).ifPresent(entity -> {
443443

444+
MappedProperties mapped = MappedProperties.forDescription(entity, beanDesc);
445+
444446
while (properties.hasNext()) {
445447

446448
SettableBeanProperty property = properties.next();
447-
PersistentProperty<?> persistentProperty = entity.getPersistentProperty(property.getName());
449+
PersistentProperty<?> persistentProperty = mapped.getPersistentProperty(property.getName());
448450

449451
if (persistentProperty == null) {
450452
continue;

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2ModuleUnitTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,40 @@ void resolvesReferenceToSubtypeCorrectly() throws IOException {
157157
assertThat(petOwner.getPet()).isNotNull();
158158
}
159159

160+
@Test
161+
void allowsUrlsForLinkableAssociation() throws Exception {
162+
163+
when(converter.convert(UriTemplate.of("/homes/1").expand(), TypeDescriptor.valueOf(URI.class),
164+
TypeDescriptor.valueOf(Home.class))).thenReturn(new Home());
165+
166+
PersistentProperty<?> property = persistentEntities.getRequiredPersistentEntity(PetOwner.class)
167+
.getRequiredPersistentProperty("home");
168+
169+
when(associations.isLinkableAssociation(property)).thenReturn(true);
170+
171+
PetOwner petOwner = mapper.readValue("{\"home\": \"/homes/1\" }", PetOwner.class);
172+
173+
assertThat(petOwner).isNotNull();
174+
assertThat(petOwner.getHome()).isInstanceOf(Home.class);
175+
}
176+
177+
@Test
178+
void allowsUrlsForRenamedLinkableAssociation() throws IOException {
179+
180+
when(converter.convert(UriTemplate.of("/packages/1").expand(), TypeDescriptor.valueOf(URI.class),
181+
TypeDescriptor.valueOf(Package.class))).thenReturn(new Package());
182+
183+
PersistentProperty<?> property = persistentEntities.getRequiredPersistentEntity(PetOwner.class)
184+
.getRequiredPersistentProperty("_package");
185+
186+
when(associations.isLinkableAssociation(property)).thenReturn(true);
187+
188+
PetOwner petOwner = mapper.readValue("{\"package\":\"/packages/1\"}", PetOwner.class);
189+
190+
assertThat(petOwner).isNotNull();
191+
assertThat(petOwner._package).isNotNull();
192+
}
193+
160194
@Test // DATAREST-1321
161195
void allowsNumericIdsForLookupTypes() throws Exception {
162196

@@ -260,8 +294,12 @@ static class PetOwner {
260294

261295
Pet pet;
262296
Home home;
297+
298+
@JsonProperty("package") Package _package;
263299
}
264300

301+
static class Package {}
302+
265303
@JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.MINIMAL_CLASS)
266304
static class Pet {}
267305

0 commit comments

Comments
 (0)