DATAREST-575 - Fixed property lookup in MappedProperties.

Previously, MappedProperties didn't handle Jackson properties correctly, that do not expose a PersistentProperty, e.g. transient ones. That led to potential nullPointerExceptions in clients as the guarding hasPersistentPropertyForField(…) still answered true, as the backing cache contained an entry with a null value. We now skip those properties completely.
This commit is contained in:
Oliver Gierke
2016-10-23 20:43:46 +02:00
parent e855f73af1
commit c5948e6690
2 changed files with 142 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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.
@@ -51,7 +51,6 @@ public class DomainObjectReader {
private final PersistentEntities entities;
private final AssociationLinks associationLinks;
private final ClassIntrospector introspector;
/**
* Creates a new {@link DomainObjectReader} using the given {@link PersistentEntities} and {@link ResourceMappings}.
@@ -66,7 +65,6 @@ public class DomainObjectReader {
this.entities = entities;
this.associationLinks = new AssociationLinks(mappings);
this.introspector = new BasicClassIntrospector();
}
/**
@@ -110,7 +108,7 @@ public class DomainObjectReader {
Assert.notNull(entity, "No PersistentEntity found for ".concat(type.getName()).concat("!"));
final MappedProperties properties = getJacksonProperties(entity, mapper);
final MappedProperties properties = MappedProperties.fromJacksonProperties(entity, mapper);
entity.doWithProperties(new SimplePropertyHandler() {
@@ -169,7 +167,7 @@ public class DomainObjectReader {
return mapper.readerForUpdating(target).readValue(root);
}
MappedProperties mappedProperties = getJacksonProperties(entity, mapper);
MappedProperties mappedProperties = MappedProperties.fromJacksonProperties(entity, mapper);
for (Iterator<Entry<String, JsonNode>> i = root.fields(); i.hasNext();) {
@@ -255,27 +253,14 @@ public class DomainObjectReader {
}
}
/**
* Returns the {@link MappedProperties} for the given {@link PersistentEntity}.
*
* @param entity must not be {@literal null}.
* @param mapper must not be {@literal null}.
* @return
*/
private MappedProperties getJacksonProperties(PersistentEntity<?, ?> entity, ObjectMapper mapper) {
BeanDescription description = introspector.forDeserialization(mapper.getDeserializationConfig(),
mapper.constructType(entity.getType()), mapper.getDeserializationConfig());
return new MappedProperties(entity, description);
}
/**
* Simple value object to capture a mapping of Jackson mapped field names and {@link PersistentProperty} instances.
*
* @author Oliver Gierke
*/
private static class MappedProperties {
static class MappedProperties {
private static final ClassIntrospector INTROSPECTOR = new BasicClassIntrospector();
private final Map<PersistentProperty<?>, String> propertyToFieldName;
private final Map<String, PersistentProperty<?>> fieldNameToProperty;
@@ -283,11 +268,14 @@ public class DomainObjectReader {
/**
* Creates a new {@link MappedProperties} instance for the given {@link PersistentEntity} and
* {@link BeanDescription}.
*
*
* @param entity must not be {@literal null}.
* @param description must not be {@literal null}.
*/
public MappedProperties(PersistentEntity<?, ?> entity, BeanDescription description) {
private MappedProperties(PersistentEntity<?, ?> entity, BeanDescription description) {
Assert.notNull(entity, "Entity must not be null!");
Assert.notNull(description, "BeanDescription must not be null!");
this.propertyToFieldName = new HashMap<PersistentProperty<?>, String>();
this.fieldNameToProperty = new HashMap<String, PersistentProperty<?>>();
@@ -296,20 +284,58 @@ public class DomainObjectReader {
PersistentProperty<?> persistentProperty = entity.getPersistentProperty(property.getInternalName());
propertyToFieldName.put(persistentProperty, property.getName());
fieldNameToProperty.put(property.getName(), persistentProperty);
if (persistentProperty != null) {
propertyToFieldName.put(persistentProperty, property.getName());
fieldNameToProperty.put(property.getName(), persistentProperty);
}
}
}
/**
* Creates {@link MappedProperties} for the given {@link PersistentEntity}.
*
* @param entity must not be {@literal null}.
* @param mapper must not be {@literal null}.
* @return
*/
public static MappedProperties fromJacksonProperties(PersistentEntity<?, ?> entity, ObjectMapper mapper) {
BeanDescription description = INTROSPECTOR.forDeserialization(mapper.getDeserializationConfig(),
mapper.constructType(entity.getType()), mapper.getDeserializationConfig());
return new MappedProperties(entity, description);
}
/**
* @param property must not be {@literal null}
* @return the mapped name for the {@link PersistentProperty}
*/
public String getMappedName(PersistentProperty<?> property) {
Assert.notNull(property, "PersistentProperty must not be null!");
return propertyToFieldName.get(property);
}
/**
* @param fieldName must not be empty or {@literal null}.
* @return {@literal true} if the field name resolves to a {@literal PersistentProperty}.
*/
public boolean hasPersistentPropertyForField(String fieldName) {
Assert.hasText(fieldName, "Field name must not be null or empty!");
return fieldNameToProperty.containsKey(fieldName);
}
/**
* @param fieldName must not be empty or {@literal null}.
* @return the {@link PersistentProperty} backing the field with the field name.
*/
public PersistentProperty<?> getPersistentProperty(String fieldName) {
Assert.hasText(fieldName, "Field name must not be null or empty!");
return fieldNameToProperty.get(fieldName);
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.webmvc.json;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.rest.webmvc.json.DomainObjectReader.MappedProperties;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Unit tests for {@link MappedProperties}.
*
* @author Oliver Gierke
*/
public class MappedPropertiesUnitTests {
ObjectMapper mapper = new ObjectMapper();
MongoMappingContext context = new MongoMappingContext();
MongoPersistentEntity<?> entity = context.getPersistentEntity(Sample.class);
MappedProperties properties = MappedProperties.fromJacksonProperties(entity, mapper);
/**
* @see DATAREST-575
*/
@Test
public void doesNotExposeMappedPropertyForNonSpringDataPersistentProperty() {
assertThat(properties.hasPersistentPropertyForField("notExposedBySpringData"), is(false));
assertThat(properties.getPersistentProperty("notExposedBySpringData"), is(nullValue()));
}
/**
* @see DATAREST-575
*/
@Test
public void doesNotExposeMappedPropertyForNonJacksonProperty() {
assertThat(properties.hasPersistentPropertyForField("notExposedByJackson"), is(false));
assertThat(properties.getPersistentProperty("notExposedByJackson"), is(nullValue()));
}
/**
* @see DATAREST-575
*/
@Test
public void exposesProperty() {
assertThat(properties.hasPersistentPropertyForField("exposedProperty"), is(true));
assertThat(properties.getPersistentProperty("exposedProperty"), is(notNullValue()));
}
/**
* @see DATAREST-575
*/
@Test
public void exposesRenamedPropertyByExternalName() {
assertThat(properties.hasPersistentPropertyForField("email"), is(true));
assertThat(properties.getPersistentProperty("email"), is(notNullValue()));
assertThat(properties.getMappedName(entity.getPersistentProperty("emailAddress")), is("email"));
}
static class Sample {
public @Transient String notExposedBySpringData;
public @JsonIgnore String notExposedByJackson;
public String exposedProperty;
public @JsonProperty("email") String emailAddress;
}
}