#1436 - Avoid rendering curies if none is available.

If CurieProvider.getCurieInformation(Links) returns an empty list of curies, we now don't render curies at all.
This commit is contained in:
Oliver Drotbohm
2021-01-18 17:35:58 +01:00
parent 894de4a245
commit 37f2143681
2 changed files with 18 additions and 2 deletions

View File

@@ -190,9 +190,11 @@ public class Jackson2HalModule extends SimpleModule {
if (!skipCuries && prefixingRequired && curiedLinkPresent) {
List<Object> curies = new ArrayList<>(curieProvider.getCurieInformation(Links.of(links)));
Collection<?> curies = curieProvider.getCurieInformation(Links.of(links));
sortedLinks.put(HalLinkRelation.CURIES, curies);
if (!curies.isEmpty()) {
sortedLinks.put(HalLinkRelation.CURIES, new ArrayList<>(curies));
}
}
TypeFactory typeFactory = provider.getConfig().getTypeFactory();

View File

@@ -18,6 +18,7 @@ package org.springframework.hateoas.mediatype.hal;
import static org.assertj.core.api.Assertions.*;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
@@ -65,6 +66,7 @@ import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
/**
* Integration tests for Jackson 2 HAL integration.
@@ -622,6 +624,18 @@ class Jackson2HalIntegrationTest {
assertThat(document.read("$._links.curies", JSONArray.class)).isNotEmpty();
}
@Test // #1428
void doesNotRenderCuriesIfNoneConfigured() throws Exception {
ObjectMapper mapper = getCuriedObjectMapper(new DefaultCurieProvider(Collections.emptyMap()));
RepresentationModel<?> model = new RepresentationModel<>().add(Link.of("/href", LinkRelation.of("foo:bar")));
DocumentContext document = JsonPath.parse(mapper.writeValueAsString(model));
assertThatExceptionOfType(PathNotFoundException.class)
.isThrownBy(() -> document.read("$.curies", JSONObject.class));
}
@Relation(collectionRelation = "someSample")
static class SomeSample {
@JsonProperty String name;