From 6484e5ecaf0bae2ebd66f1a1790c36c0d63fd41e Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Mon, 14 Jan 2019 15:12:56 +0100 Subject: [PATCH] #784 - Polishing. Tested and fixed NPE with empty pojo. --- .../hateoas/uber/Jackson2UberModule.java | 15 +++++--- .../uber/Jackson2UberIntegrationTest.java | 23 +++++++++++- ...with-resource-objects-and-empty-value.json | 36 +++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/org/springframework/hateoas/uber/resources-with-resource-objects-and-empty-value.json diff --git a/src/main/java/org/springframework/hateoas/uber/Jackson2UberModule.java b/src/main/java/org/springframework/hateoas/uber/Jackson2UberModule.java index 4a4af6d7c..704976b69 100644 --- a/src/main/java/org/springframework/hateoas/uber/Jackson2UberModule.java +++ b/src/main/java/org/springframework/hateoas/uber/Jackson2UberModule.java @@ -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 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 properties = item.getData().stream() - .collect(Collectors.toMap(UberData::getName, UberData::getValue)); + Map 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()); } diff --git a/src/test/java/org/springframework/hateoas/uber/Jackson2UberIntegrationTest.java b/src/test/java/org/springframework/hateoas/uber/Jackson2UberIntegrationTest.java index c4e0842ec..9034c5571 100644 --- a/src/test/java/org/springframework/hateoas/uber/Jackson2UberIntegrationTest.java +++ b/src/test/java/org/springframework/hateoas/uber/Jackson2UberIntegrationTest.java @@ -299,6 +299,28 @@ public void deserializeResources() throws Exception { assertThat(actual).isEqualTo(expected); } + /** + * @see #784 + */ + @Test + public void deserializeEmptyValue() throws Exception { + + List> data = new ArrayList>(); + 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> 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 */ @@ -321,7 +343,6 @@ public void deserializeEmptyResources() throws Exception { mapper.getTypeFactory().constructParametricType(Resource.class, String.class) // ) // )).isInstanceOf(RuntimeException.class); - } /** diff --git a/src/test/resources/org/springframework/hateoas/uber/resources-with-resource-objects-and-empty-value.json b/src/test/resources/org/springframework/hateoas/uber/resources-with-resource-objects-and-empty-value.json new file mode 100644 index 000000000..e1dd7a393 --- /dev/null +++ b/src/test/resources/org/springframework/hateoas/uber/resources-with-resource-objects-and-empty-value.json @@ -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" + } ] + } ] + } ] + } +} \ No newline at end of file