#1437 - 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 b5ce20a0fb
commit 93cdc2d887
2 changed files with 19 additions and 2 deletions

View File

@@ -203,9 +203,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

@@ -17,6 +17,8 @@ package org.springframework.hateoas.mediatype.hal;
import static org.assertj.core.api.Assertions.*;
import net.minidev.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -63,6 +65,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.
@@ -610,6 +613,18 @@ class Jackson2HalIntegrationTest {
assertThat(result.getContent().name).isEqualTo("Dave");
}
@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;