spring-projects / spring-projects/spring-hateoas

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

Open
#2,478 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

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:

https://github.com/spring-projects/spring-hateoas/blob/d6265fb1c360040c049830bb784b3e487a1542a8/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java#L136-L154

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in HateoasConfiguration.java around lines 136–154 and reproduce the Spring Boot test with src/main/resources/rest-message.properties containing foo._prompt=Foo. Check why loadResourceBundleResources(I18N_BASE_NAME, true) is empty, then verify the resulting message source is non-null and resolves foo._prompt from the main resource.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.