Skip to content

Commit

Permalink
#1019 - Avoid exceptions on HAL link title rendering.
Browse files Browse the repository at this point in the history
HalLinkRelation now implements MessageSourceResolvable.getDefaultMessage() returning an empty String to avoid NoSuchMessageExceptions for every resolution not backed by an actual translation. Tweaked serialization configuration for HalLink to not render empty title strings.
  • Loading branch information
odrotbohm committed Jul 15, 2019
1 parent 4c434aa commit ff63800
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Expand Up @@ -174,6 +174,16 @@ public String[] getCodes() {
.toArray(String[]::new);
}

/*
* (non-Javadoc)
* @see org.springframework.context.MessageSourceResolvable#getDefaultMessage()
*/
@Override
@org.springframework.lang.NonNull
public String getDefaultMessage() {
return "";
}

/**
* Simple builder interface to easily create multiple {@link HalLinkRelation}s for a single curie.
*
Expand Down
Expand Up @@ -965,8 +965,8 @@ public Link getLink() {
return link;
}

@JsonInclude(Include.NON_NULL)
@Nullable
@JsonInclude(Include.NON_EMPTY)
public String getTitle() {
return title;
}
Expand Down
Expand Up @@ -495,6 +495,27 @@ void rendersSpecificRelWithSingleLinkAsArrayIfConfigured() throws Exception {
.isEqualTo("{\"_links\":{\"foo\":[{\"href\":\"/some-href\"}]}}");
}

@Test // #1019
void doesNotRenderTitleForEmptyString() throws Exception {

Link link = new Link("/some-href", "foo");

assertThat(mapper.writeValueAsString(new Jackson2HalModule.HalLink(link, ""))) //
.isEqualTo("{\"href\":\"/some-href\"}");
}

@Test // #1019
void resolvesMissingHalLinkRelationToEmptyString() throws Exception {

HalLinkRelation relation = HalLinkRelation.of(LinkRelation.of("someRel"));

MessageSourceAccessor accessor = new MessageSourceAccessor(new StaticMessageSource());

assertThatCode(() -> {
assertThat(accessor.getMessage(relation)).isEqualTo("");
}).doesNotThrowAnyException();
}

private void verifyResolvedTitle(String resourceBundleKey) throws Exception {

LocaleContextHolder.setLocale(Locale.US);
Expand Down

0 comments on commit ff63800

Please sign in to comment.