Skip to content

Injection on Path misinterprets absolute paths #26132

Closed
@sebastien-helbert

Description

@sebastien-helbert

Starting from spring-boot 2.4.4 and including spring-boot-starter-web, injection on Path objects misinterprets absolute paths which are transformed into relative paths.

For exemple with this pom file :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.4</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
</project>

And This Component :

@Component
public class PathInjectionIssue {
  @Value("${myconfigfile}")
  private Path configAsPath; // here is the issue
  
  @Value("${myconfigfile}")
  private File configAsFile; /// this one is OK

  @Value("${myconfigfile}")
  private String configFileAsString; // this one is OK this is just a string

  @PostConstruct
  public void check() {
    if (configAsPath.getNameCount() > 1) {
      throw new RuntimeException("This absolute file path [" + configAsPath.toAbsolutePath() + "] is not correct. Expected : " + configFileAsString);
    }
  }
}

This application.properties file :

myconfigfile=/conf.json

The output is :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pathInjectionIssue': Invocation of init method failed; nested exception is java.lang.RuntimeException: This absolute file path [C:\Users\shelbert\AppData\Local\Temp\tomcat-docbase.8080.9485259209731591160\conf.json] is not correct. Expected : /conf.json

[...]

There is no issue with spring-boot 2.4.3 nor without the spring-boot-starter-web dependency

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions