Skip to content

Commit b65e0c9

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 Sprign Data property names. Fixes: #2165
1 parent 5335fe6 commit b65e0c9

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
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 & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Map.Entry;
27-
import java.util.Objects;
2827
import java.util.Optional;
2928

3029
import org.slf4j.Logger;
@@ -83,8 +82,6 @@
8382
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
8483
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
8584
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
86-
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
87-
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
8885
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
8986
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
9087
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -445,21 +442,12 @@ public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanD
445442

446443
entities.getPersistentEntity(beanDesc.getBeanClass()).ifPresent(entity -> {
447444

445+
MappedProperties mapped = MappedProperties.forDescription(entity, beanDesc);
446+
448447
while (properties.hasNext()) {
449448

450449
SettableBeanProperty property = properties.next();
451-
// To find the PersistentProperty name in case there is a @JsonProperty annotation
452-
// on the field. Both BeanPropertyDefinition#getName() and BeanPropertyDefinition#getInternalName()
453-
// don't return the actual name of the field, so we look up the AnnotatedField itself to retrieve
454-
// the real name from, so it can be used for PersistentProperty lookup
455-
String persistentPropertyName = beanDesc.findProperties().stream()
456-
.filter(propertyDefinition -> property.getName().equals(propertyDefinition.getName()))
457-
.map(BeanPropertyDefinition::getField).filter(Objects::nonNull).map(AnnotatedField::getName).findFirst()
458-
// Fall back to the JSON name in case we can't find a BeanPropertyDefinition,
459-
// so things can be mapped by convention in case they are immutable objects and are
460-
// using constructor injection
461-
.orElse(property.getName());
462-
PersistentProperty<?> persistentProperty = entity.getPersistentProperty(persistentPropertyName);
450+
PersistentProperty<?> persistentProperty = mapped.getPersistentProperty(property.getName());
463451

464452
if (persistentProperty == null) {
465453
continue;

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.mockito.ArgumentMatchers.*;
2020
import static org.mockito.Mockito.*;
2121

22-
import lombok.AccessLevel;
2322
import lombok.Data;
2423
import lombok.Getter;
2524

@@ -190,10 +189,9 @@ void allowsUrlsForRenamedLinkableAssociation() throws IOException {
190189
PetOwner petOwner = mapper.readValue("{\"package\":\"/packages/1\"}", PetOwner.class);
191190

192191
assertThat(petOwner).isNotNull();
193-
assertThat(petOwner.getPackage()).isNotNull();
192+
assertThat(petOwner._package).isNotNull();
194193
}
195194

196-
197195
@Test // DATAREST-1321
198196
void allowsNumericIdsForLookupTypes() throws Exception {
199197

@@ -298,12 +296,7 @@ static class PetOwner {
298296
Pet pet;
299297
Home home;
300298

301-
@Getter(value = AccessLevel.NONE)
302299
@JsonProperty("package") Package _package;
303-
304-
public Package getPackage() {
305-
return _package;
306-
}
307300
}
308301

309302
static class Package {}

0 commit comments

Comments
 (0)