Skip to content

Commit

Permalink
DATAREST-662 - Tweaked UriStringDeserializer to work for polymorphic …
Browse files Browse the repository at this point in the history
…references.
  • Loading branch information
Valentin Rentschler authored and odrotbohm committed Aug 29, 2015
1 parent e737469 commit b7e650a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.CollectionFactory;
Expand Down Expand Up @@ -411,6 +412,14 @@ public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOE
throw ctxt.weirdStringException(source, URI.class, String.format(UNEXPECTED_VALUE, property));
}
}

/**
* Deserialize by ignoring typeDeserializer, as URI will either resolve to null or concrete instance
*/
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
return deserialize(jp, ctxt);
}
}

@SuppressWarnings("serial")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.rest.core.UriToEntityConverter;
import org.springframework.data.rest.core.config.EnumTranslationConfiguration;
import org.springframework.data.rest.core.config.MetadataConfiguration;
import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration;
Expand All @@ -38,6 +45,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.jayway.jsonpath.JsonPath;
import org.springframework.hateoas.UriTemplate;

/**
* Unit tests for {@link PersistentEntityJackson2Module}.
Expand All @@ -48,6 +56,8 @@
public class PersistentEntityJackson2ModuleUnitTests {

@Mock AssociationLinks associationLinks;
@Mock PersistentEntities repositories;
@Mock UriToEntityConverter converter;

ObjectMapper mapper;

Expand All @@ -65,6 +75,9 @@ public void setUp() {
persistentEntities, associationLinks, new RepositoryRestConfiguration(new ProjectionDefinitionConfiguration(),
new MetadataConfiguration(), mock(EnumTranslationConfiguration.class))));

module.setDeserializerModifier(new PersistentEntityJackson2Module.
AssociationUriResolvingDeserializerModifier(repositories, converter, associationLinks));

this.mapper = new ObjectMapper();
this.mapper.registerModule(module);
}
Expand Down Expand Up @@ -95,6 +108,46 @@ public void rendersAdditionalJsonPropertiesNotBackedByAPersistentField() throws
assertThat(JsonPath.read(result, "$.number"), is((Object) 5));
}

/**
* @see DATAREST-662
*/
@Test
public void isAbleToResolveSubclassedProperty() throws IOException {
PersistentEntity petOwnerPersistentEntity = mock(PersistentEntity.class);
PersistentProperty petProperty = mock(PersistentProperty.class);
when(petProperty.isCollectionLike()).thenReturn(false);
when(petProperty.getActualType()).thenReturn(Pet.class);
when(petOwnerPersistentEntity.getPersistentProperty("pet")).thenReturn(petProperty);
when(repositories.getPersistentEntity(PetOwner.class)).thenReturn(petOwnerPersistentEntity);
when(associationLinks.isLinkableAssociation(petProperty)).thenReturn(true);
when(converter.convert(new UriTemplate("/pets/1").expand(), TypeDescriptor.valueOf(URI.class),
TypeDescriptor.valueOf(Pet.class))).thenReturn(new Cat());

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

assertNotNull(petOwner);
assertNotNull(petOwner.getPet());
}

static class PetOwner {

private Pet pet;

public Pet getPet() {
return pet;
}

}

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

}

static class Cat extends Pet {

}

static class Sample {
public @JsonProperty("foo") String name;
}
Expand Down

0 comments on commit b7e650a

Please sign in to comment.