Skip to content

Add Paths#get to forbidden APIs#3289

Merged
dsmiley merged 5 commits intoapache:mainfrom
AndreyBozhko:ban-paths-get
Mar 29, 2025
Merged

Add Paths#get to forbidden APIs#3289
dsmiley merged 5 commits intoapache:mainfrom
AndreyBozhko:ban-paths-get

Conversation

@AndreyBozhko
Copy link
Contributor

Description

Replace Paths#get with Path#of, and add Paths#get to forbidden apis.
Also cleaned up some of the Path -> String -> Path conversions that seemed unnecessary.

Prompted by the comment here: #3146 (comment)

Checklist

Please review the following and check all that apply:

  • I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
  • I have created a Jira issue and added the issue ID to my pull request title.
  • I have given Solr maintainers access to contribute to my PR branch. (optional but recommended, not available for branches on forks living under an organisation)
  • I have developed this patch against the main branch.
  • I have run ./gradlew check.
  • I have added tests for my changes.
  • I have added documentation for the Reference Guide

Copy link
Contributor

@epugh epugh left a comment

Choose a reason for hiding this comment

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

I love that this removes more lines than it adds!

LGTM.

// there.
if (!configSetPath.endsWith("/conf")) {
configSetPath = Paths.get(configSetPath.toString(), "conf");
configSetPath = configSetPath.resolve("conf");
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

Copy link
Contributor

@dsmiley dsmiley left a comment

Choose a reason for hiding this comment

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

Thanks!

This PR is certainly going to conflict with @mlbiscoc 's work in #3263
I think that PR should be merged first. He put a ton of time into that one; it'd be a shame to resolve conflicts coming from this one doing some of the same actual changes!


public static Path getConfigSetsDir(Path solrInstallDir) {
Path configSetsPath = Paths.get("server/solr/configsets/");
Path configSetsPath = Path.of("server/solr/configsets/");
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't need "Path.of" here at all; pass this slashy string to resolve


Path managedSchemaPath =
Paths.get(loader.getConfigPath().toString(), managedSchemaResourceName);
Path managedSchemaPath = loader.getConfigPath().resolve(managedSchemaResourceName);
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

// Ensure that empty files don't become directories (SOLR-11198)

Path emptyFile = Paths.get(tmp.toAbsolutePath().toString(), "conf", "stopwords", "emptyfile");
Path emptyFile = tmp.resolve("conf").resolve("stopwords").resolve("emptyfile").toAbsolutePath();
Copy link
Contributor

Choose a reason for hiding this comment

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

please also drop the toAbsolutePath calls on temp dir resolution, as the path is already absolute. Just touch the lines you modify.

final String standardOutput2 = runtime.getOutput();
assertTrue(standardOutput2.startsWith("Copying from 'zk:/getNode'"));
byte[] fileBytes = Files.readAllBytes(Paths.get(localFile.getAbsolutePath()));
byte[] fileBytes = Files.readAllBytes(Path.of(localFile.getAbsolutePath()));
Copy link
Contributor

Choose a reason for hiding this comment

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

no point in getAbsolutePath

// reconsistutes the path from string so we have to go the string route here as well...
final Path workDir = Paths.get(createTempDir().toAbsolutePath().toString());
// reconstitutes the path from string so we have to go the string route here as well...
final Path workDir = Path.of(createTempDir().toAbsolutePath().toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to go to from Path to String then Path

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that initially and that led to broken tests, so I suppose the warning in the comment above still holds...

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right (and "duh" on me for not reading the comment :shame:).
In the other PR, I like the different approach we're taking of unwrapping the custom Path, which is more plainly apparent and also searchable.

@Test
public void testReloadFromPersistentStorage() throws IOException {
SolrResourceLoader loader = new SolrResourceLoader(Paths.get("./"));
SolrResourceLoader loader = new SolrResourceLoader(Path.of("./"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Any place we use "." or "./", it's the same as the empty string, which I think is more canonical. Maybe a separate PR.

@mlbiscoc
Copy link
Contributor

Cool! Yeah we are going to be stepping on each other toes here... My PR just had a merge conflict which I need to fix. Going to fix it today or tomorrow and hopefully we can merge it soon. Happy to help with merge conflicts on this PR when it arises. Or anything else in here.

@AndreyBozhko
Copy link
Contributor Author

Thanks - I agree that it makes total sense to wait with this PR.

@github-actions github-actions bot removed documentation Improvements or additions to documentation cat:index cat:metrics labels Mar 28, 2025
@dsmiley dsmiley merged commit 470ae76 into apache:main Mar 29, 2025
2 checks passed
colvinco pushed a commit to colvinco/solr that referenced this pull request Apr 4, 2025
* add Paths.get to forbidden apis
* replace Paths.get with Path.of


Co-authored-by: Andrey Bozhko <abozhko@apple.com>
Copy link
Contributor

Choose a reason for hiding this comment

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

The use of Path.of in uploadToZK file is an absolute tragedy. There are lots of nice utility methods in this class that take a Path argument (yay), then convert to a String (boo), then pass to Path.of (nausea), thus assuming it's a file system path. So much for uploading a configSet from a ZIP/JAR.

Not introduced by you of course, but I thought you might care. @AndreyBozhko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants