Skip to content

Fix mapping URIs to @JsonProperty annotated associations #2169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;

import org.slf4j.Logger;
Expand Down Expand Up @@ -82,6 +83,8 @@
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
Expand Down Expand Up @@ -410,6 +413,7 @@ private Object toModel(Object value, SerializerProvider provider) throws JsonMap
* non-optional associations can be populated on resource creation.
*
* @author Oliver Gierke
* @author Lars Vierbergen
*/
public static class AssociationUriResolvingDeserializerModifier extends BeanDeserializerModifier {

Expand Down Expand Up @@ -444,7 +448,18 @@ public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanD
while (properties.hasNext()) {

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

if (persistentProperty == null) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;

Expand Down Expand Up @@ -96,6 +97,7 @@ void setUp() {

KeyValueMappingContext<?, ?> mappingContext = new KeyValueMappingContext<>();
mappingContext.getPersistentEntity(Sample.class);
mappingContext.getPersistentEntity(Package.class);
mappingContext.getPersistentEntity(SampleWithAdditionalGetters.class);
mappingContext.getPersistentEntity(PersistentEntityJackson2ModuleUnitTests.PetOwner.class);
mappingContext.getPersistentEntity(Immutable.class);
Expand Down Expand Up @@ -157,6 +159,41 @@ void resolvesReferenceToSubtypeCorrectly() throws IOException {
assertThat(petOwner.getPet()).isNotNull();
}

@Test
void allowsUrlsForLinkableAssociation() throws Exception {

when(converter.convert(UriTemplate.of("/homes/1").expand(), TypeDescriptor.valueOf(URI.class),
TypeDescriptor.valueOf(Home.class))).thenReturn(new Home());

PersistentProperty<?> property = persistentEntities.getRequiredPersistentEntity(PetOwner.class)
.getRequiredPersistentProperty("home");

when(associations.isLinkableAssociation(property)).thenReturn(true);

PetOwner petOwner = mapper.readValue("{\"home\": \"/homes/1\" }", PetOwner.class);

assertThat(petOwner).isNotNull();
assertThat(petOwner.getHome()).isInstanceOf(Home.class);
}

@Test
void allowsUrlsForRenamedLinkableAssociation() throws IOException {

when(converter.convert(UriTemplate.of("/packages/1").expand(), TypeDescriptor.valueOf(URI.class),
TypeDescriptor.valueOf(Package.class))).thenReturn(new Package());

PersistentProperty<?> property = persistentEntities.getRequiredPersistentEntity(PetOwner.class)
.getRequiredPersistentProperty("_package");

when(associations.isLinkableAssociation(property)).thenReturn(true);

PetOwner petOwner = mapper.readValue("{\"package\":\"/packages/1\"}", PetOwner.class);

assertThat(petOwner).isNotNull();
assertThat(petOwner.getPackage()).isNotNull();
}


@Test // DATAREST-1321
void allowsNumericIdsForLookupTypes() throws Exception {

Expand Down Expand Up @@ -260,8 +297,17 @@ static class PetOwner {

Pet pet;
Home home;

@Getter(value = AccessLevel.NONE)
@JsonProperty("package") Package _package;

public Package getPackage() {
return _package;
}
}

static class Package {}

@JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.MINIMAL_CLASS)
static class Pet {}

Expand Down