Skip to content

Commit

Permalink
Merge pull request #2066 from smainz/fix_dot_in_path
Browse files Browse the repository at this point in the history
Directory with dot not recognized correctly when loading data (breaks migration from 3.x to 4.x)
  • Loading branch information
suryaaki2 committed Dec 16, 2021
2 parents 34287cb + a76ccb7 commit ed0b37f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ public InputStreamList openStreams(String relativeTo, String streamPath) throws
return returnList;
}

/**
* Generates a final path to <code>streamPath</code> relative to <code>relatoveTo</code>.
* If the last part of relativeTo contains a dot character (`.`)
* this part is considered to be a file name, if it does not, it is
* considered to be a directory.
* i.e.<br>
* <pre>
* changelog/some.sql -> some.sql is considered to be a file
* changelog/some_sql -> some_sql is considered to be a directory
* </pre>
*
* @param relativeTo starting point of the path resolution (may be null)
* @param streamPath a path to a resource relative to relativeTo must not be null
* @return a canonicalized absolute path to a resource
*/
protected String getFinalPath(String relativeTo, String streamPath) {
streamPath = streamPath.replace("\\", "/");
streamPath = streamPath.replaceFirst("^classpath\\*?:", "");
Expand All @@ -138,21 +153,25 @@ protected String getFinalPath(String relativeTo, String streamPath) {
relativeTo = relativeTo.replace("\\", "/");
relativeTo = relativeTo.replaceFirst("^classpath\\*?:", "");
relativeTo = relativeTo.replaceAll("//+", "/");
//
// If this is a simple file name then set the
// relativeTo value as if it is a root path
//
if (!relativeTo.contains("/") && relativeTo.contains(".")) {
relativeTo = "/";
}

//
// If this is not a simple file name and the last component
// of the path contains a '.' remove the last component
//
if (!relativeTo.endsWith("/")) {
String lastPortion = relativeTo.replaceFirst(".+/", "");
if (lastPortion.contains(".")) {
relativeTo = relativeTo.replaceFirst("/[^/]+?$", "");
}
}

//
// If this is a simple file name then set the
// relativeTo value as if it is a root path
//
if (!relativeTo.contains("/") && relativeTo.contains(".")) {
relativeTo = "/";
}
streamPath = relativeTo + "/" + streamPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ClassLoaderResourceAccessorTest extends Specification {
"com/example/other.file" | "/my/test.sql" | "com/example/my/test.sql"
"classpath:com/example/other.file" | "my/test.sql" | "com/example/my/test.sql"
"changelog.xml" | "sql/function.sql" | "sql/function.sql"
"db-change.log/changelog.xml" | "data/file.csv" | "db-change.log/data/file.csv"
}

@Unroll("#featureName: #relativeTo #streamPath")
Expand Down

0 comments on commit ed0b37f

Please sign in to comment.