DATAREST-929 - EnumTranslatingDeserializers now handles container types correctly.

When translating enumerations, we now inspect the property and use a container type's value type as translation target.
This commit is contained in:
Oliver Gierke
2016-10-25 11:50:07 +02:00
parent c5948e6690
commit e204a0a00e
2 changed files with 120 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-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.
@@ -29,6 +29,7 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.SerializerProvider;
@@ -181,7 +182,18 @@ public class JacksonSerializers extends SimpleModule {
throw new IllegalStateException("Can only translate enum with property information!");
}
return translator.fromText((Class<? extends Enum<?>>) property.getType().getRawClass(), p.getText());
return translator.fromText((Class<? extends Enum<?>>) getActualType(property.getType()).getRawClass(),
p.getText());
}
/**
* Returns the value types for containers or the original type otherwise.
*
* @param type must not be {@literal null}.
* @return
*/
private static JavaType getActualType(JavaType type) {
return type.isContainerType() ? type.getContentType() : type;
}
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Collection;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.rest.webmvc.json.JacksonSerializersUnitTests.Sample.SampleEnum;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Unit tests for {@link JacksonSerializers}.
*
* @author Oliver Gierke
* @soundtrack James Bay - Move Together (Chaos And The Calm)
*/
public class JacksonSerializersUnitTests {
ObjectMapper mapper;
@Before
public void setUp() {
EnumTranslator translator = mock(EnumTranslator.class);
doReturn(SampleEnum.VALUE).when(translator).fromText(SampleEnum.class, "value");
this.mapper = new ObjectMapper();
this.mapper.registerModule(new JacksonSerializers(translator));
}
/**
* @see DATAREST-929
*/
@Test
public void translatesPlainEnumCorrectly() throws Exception {
Sample result = mapper.readValue("{ \"property\" : \"value\"}", Sample.class);
assertThat(result.property, is(SampleEnum.VALUE));
}
/**
* @see DATAREST-929
*/
@Test
public void translatesCollectionOfEnumsCorrectly() throws Exception {
Sample result = mapper.readValue("{ \"collection\" : [ \"value\" ] }", Sample.class);
assertThat(result.collection, hasItem(SampleEnum.VALUE));
}
/**
* @see DATAREST-929
*/
@Test
public void translatesEnumArraysCorrectly() throws Exception {
Sample result = mapper.readValue("{ \"array\" : [ \"value\" ] }", Sample.class);
assertThat(result.array, hasItemInArray(SampleEnum.VALUE));
}
/**
* @see DATAREST-929
*/
@Test
public void translatesMapEnumValueCorrectly() throws Exception {
Sample result = mapper.readValue("{ \"mapToEnum\" : { \"foo\" : \"value\" } }", Sample.class);
assertThat(result.mapToEnum.get("foo"), is(SampleEnum.VALUE));
}
static class Sample {
enum SampleEnum {
VALUE;
}
public SampleEnum property;
public Collection<SampleEnum> collection;
public SampleEnum[] array;
public Map<String, SampleEnum> mapToEnum;
}
}