Skip to content

Provide safe defaults for temp file prefix/suffix when null and add regression test for issue #95. - #118

Merged
elharo merged 1 commit into
apache:masterfrom
LuckysHorizon:fix-95-urllocation-tempfile-defaults
Jul 31, 2026
Merged

Provide safe defaults for temp file prefix/suffix when null and add regression test for issue #95.#118
elharo merged 1 commit into
apache:masterfrom
LuckysHorizon:fix-95-urllocation-tempfile-defaults

Conversation

@LuckysHorizon

Copy link
Copy Markdown
Contributor

Summary

This pull request fixes issue #95 by providing safe default values when
URLLocation creates a temporary file using Files.createTempFile(...).

Previously, passing null as the prefix or suffix resulted in a
NullPointerException.

Changes

  • Use "url" as the default prefix when the provided prefix is null
  • Use ".tmp" as the default suffix when the provided suffix is null
  • Add a regression test covering this behavior

Fixes #95

protected void initFile() throws IOException {
if (unsafeGetFile() == null) {
File tempFile = Files.createTempFile(tempFilePrefix, tempFileSuffix).toFile();
String prefix = tempFilePrefix != null ? tempFilePrefix : DEFAULT_TEMP_FILE_PREFIX;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cleaner to do this in the constructor

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.

Thanks for the review! Agreed on both points — I'll move the null defaulting into the constructor so tempFilePrefix/tempFileSuffix are guaranteed non-null by the time initFile() runs, and inline the "url"/".tmp" literals directly instead of the named constants. Will push an update shortly.

*/
public class URLLocation extends FileLocation {

private static final String DEFAULT_TEMP_FILE_PREFIX = "url";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm tempted to just inline these

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.

Thanks for the review, @elharo — both points are addressed now.

Changes to URLLocation.java:

  • Removed the two static final constants
  • Constructor now applies null-safe defaults inline:
  this.tempFilePrefix = (tempFilePrefix != null) ? tempFilePrefix : "url";
  this.tempFileSuffix = (tempFileSuffix != null) ? tempFileSuffix : ".tmp";
  • initFile() reverted to the original one-liner (fields are now guaranteed non-null)
  • Existing regression test left unchanged — still passes

mvn -q test passed. Commit amended and force-pushed to fix-95-urllocation-tempfile-defaults.

Provide safe defaults for temp file prefix/suffix when null and add regression test for issue apache#95.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@LuckysHorizon
LuckysHorizon force-pushed the fix-95-urllocation-tempfile-defaults branch from ae904bb to 97a000e Compare July 31, 2026 17:28
@elharo
elharo merged commit cc9450f into apache:master Jul 31, 2026
17 checks passed
@github-actions github-actions Bot added this to the 3.1.0 milestone Jul 31, 2026
@github-actions

Copy link
Copy Markdown

@elharo Please assign appropriate label to PR according to the type of change.

@elharo elharo added the bug Something isn't working label Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

URLLocation: NPE from Files.createTempFile(null, null)

2 participants