Skip to content

Commit d5649c0

Browse files
committed
#667 - Fix Link to ignore setting "templated" properties.
Deserializing a Link with a templated URI will cause Jackson to break due to there being no "templated" property. This adds a private, no-op `setTemplated` method to allow deserialization to occur yet not alter the Link..
1 parent 367f560 commit d5649c0

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/main/java/org/springframework/hateoas/Link.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,26 @@ public List<TemplateVariable> getVariables() {
207207
}
208208

209209
/**
210-
* Returns whether the link is templated.
210+
* Returns whether or not the link is templated.
211211
*
212212
* @return
213213
*/
214214
public boolean isTemplated() {
215215
return !getUriTemplate().getVariables().isEmpty();
216216
}
217217

218+
/**
219+
* This no-op setter is required to deserialize a link that contains a templated URL. It allows Jackson to
220+
* "set" the property, but since {@code templated} is a virtual property, the injected value {@code true} or
221+
* {@code false) is ignored.
222+
*
223+
* The method is kept private so no one attempts to actually use it.
224+
*
225+
* @param __ - don't care what value is passed in. The true value {@link #isTemplated()} is based upon the {@link UriTemplate}.
226+
*/
227+
private void setTemplated(boolean __) {
228+
}
229+
218230
/**
219231
* Turns the current template into a {@link Link} by expanding it using the given parameters.
220232
*

src/test/java/org/springframework/hateoas/hal/Jackson2HalIntegrationTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import java.io.IOException;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Collection;
@@ -453,6 +454,26 @@ public void rendersSingleLinkAsArrayWhenConfigured() throws Exception {
453454
assertThat(write(resourceSupport)).isEqualTo("{\"_links\":{\"self\":[{\"href\":\"localhost\"}]}}");
454455
}
455456

457+
/**
458+
* @see #667
459+
*/
460+
@Test
461+
public void handleTemplatedLinksOnDeserialization() throws IOException {
462+
463+
ResourceSupport original = new ResourceSupport();
464+
original.add(new Link("/orders{?id}", "order"));
465+
466+
String serialized = mapper.writeValueAsString(original);
467+
468+
String expected = "{\"_links\":{\"order\":{\"href\":\"/orders{?id}\",\"templated\":true}}}";
469+
470+
assertThat(serialized).isEqualTo(expected);
471+
472+
ResourceSupport deserialized = mapper.readValue(serialized, ResourceSupport.class);
473+
474+
assertThat(deserialized).isEqualTo(original);
475+
}
476+
456477
private static void verifyResolvedTitle(String resourceBundleKey) throws Exception {
457478

458479
LocaleContextHolder.setLocale(Locale.US);

0 commit comments

Comments
 (0)