diff --git a/src/main/java/org/springframework/hateoas/Link.java b/src/main/java/org/springframework/hateoas/Link.java index 8383d844..b48170a8 100755 --- a/src/main/java/org/springframework/hateoas/Link.java +++ b/src/main/java/org/springframework/hateoas/Link.java @@ -207,7 +207,7 @@ public class Link implements Serializable { } /** - * Returns whether the link is templated. + * Returns whether or not the link is templated. * * @return */ @@ -215,6 +215,18 @@ public class Link implements Serializable { return !getUriTemplate().getVariables().isEmpty(); } + /** + * This no-op setter is required to deserialize a link that contains a templated URL. It allows Jackson to + * "set" the property, but since {@code templated} is a virtual property, the injected value {@code true} or + * {@code false) is ignored. + * + * The method is kept private so no one attempts to actually use it. + * + * @param __ - don't care what value is passed in. The true value {@link #isTemplated()} is based upon the {@link UriTemplate}. + */ + private void setTemplated(boolean __) { + } + /** * Turns the current template into a {@link Link} by expanding it using the given parameters. * diff --git a/src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java b/src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java index 8ec8b274..0e82e16a 100755 --- a/src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java +++ b/src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.hateoas.hal; import static org.assertj.core.api.Assertions.*; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -453,6 +454,26 @@ public class Jackson2HalIntegrationTest extends AbstractJackson2MarshallingInteg assertThat(write(resourceSupport)).isEqualTo("{\"_links\":{\"self\":[{\"href\":\"localhost\"}]}}"); } + /** + * @see #667 + */ + @Test + public void handleTemplatedLinksOnDeserialization() throws IOException { + + ResourceSupport original = new ResourceSupport(); + original.add(new Link("/orders{?id}", "order")); + + String serialized = mapper.writeValueAsString(original); + + String expected = "{\"_links\":{\"order\":{\"href\":\"/orders{?id}\",\"templated\":true}}}"; + + assertThat(serialized).isEqualTo(expected); + + ResourceSupport deserialized = mapper.readValue(serialized, ResourceSupport.class); + + assertThat(deserialized).isEqualTo(original); + } + private static void verifyResolvedTitle(String resourceBundleKey) throws Exception { LocaleContextHolder.setLocale(Locale.US);