Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.function.Supplier;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -127,8 +126,8 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

protected Map<String, Authentication> authentications;

protected Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
protected Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();
protected ThreadLocal<Integer> lastStatusCode = new ThreadLocal<>();
protected ThreadLocal<Map<String, List<String>>> lastResponseHeaders = new ThreadLocal<>();

protected DateFormat dateFormat;

Expand Down Expand Up @@ -284,7 +283,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
*/
@Deprecated
public int getStatusCode() {
return lastStatusCodeByThread.get(Thread.currentThread().getId());
return lastStatusCode.get();
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

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

ThreadLocal.get() can return null if no value has been set for the current thread. This will cause a NullPointerException when auto-unboxing Integer to int. Consider using ThreadLocal.withInitial() or add null checking.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same issue was always there before. It is an error to call the method before any call is made.

}

/**
Expand All @@ -293,7 +292,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
*/
@Deprecated
public Map<String, List<String>> getResponseHeaders() {
return lastResponseHeadersByThread.get(Thread.currentThread().getId());
return lastResponseHeaders.get();
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

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

ThreadLocal.get() can return null if no value has been set for the current thread. This could cause NullPointerException in calling code that expects a Map. Consider using ThreadLocal.withInitial() to provide a default empty map.

Copilot uses AI. Check for mistakes.
}

/**
Expand Down Expand Up @@ -1015,13 +1014,13 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException {
int statusCode = response.getCode();
lastStatusCodeByThread.put(Thread.currentThread().getId(), statusCode);
lastStatusCode.set(statusCode);
if (statusCode == HttpStatus.SC_NO_CONTENT) {
return null;
}

Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);
lastResponseHeaders.set(responseHeaders);

if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import java.util.Date;
import java.util.function.Supplier;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -104,8 +103,8 @@ public class ApiClient extends JavaTimeFormatter {

protected Map<String, Authentication> authentications;

protected Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
protected Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();
protected ThreadLocal<Integer> lastStatusCode = new ThreadLocal<>();
protected ThreadLocal<Map<String, List<String>>> lastResponseHeaders = new ThreadLocal<>();

protected DateFormat dateFormat;

Expand Down Expand Up @@ -254,7 +253,7 @@ public ApiClient setServerVariables(Map<String, String> serverVariables) {
*/
@Deprecated
public int getStatusCode() {
return lastStatusCodeByThread.get(Thread.currentThread().getId());
return lastStatusCode.get();
}

/**
Expand All @@ -263,7 +262,7 @@ public int getStatusCode() {
*/
@Deprecated
public Map<String, List<String>> getResponseHeaders() {
return lastResponseHeadersByThread.get(Thread.currentThread().getId());
return lastResponseHeaders.get();
}

/**
Expand Down Expand Up @@ -932,13 +931,13 @@ protected Cookie buildCookie(String key, String value, URI uri) {

protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException {
int statusCode = response.getCode();
lastStatusCodeByThread.put(Thread.currentThread().getId(), statusCode);
lastStatusCode.set(statusCode);
if (statusCode == HttpStatus.SC_NO_CONTENT) {
return null;
}

Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);
lastResponseHeaders.set(responseHeaders);

if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import java.util.Date;
import java.util.function.Supplier;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -149,8 +148,8 @@ public class ApiClient extends JavaTimeFormatter {

protected Map<String, Authentication> authentications;

protected Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
protected Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();
protected ThreadLocal<Integer> lastStatusCode = new ThreadLocal<>();
protected ThreadLocal<Map<String, List<String>>> lastResponseHeaders = new ThreadLocal<>();

protected DateFormat dateFormat;

Expand Down Expand Up @@ -302,7 +301,7 @@ public ApiClient setServerVariables(Map<String, String> serverVariables) {
*/
@Deprecated
public int getStatusCode() {
return lastStatusCodeByThread.get(Thread.currentThread().getId());
return lastStatusCode.get();
}

/**
Expand All @@ -311,7 +310,7 @@ public int getStatusCode() {
*/
@Deprecated
public Map<String, List<String>> getResponseHeaders() {
return lastResponseHeadersByThread.get(Thread.currentThread().getId());
return lastResponseHeaders.get();
}

/**
Expand Down Expand Up @@ -1025,13 +1024,13 @@ protected Cookie buildCookie(String key, String value, URI uri) {

protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException {
int statusCode = response.getCode();
lastStatusCodeByThread.put(Thread.currentThread().getId(), statusCode);
lastStatusCode.set(statusCode);
if (statusCode == HttpStatus.SC_NO_CONTENT) {
return null;
}

Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);
lastResponseHeaders.set(responseHeaders);

if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType);
Expand Down
Loading