diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JacksonSerializers.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JacksonSerializers.java index 96b857533..4e8557913 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JacksonSerializers.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JacksonSerializers.java @@ -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>) property.getType().getRawClass(), p.getText()); + return translator.fromText((Class>) 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; } } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/JacksonSerializersUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/JacksonSerializersUnitTests.java new file mode 100644 index 000000000..64380c2d3 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/JacksonSerializersUnitTests.java @@ -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 collection; + public SampleEnum[] array; + public Map mapToEnum; + } +}