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

Injection on Path misinterprets absolute paths #26132

Closed
sebastien-helbert opened this issue Apr 15, 2021 · 2 comments
Closed

Injection on Path misinterprets absolute paths #26132

sebastien-helbert opened this issue Apr 15, 2021 · 2 comments
Labels
status: duplicate A duplicate of another issue

Comments

@sebastien-helbert
Copy link

sebastien-helbert commented Apr 15, 2021

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 15, 2021
@radarsh
Copy link

radarsh commented Apr 15, 2021

Came to say that I am experiencing the same. In my case, I have a Path property defined inside a @ConfigurationProperties bean and we set it using command line using the below syntax:

--<property-name>=/var/folders/t9/ddcqvwls0vb0jz04zjk_ws_80000gq/T/14198791533976608309

But by the time the application reads it, it would have already converted it by prefixing with tomcat-docbase directory to be

/private/var/folders/t9/ddcqvwls0vb0jz04zjk_ws_80000gq/T/tomcat-docbase.8080.14679614108707425001/var/folders/t9/ddcqvwls0vb0jz04zjk_ws_80000gq/T/14198791533976608309

@wilkinsona
Copy link
Member

Thanks for the report. This is a duplicate of #25881 and spring-projects/spring-framework#26702.

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