Skip to content

Commit

Permalink
#1157 - Tweaks to EntityModel to allow deserialization with Map<Strin…
Browse files Browse the repository at this point in the history
…g, 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.
  • Loading branch information
odrotbohm committed Dec 11, 2019
1 parent 8cf23aa commit aef81d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/main/java/org/springframework/hateoas/EntityModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

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;

/**
Expand All @@ -31,7 +34,7 @@
*/
public class EntityModel<T> extends RepresentationModel<EntityModel<T>> {

private final T content;
private T content;

/**
* Creates an empty {@link EntityModel}.
Expand Down Expand Up @@ -75,6 +78,25 @@ public T getContent() {
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@
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;

Expand Down Expand Up @@ -569,9 +572,36 @@ void rendersMapContentCorrectly() throws Exception {
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 {
Expand Down

0 comments on commit aef81d1

Please sign in to comment.