#125 - Prevent curies from being rendered if no link is curied.
This commit is contained in:
@@ -128,12 +128,18 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
|
||||
// sort links according to their relation
|
||||
Map<String, List<Object>> sortedLinks = new LinkedHashMap<String, List<Object>>();
|
||||
|
||||
boolean prefixingRequired = curieProvider != null;
|
||||
boolean curiedLinkPresent = false;
|
||||
|
||||
for (Link link : value) {
|
||||
|
||||
String rel = prefixingRequired ? curieProvider.getNamespacedRelFrom(link) : link.getRel();
|
||||
|
||||
if (!link.getRel().equals(rel)) {
|
||||
curiedLinkPresent = true;
|
||||
}
|
||||
|
||||
if (sortedLinks.get(rel) == null) {
|
||||
sortedLinks.put(rel, new ArrayList<Object>());
|
||||
}
|
||||
@@ -141,7 +147,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
sortedLinks.get(rel).add(link);
|
||||
}
|
||||
|
||||
if (prefixingRequired) {
|
||||
if (prefixingRequired && curiedLinkPresent) {
|
||||
Object curieInformation = curieProvider.getCurieInformation();
|
||||
List<Object> curies = new ArrayList<Object>();
|
||||
curies.add(Arrays.asList(curieInformation));
|
||||
|
||||
@@ -61,6 +61,7 @@ public class Jackson2HalIntegrationTest extends AbstractJackson2MarshallingInteg
|
||||
static final Links PAGINATION_LINKS = new Links(new Link("foo", Link.REL_NEXT), new Link("bar", Link.REL_PREVIOUS));
|
||||
|
||||
static final String CURIED_DOCUMENT = "{\"_links\":{\"self\":{\"href\":\"foo\"},\"foo:myrel\":{\"href\":\"bar\"},\"curies\":[{\"href\":\"http://localhost:8080/rels/{rel}\",\"name\":\"foo\",\"templated\":true}]}}";
|
||||
static final String SINGLE_NON_CURIE_LINK = "{\"_links\":{\"self\":{\"href\":\"foo\"}}}";
|
||||
static final String EMPTY_DOCUMENT = "{}";
|
||||
|
||||
@Before
|
||||
@@ -295,6 +296,18 @@ public class Jackson2HalIntegrationTest extends AbstractJackson2MarshallingInteg
|
||||
assertThat(getCuriedObjectMapper().writeValueAsString(resources), is(EMPTY_DOCUMENT));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #125
|
||||
*/
|
||||
@Test
|
||||
public void doesNotRenderCuriesIfNoCurieLinkIsPresent() throws Exception {
|
||||
|
||||
Resources<Object> resources = new Resources<Object>(Collections.emptySet());
|
||||
resources.add(new Link("foo"));
|
||||
|
||||
assertThat(getCuriedObjectMapper().writeValueAsString(resources), is(SINGLE_NON_CURIE_LINK));
|
||||
}
|
||||
|
||||
private static Resources<Resource<SimpleAnnotatedPojo>> setupAnnotatedPagedResources() {
|
||||
|
||||
List<Resource<SimpleAnnotatedPojo>> content = new ArrayList<Resource<SimpleAnnotatedPojo>>();
|
||||
|
||||
Reference in New Issue
Block a user