DATAREST-662 - Polishing.
Simplified test case. Added author tags.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,7 +21,6 @@ import java.util.ArrayList;
|
||||
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;
|
||||
@@ -63,6 +62,7 @@ import com.fasterxml.jackson.databind.deser.ValueInstantiator;
|
||||
import com.fasterxml.jackson.databind.deser.std.CollectionDeserializer;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
|
||||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerBuilder;
|
||||
@@ -364,6 +364,7 @@ public class PersistentEntityJackson2Module extends SimpleModule {
|
||||
* {@link UriToEntityConverter}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Valentin Rentschler
|
||||
*/
|
||||
static class UriStringDeserializer extends StdDeserializer<Object> {
|
||||
|
||||
@@ -412,10 +413,16 @@ public class PersistentEntityJackson2Module extends SimpleModule {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize by ignoring typeDeserializer, as URI will either resolve to null or concrete instance
|
||||
* Deserialize by ignoring the {@link TypeDeserializer}, as URIs will either resolve to {@literal null} or a
|
||||
* concrete instance anyway.
|
||||
*
|
||||
* @see com.fasterxml.jackson.databind.deser.std.StdDeserializer#deserializeWithType(com.fasterxml.jackson.core.JsonParser,
|
||||
* com.fasterxml.jackson.databind.DeserializationContext,
|
||||
* com.fasterxml.jackson.databind.jsontype.TypeDeserializer)
|
||||
*/
|
||||
@Override
|
||||
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
|
||||
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer)
|
||||
throws IOException {
|
||||
return deserialize(jp, ctxt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ 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;
|
||||
@@ -48,14 +47,15 @@ import com.jayway.jsonpath.JsonPath;
|
||||
* Unit tests for {@link PersistentEntityJackson2Module}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Valentin Rentschler
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PersistentEntityJackson2ModuleUnitTests {
|
||||
|
||||
@Mock AssociationLinks associationLinks;
|
||||
@Mock PersistentEntities repositories;
|
||||
@Mock UriToEntityConverter converter;
|
||||
|
||||
PersistentEntities persistentEntities;
|
||||
ObjectMapper mapper;
|
||||
|
||||
@Before
|
||||
@@ -64,15 +64,20 @@ public class PersistentEntityJackson2ModuleUnitTests {
|
||||
MongoMappingContext mappingContext = new MongoMappingContext();
|
||||
mappingContext.getPersistentEntity(Sample.class);
|
||||
mappingContext.getPersistentEntity(SampleWithAdditionalGetters.class);
|
||||
mappingContext.getPersistentEntity(PersistentEntityJackson2ModuleUnitTests.PetOwner.class);
|
||||
|
||||
PersistentEntities persistentEntities = new PersistentEntities(Arrays.asList(mappingContext));
|
||||
this.persistentEntities = new PersistentEntities(Arrays.asList(mappingContext));
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.setSerializerModifier(new PersistentEntityJackson2Module.AssociationOmittingSerializerModifier(
|
||||
persistentEntities, associationLinks, new RepositoryRestConfiguration()));
|
||||
|
||||
module.setDeserializerModifier(new PersistentEntityJackson2Module.AssociationUriResolvingDeserializerModifier(
|
||||
persistentEntities, converter, associationLinks));
|
||||
|
||||
this.mapper = new ObjectMapper();
|
||||
this.mapper.registerModule(module);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,41 +110,34 @@ public class PersistentEntityJackson2ModuleUnitTests {
|
||||
* @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);
|
||||
public void resolvesReferenceToSubtypeCorrectly() throws IOException {
|
||||
|
||||
PersistentProperty<?> property = persistentEntities.getPersistentEntity(PetOwner.class)
|
||||
.getPersistentProperty("pet");
|
||||
|
||||
when(associationLinks.isLinkableAssociation(property)).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());
|
||||
assertThat(petOwner, is(notNullValue()));
|
||||
assertThat(petOwner.getPet(), is(notNullValue()));
|
||||
}
|
||||
|
||||
static class PetOwner {
|
||||
|
||||
private Pet pet;
|
||||
Pet pet;
|
||||
|
||||
public Pet getPet() {
|
||||
return pet;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.MINIMAL_CLASS)
|
||||
static class Pet {
|
||||
static class Pet {}
|
||||
|
||||
}
|
||||
|
||||
static class Cat extends Pet {
|
||||
|
||||
}
|
||||
static class Cat extends Pet {}
|
||||
|
||||
static class Sample {
|
||||
public @JsonProperty("foo") String name;
|
||||
|
||||
Reference in New Issue
Block a user