From 2dea3f61c67f9397075c7b5ed36624402c9aa657 Mon Sep 17 00:00:00 2001 From: Jeffrey Walraven Date: Fri, 16 Feb 2018 20:51:16 -0500 Subject: [PATCH] Convert link with RFC5988 additional attributes Added full conversion into Jackson2HalModule deserialize --- .../hateoas/hal/Jackson2HalModule.java | 14 +++++++++-- .../hal/Jackson2HalIntegrationTest.java | 25 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java b/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java index 1f7af5a7a..14b771cd2 100644 --- a/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java +++ b/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java @@ -573,11 +573,21 @@ public List deserialize(JsonParser jp, DeserializationContext ctxt) if (JsonToken.START_ARRAY.equals(jp.nextToken())) { while (!JsonToken.END_ARRAY.equals(jp.nextToken())) { link = jp.readValueAs(Link.class); - result.add(new Link(link.getHref(), relation)); + result.add(new Link(link.getHref(), relation) + .withHreflang(link.getHreflang()) + .withMedia(link.getMedia()) + .withTitle(link.getTitle()) + .withType(link.getType()) + .withDeprecation(link.getDeprecation())); } } else { link = jp.readValueAs(Link.class); - result.add(new Link(link.getHref(), relation)); + result.add(new Link(link.getHref(), relation) + .withHreflang(link.getHreflang()) + .withMedia(link.getMedia()) + .withTitle(link.getTitle()) + .withType(link.getType()) + .withDeprecation(link.getDeprecation())); } } diff --git a/src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java b/src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java index b4c9222c9..02dfc5de0 100755 --- a/src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java +++ b/src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java @@ -79,7 +79,7 @@ public class Jackson2HalIntegrationTest extends AbstractJackson2MarshallingInteg static final String LINK_WITH_TITLE = "{\"_links\":{\"ns:foobar\":{\"href\":\"target\",\"title\":\"Foobar's title!\"}}}"; static final String SINGLE_WITH_ONE_EXTRA_ATTRIBUTES = "{\"_links\":{\"self\":{\"href\":\"localhost\",\"title\":\"the title\"}}}"; - static final String SINGLE_WITH_ALL_EXTRA_ATTRIBUTES = "{\"_links\":{\"self\":{\"href\":\"localhost\",\"hreflang\":\"en\",\"title\":\"the title\",\"type\":\"the type\",\"deprecation\":\"/customers/deprecated\"}}}"; + static final String SINGLE_WITH_ALL_EXTRA_ATTRIBUTES = "{\"_links\":{\"self\":{\"href\":\"localhost\",\"hreflang\":\"en\",\"media\":\"the media\",\"title\":\"the title\",\"type\":\"the type\",\"deprecation\":\"/customers/deprecated\"}}}"; @Before public void setUpModule() { @@ -118,6 +118,20 @@ public void rendersAllExtraRFC5988Attributes() throws Exception { assertThat(write(resourceSupport)).isEqualTo(SINGLE_WITH_ALL_EXTRA_ATTRIBUTES); } + @Test + public void deserializeAllExtraRFC5988Attributes() throws Exception { + + ResourceSupport expected = new ResourceSupport(); + expected.add(new Link("localhost", "self") // + .withHreflang("en") // + .withTitle("the title") // + .withType("the type") // + .withMedia("the media") // + .withDeprecation("/customers/deprecated")); + + assertThat(read(SINGLE_WITH_ALL_EXTRA_ATTRIBUTES, ResourceSupport.class)).isEqualTo(expected); + } + @Test public void rendersWithOneExtraRFC5988Attribute() throws Exception { @@ -127,6 +141,15 @@ public void rendersWithOneExtraRFC5988Attribute() throws Exception { assertThat(write(resourceSupport)).isEqualTo(SINGLE_WITH_ONE_EXTRA_ATTRIBUTES); } + @Test + public void deserializeOneExtraRFC5988Attribute() throws Exception { + + ResourceSupport expected = new ResourceSupport(); + expected.add(new Link("localhost", "self").withTitle("the title")); + + assertThat(read(SINGLE_WITH_ONE_EXTRA_ATTRIBUTES, ResourceSupport.class)).isEqualTo(expected); + } + @Test public void deserializeSingleLink() throws Exception { ResourceSupport expected = new ResourceSupport();