Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devtools customization breaks Web MVC resource config #24416

Closed
chrisrhut opened this issue Dec 9, 2020 · 5 comments
Closed

Devtools customization breaks Web MVC resource config #24416

chrisrhut opened this issue Dec 9, 2020 · 5 comments
Labels
status: duplicate A duplicate of another issue

Comments

@chrisrhut
Copy link
Contributor

There are two ways to configure Spring MVC resources - via the spring.web.resources configuration, and the deprecated spring.resources configuration.

With spring-boot 2.4.0, there is a bug where if Devtools is enabled, spring.web.resources is ignored and the deprecated configuration is read instead.

The WebMvcAutoConfiguration ostensibly only chooses the deprecated configuration if it's been customized, and falls back to the WebProperties Resources. However, one of Devtools' customizations (disabling spring.resources.chain.cache) sets the "has been customized" flag and thus switches configurations out from under you.

I noticed this because static resources started 404'ing with Devtools enabled, but worked fine without. Switching to the deprecated configuration fixes the issue, but is clearly an undesirable solution.

This can be observed in the following test case. Note that Devtools turns itself off in tests and this can't be bypassed, but setting the spring.resources.chain.cache=false simulates its modification.

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
  properties = {
    "spring.web.resources.static-locations=file:" + DevToolsWebResourceTest.tmpDir,
    // uncomment the following line to simulate devtools' change and break static resource resolution
    // "spring.resources.chain.cache=false"
  }
)
class DevToolsWebResourceTest {
  static final String tmpDir = "/tmp";
  private static final String content = "Hello, world";
  private static final String resource = "hello" + System.currentTimeMillis() + ".txt";
  private static final Path path = Path.of(tmpDir, resource);

  @BeforeAll
  static void setUp() throws Exception {
    Files.writeString(path, content);
  }

  @AfterAll
  static void tearDown() throws Exception {
    Files.delete(path);
  }

  @LocalServerPort
  private int port;

  @Autowired
  private TestRestTemplate restTemplate;

  @Test
  void staticResource() {
    final String s = restTemplate
        .getForObject("http://localhost:" + port + "/" + resource, String.class);
    assertEquals(content, s);
  }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 9, 2020
@snicoll snicoll added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 9, 2020
@snicoll snicoll added this to the 2.4.1 milestone Dec 9, 2020
@wilkinsona
Copy link
Member

I think this may be a duplicate of #24203.

@snicoll
Copy link
Member

snicoll commented Dec 9, 2020

I thought the same Andy but this test is failing for me with 2.4.1-SNAPSHOT.

@snicoll
Copy link
Member

snicoll commented Dec 9, 2020

Courtesy of @wilkinsona for reminding me. With spring.web.resources.static-locations and spring.web.resources.chain.cache this test passes. You can't mix and match deprecated properties with the new namespace. So it looks like indeed this was fixed via #24203.

@chrisrhut please give your actual project a try with 2.4.1-SNAPSHOT. If that still failing we can reopen but we'd need more details. Thanks!

@snicoll snicoll closed this as completed Dec 9, 2020
@snicoll snicoll added status: duplicate A duplicate of another issue and removed type: regression A regression from a previous release labels Dec 9, 2020
@snicoll snicoll removed this from the 2.4.1 milestone Dec 9, 2020
@chrisrhut
Copy link
Contributor Author

Thank you very much for the quick response, @wilkinsona and @snicoll - I confirmed 2.4.1-SNAPSHOT works in my application 🙏

@snicoll
Copy link
Member

snicoll commented Dec 10, 2020

Thank you very much for checking @chrisrhut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants