#1019 - Avoid exceptions on HAL link title rendering.
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.
This commit is contained in:
@@ -174,6 +174,16 @@ public class HalLinkRelation implements LinkRelation, MessageSourceResolvable {
|
||||
.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.
|
||||
*
|
||||
|
||||
@@ -965,8 +965,8 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
return link;
|
||||
}
|
||||
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
@Nullable
|
||||
@JsonInclude(Include.NON_EMPTY)
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@@ -495,6 +495,27 @@ public class Jackson2HalIntegrationTest extends AbstractJackson2MarshallingInteg
|
||||
.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);
|
||||
|
||||
Reference in New Issue
Block a user