Skip to content

Commit

Permalink
Undocumented NPE if Stapler.getCurrent() called outside an HTTP han…
Browse files Browse the repository at this point in the history
…dling thread (#596)
  • Loading branch information
jglick authored Nov 4, 2024
1 parent 40d1c29 commit 87c77d1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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());
}
}

0 comments on commit 87c77d1

Please sign in to comment.