#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

@@ -17,10 +17,13 @@ package org.springframework.hateoas;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
/**
@@ -31,7 +34,7 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped;
*/
public class EntityModel<T> extends RepresentationModel<EntityModel<T>> {
private final T content;
private T content;
/**
* Creates an empty {@link EntityModel}.
@@ -75,6 +78,25 @@ public class EntityModel<T> extends RepresentationModel<EntityModel<T>> {
return content;
}
// Hacks to allow deserialization into an EntityModel<Map<String, Object>>
@JsonAnySetter
private void setPropertiesAsMap(String key, Object value) {
getOrInitAsMap().put(key, value);
}
@SuppressWarnings("unchecked")
private Map<String, Object> getOrInitAsMap() {
if (this.content == null) {
this.content = (T) new LinkedHashMap<>();
} else {
Assert.state(Map.class.isInstance(this.content), "Content is not a Map!");
}
return (Map<String, Object>) this.content;
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.ResourceSupport#toString()

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 {