#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..
This commit is contained in:
Greg Turnquist
2017-11-14 17:39:20 -06:00
parent 367f560745
commit d5649c0de1
2 changed files with 35 additions and 2 deletions

View File

@@ -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.
*

View File

@@ -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);