Skip to content

MockServletContext getResourcePaths does not resolve directories correctly. [SPR-1715] #6412

@spring-projects-issues

Description

@spring-projects-issues

leo mendoza opened SPR-1715 and commented

The current implementation of getResourcePaths in MockServletContext does not append a trailing slash to the directories it finds. As a result, the ServletContextResourcePatternResolver does not operate correctly (since the recursion is triggered when a trailing slash is found).

Below is a potential patch for the problem. It is not heavily tested, nor is it modified for performance. However, since the MockServletContext is only used for testing, I didn't see performance as too big an issue. If there are any apparent bugs in my implementation, please let me know so I can update my local MockServletContext as well. I apologize for not having this in traditional "patch" format.

// ------ MockServletContext.getResourcePaths ---------
public Set getResourcePaths(String path) {
Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
try {
File file = resource.getFile();
file.listFiles();

		String[] fileList= file.list();
		String prefix = (path.endsWith("/") ? path : path + "/");
		Set resourcePaths = new HashSet(fileList.length);
		for (int i = 0; i < fileList.length; i++) {
			String rscPath = prefix + fileList[i];
			
			File f = new File(file.getPath() + "/" + fileList[i]);
			if( f.isDirectory() )				
				rscPath += "/";
			
			resourcePaths.add(rscPath);
		}
		return resourcePaths;
	}
	catch (IOException ex) {
		logger.info("Couldn't get resource paths for " + resource, ex);
		return null;
	}
}

//--------

Thanks,
leo


No further details from SPR-1715

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions