Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undocumented NPE if Stapler.getCurrent() called outside an HTTP handling thread #596

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion core/src/main/java/org/kohsuke/stapler/Stapler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ public ClassLoader getClassLoader() {

/**
* Gets the current {@link StaplerRequest2} that the calling thread is associated with.
* @return null, if called from outside an HTTP handling thread
*/
public static StaplerRequest2 getCurrentRequest2() {
return CURRENT_REQUEST.get();
Expand All @@ -1064,6 +1065,7 @@ public static StaplerRequest getCurrentRequest() {

/**
* Gets the current {@link StaplerResponse2} that the calling thread is associated with.
* @return null, if called from outside an HTTP handling thread
*/
public static StaplerResponse2 getCurrentResponse2() {
return CURRENT_RESPONSE.get();
Expand All @@ -1080,9 +1082,11 @@ public static StaplerResponse getCurrentResponse() {

/**
* Gets the current {@link Stapler} that the calling thread is associated with.
* @return null, if called from outside an HTTP handling thread
*/
public static Stapler getCurrent() {
return CURRENT_REQUEST.get().getStapler();
var req = CURRENT_REQUEST.get();
return req != null ? req.getStapler() : null;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions core/src/test/java/org/kohsuke/stapler/StaplerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ public void testToFileOnUnix() throws Exception {
new URL("file:/tmp/" + path));
}
}

public void testGetCurrent() throws Exception {
assertNull("may be called outside an HTTP handling thread", Stapler.getCurrent());
}
}