Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/zowe/client/sdk/core/ZosConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ void setBasePath(String basePath) {
}

private String getNormalizedPath(String basePath) {

@frankgiordano frankgiordano Dec 25, 2025 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about change this method as follows:

/**
 * Normalizes a path by collapsing multiple slashes, resolving relative segments (. and ..),  
 * and ensuring a consistent leading slash. The result is standardized to forward slashes   
 * and excludes a trailing slash unless it is the root path.  
 * 
 * @param basePath The raw path string to normalize.
 * @return A canonical Unix-style path string.
 */
private String getNormalizedPath(String basePath) {
    // Paths.get handles leading slashes and collapses internal duplicate slashes
    // normalize() resolves dot-segments like /./ and /../
    final String normalized = Paths.get("/", basePath).normalize().toString().replace("\\", "/");
    
    // Remove trailing slash unless the result is the root "/"
    return (normalized.length() > 1 && normalized.endsWith("/"))
            ? normalized.substring(0, normalized.length() - 1) : normalized;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can replace all the backward slashes with forward slash in the same logic, but I don't think its a good idea to use "Paths" class as it will interpret "file separator" differently based on OS, for windows and Unix it will be different.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a new commit to replace backward slashes to forward slashes

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok thanks.. with your updated version and without using Path how about we change the method to be like this way:

/**
 * Normalizes a path string by standardizing separators, collapsing multiple 
 * consecutive slashes, and ensuring a consistent leading slash.
 * <p>
 * This method performs the following transformations:
 * <ul>
 *   <li>Converts all backslashes (\) to forward slashes (/).</li>
 *   <li>Collapses multiple consecutive forward slashes into a single slash.</li>
 *   <li>Ensures the path begins with a leading slash.</li>
 *   <li>Removes the trailing slash unless the path is the root "/".</li>
 * </ul>
 *
 * @param basePath raw path string to be normalized.
 * @return canonicalize path string, or "/" if the input is null or blank.
 */
private String getNormalizedPath(String basePath) {
    // standardize separators and ensure a leading slash
    String normalizedPath = basePath.replace('\\', '/');
    normalizedPath = normalizedPath.startsWith("/") ? normalizedPath : "/" + normalizedPath;
    
    // collapse two or more consecutive slashes into one
    normalizedPath = normalizedPath.replaceAll("/{2,}", "/");

    // remove trailing slash unless the result is just the root "/"
    if (normalizedPath.length() > 1 && normalizedPath.endsWith("/")) {
        normalizedPath = normalizedPath.substring(0, normalizedPath.length() - 1);
    }

    return normalizedPath;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually lets change the beginning to this to handle spaces too:
// remove all spaces and standardize separators and ensure a leading slash
String path = basePath.replace(" ", "").replace('\', '/');

String normalizedPath = basePath.startsWith("/") ? basePath : "/" + basePath;
String normalizedPath = basePath.replace('\\', '/');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do the following here:
String normalizedPath = basePath.replace(" ", "").replace('', '/');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to say:
String normalizedPath = basePath.replace(" ", "").replace('\', '/');

normalizedPath = normalizedPath.startsWith("/") ? normalizedPath : "/" + normalizedPath;
return normalizedPath.endsWith("/") ?
normalizedPath.substring(0, normalizedPath.length() - 1) : normalizedPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,14 @@ public void tstZosConnectionFactoryBasePathHashCodeMapWithSamePathsSuccess() {
"test", "zosmfPort", "user", "password", "custom/path/");
final ZosConnection zc4 = ZosConnectionFactory.createBasicConnection(
"test", "zosmfPort", "user", "password", "custom/path");
final ZosConnection zc5 = ZosConnectionFactory.createBasicConnection(
"test", "zosmfPort", "user", "password", "\\custom\\path");
final var zcs = new HashMap<ZosConnection, Integer>();
zcs.put(zc1, 1);
zcs.put(zc2, 2);
zcs.put(zc3, 3);
zcs.put(zc4, 4);
zcs.put(zc5, 5);
assertEquals(1, zcs.size());
}

Expand Down