Skip to content

Commit

Permalink
#784 - Polishing.
Browse files Browse the repository at this point in the history
Tested and fixed NPE with empty pojo.
  • Loading branch information
schauder committed Jan 14, 2019
1 parent f448384 commit 6484e5e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,21 @@ private static Resources<?> extractResources(UberDocument doc, JavaType rootType
} else {

// Primitive type
if (item.getData().size() == 1 && item.getData().get(0).getName() == null) {
List<UberData> itemData = item.getData();
if (itemData != null && itemData.size() == 1 && itemData.get(0).getName() == null) {

Object scalarValue = item.getData().get(0).getValue();
Object scalarValue = itemData.get(0).getValue();
resource = new Resource<>(scalarValue, uberData.getLinks());
} else {

Map<String, Object> properties = item.getData().stream()
.collect(Collectors.toMap(UberData::getName, UberData::getValue));
Map<String, Object> properties;
if (itemData == null) {
properties = new HashMap<>();
} else {
properties = itemData.stream() //
.collect(Collectors.toMap(UberData::getName, UberData::getValue));
}

Object obj = PropertyUtils.createObjectFromProperties(rootType.getRawClass(), properties);
resource = new Resource<>(obj, uberData.getLinks());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,28 @@ public void deserializeResources() throws Exception {
assertThat(actual).isEqualTo(expected);
}

/**
* @see #784
*/
@Test
public void deserializeEmptyValue() throws Exception {

List<Resource<String>> data = new ArrayList<Resource<String>>();
data.add(new Resource<>("", new Link("localhost"), new Link("orders").withRel("orders")));
data.add(new Resource<>("second", new Link("remotehost"), new Link("order").withRel("orders")));

Resources expected = new Resources<>(data);
expected.add(new Link("localhost"));
expected.add(new Link("/page/2").withRel("next"));

Resources<Resource<String>> actual = mapper.readValue(
MappingUtils.read(new ClassPathResource("resources-with-resource-objects-and-empty-value.json", getClass())),
mapper.getTypeFactory().constructParametricType(Resources.class,
mapper.getTypeFactory().constructParametricType(Resource.class, String.class)));

assertThat(actual).isEqualTo(expected);
}

/**
* @see #784
*/
Expand All @@ -321,7 +343,6 @@ public void deserializeEmptyResources() throws Exception {
mapper.getTypeFactory().constructParametricType(Resource.class, String.class) //
) //
)).isInstanceOf(RuntimeException.class);

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"uber" : {
"version" : "1.0",
"data" : [ {
"rel" : [ "self" ],
"url" : "localhost"
}, {
"rel" : [ "next" ],
"url" : "/page/2"
}, {
"data" : [ {
"rel" : [ "self" ],
"url" : "localhost"
}, {
"rel" : [ "orders" ],
"url" : "orders"
}, {
"name" : "string",
"data" : []
} ]
}, {
"data" : [ {
"rel" : [ "self" ],
"url" : "remotehost"
}, {
"rel" : [ "orders" ],
"url" : "order"
}, {
"name" : "string",
"data" : [ {
"value" : "second"
} ]
} ]
} ]
}
}

0 comments on commit 6484e5e

Please sign in to comment.