Skip to content

In some situations, HateoasConfiguration#lookupMessageSource wrongly returns null instead of ReloadableResourceBundleMessageSource #2478

@reda-alaoui

Description

@reda-alaoui

While trying to define rest-messages.properties files to generate HAL Forms property prompts, I discovered the following - IMHO buggy - behavior.

Steps to reproduce

Given a maven project, suppose you have the following src/main/resources/rest-message.properties

foo._prompt=Foo

The compilation copies rest-message.properties to target/classes/rest-message.properties.

Now you run a Spring Boot test. The context initialization triggers the following method:

private final AbstractMessageSource lookupMessageSource() {
List<Resource> candidates = loadResourceBundleResources(I18N_DEFAULTS_BASE_NAME, false);
if (candidates.isEmpty() && loadResourceBundleResources(I18N_BASE_NAME, true).isEmpty()) {
return null;
}
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setResourceLoader(context);
messageSource.setBasename("classpath:".concat(I18N_BASE_NAME));
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
if (!candidates.isEmpty()) {
messageSource.setCommonMessages(loadProperties(candidates));
}
return messageSource;
}

Actual result

The method returns null because loadResourceBundleResources(I18N_BASE_NAME, true) returns an empty collection. Consequently, the prompts are never rendered in the test context.

Expected result

The method should return a non null AbstractMessageSource to allow prompt rendering in the test context.

Digging

In this case, if we ignore line 138 to 142 and build a ReloadableResourceBundleMessageSource anyway, the latter is able to resolve any message from the main file:

@Test
void test() {
   ReloadableResourceBundleMessageSource messageSource =
        new ReloadableResourceBundleMessageSource();
    messageSource.setResourceLoader(context);
    messageSource.setBasename("classpath:".concat("rest-messages"));
    messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
    String message = messageSource.getMessage("foo._prompt", new Object[0], Locale.getDefault());
    assertThat(message).isEqualTo("Foo");
}

Therefore, I think the preliminary checks (138 to 142) are not "in sync" with the way ReloadableResourceBundleMessageSource actually works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions