Skip to content

java.nio.file.Path support in FileSystemResource (with regular createRelative behavior, superseding PathResource) [SPR-16833] #21373

@spring-projects-issues

Description

@spring-projects-issues

Robert Saenger opened SPR-16833 and commented

org.springframework.core.io.PathResource#createRelative(String) doesn't work correctly if the underlying path refers to a file and not to a directory. The problem of the implementation is, that it doesn't properly distinguish between files an directories. In both cases the implementation returns a Resource whose path is simply extended by the given relative path. This is correct if the path points to a directory but invalid if the path points to a file, e.g. <path-to-file>/<relative path> is always invalid it should be <path-to-file-parent>/<relative-path>.

Suggested fix ist to change the implementation as follows:

/**
 * This implementation creates a PathResource, applying the given path relative
 * to the path of the underlying file of this resource descriptor. It properly
 * distinguishes whether the underlying file of this resource descriptor is a
 * file or directory.
 *
 * @see java.nio.file.Files#isDirectory(Path, java.nio.file.LinkOption...)
 * @see java.nio.file.Path#resolve(String)
 * @see java.nio.file.Path#resolveSibling(String)
 */
@Override
public Resource createRelative(final String relativePath) throws IOException {
      return new PathResource(Files.isDirectory(path) ? path.resolve(relativePath) : path.resolveSibling(relativePath));
}

Since the implementation prevents access to the instance variable path, it is not possible to fix the problem via inheritance and overriding the method. Thus as workaround the entire class may be copied and modified as described above.

 


Affects: 5.0.2

Issue Links:

Referenced from: commits 38f9a7b

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions