Skip to content

Commit

Permalink
#1436 - Avoid rendering curies if none is available.
Browse files Browse the repository at this point in the history
If CurieProvider.getCurieInformation(Links) returns an empty list of curies, we now don't render curies at all.
  • Loading branch information
odrotbohm committed Jan 18, 2021
1 parent 894de4a commit 37f2143
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Expand Up @@ -190,9 +190,11 @@ public void serialize(Links value, JsonGenerator jgen, SerializerProvider provid

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();
Expand Down
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -65,6 +66,7 @@
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.
Expand Down Expand Up @@ -622,6 +624,18 @@ void rendersCurieInformationIfCuriedLinkIsGiven() throws Exception {
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;
Expand Down

0 comments on commit 37f2143

Please sign in to comment.