#1157 - Tweaks to EntityModel to allow deserialization with Map<String, Object> content.

We now keep internal methods in EntityModel that are configured using Jackson annotations so that it'd populate the content with a Map in case Map<String, Content> is defined as payload type.
This commit is contained in:
Oliver Drotbohm
2019-12-11 18:54:13 +01:00
parent 8cf23aa721
commit aef81d1e57
2 changed files with 54 additions and 2 deletions

View File

@@ -46,10 +46,13 @@ import org.springframework.hateoas.server.core.EmbeddedWrappers;
import org.springframework.hateoas.server.core.Relation;
import org.springframework.lang.Nullable;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
@@ -569,9 +572,36 @@ class Jackson2HalIntegrationTest extends AbstractJackson2MarshallingIntegrationT
assertThat(context.read("$.anotherKey", String.class)).isEqualTo("anotherValue");
}
@Test // #1157
void deserializesEntityModelForMapCorrectly() throws Exception {
TypeFactory typeFactory = mapper.getTypeFactory();
JavaType mapType = typeFactory.constructParametricType(Map.class, String.class, Object.class);
JavaType modelType = typeFactory.constructParametricType(EntityModel.class, mapType);
String source = "{ \"key\" : \"value\" }";
EntityModel<Map<String, Object>> result = mapper.readValue(source, modelType);
assertThat(result.getContent()).containsEntry("key", "value");
}
@Test // #1157
void doesNotSetSuperflousPropertiesAsMapIfSpecialContentTypeIsRequested() throws Exception {
JavaType modelType = mapper.getTypeFactory().constructParametricType(EntityModel.class, SomeSample.class);
String source = "{ \"name\" : \"Dave\", \"instrument\" : \"Guitar\" }";
EntityModel<SomeSample> result = mapper.readValue(source, modelType);
assertThat(result.getContent().name).isEqualTo("Dave");
}
@Relation(collectionRelation = "someSample")
static class SomeSample {
String name;
@JsonProperty String name;
}
private void verifyResolvedTitle(String resourceBundleKey) throws Exception {