Skip to content

[Java][native] Add ability to add header to specific calls #21495

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

Merged
merged 19 commits into from
Jul 27, 2025
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 @@ -56,6 +56,26 @@ import java.util.concurrent.CompletableFuture;
{{>generatedAnnotation}}
{{#operations}}
public class {{classname}} {
/**
* Utility class for extending HttpRequest.Builder functionality.
*/
private static class HttpRequestBuilderExtensions {
/**
* Adds additional headers to the provided HttpRequest.Builder. Useful for adding method/endpoint specific headers.
*
* @param builder the HttpRequest.Builder to which headers will be added
* @param headers a map of header names and values to add; may be null
* @return the same HttpRequest.Builder instance with the additional headers set
*/
static HttpRequest.Builder withAdditionalHeaders(HttpRequest.Builder builder, Map<String, String> headers) {
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}
return builder;
}
}
private final HttpClient memberVarHttpClient;
private final ObjectMapper memberVarObjectMapper;
private final String memberVarBaseUri;
Expand All @@ -77,6 +97,7 @@ public class {{classname}} {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
}

{{#asyncNative}}

private ApiException getApiException(String operationId, HttpResponse<String> response) {
Expand Down Expand Up @@ -128,11 +149,40 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest) throws ApiException {
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}(apiRequest, null);
}

/**
* {{summary}}
* {{notes}}
* @param apiRequest {@link API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request}
* @param headers Optional headers to include in the request
{{#returnType}}
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}{{returnType}}{{#asyncNative}}&gt;{{/asyncNative}}
{{/returnType}}
{{^returnType}}
{{#asyncNative}}
* @return CompletableFuture&lt;Void&gt;
{{/asyncNative}}
{{/returnType}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest, Map<String, String> headers) throws ApiException {
{{#allParams}}
{{>nullable_var_annotations}}
{{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
{{/allParams}}
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
}

/**
Expand All @@ -153,10 +203,32 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest) throws ApiException {
return {{operationId}}WithHttpInfo(apiRequest, null);
}

/**
* {{summary}}
* {{notes}}
* @param apiRequest {@link API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request}
* @param headers Optional headers to include in the request
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}ApiResponse&lt;{{returnType}}{{^returnType}}Void{{/returnType}}&gt;{{#asyncNative}}&gt;{{/asyncNative}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest, Map<String, String> headers) throws ApiException {
{{#allParams}}
{{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
{{/allParams}}
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
}

{{/hasParams}}
Expand Down Expand Up @@ -188,15 +260,46 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}null);
}

/**
* {{summary}}
* {{notes}}
{{#allParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}
{{/allParams}}
* @param headers Optional headers to include in the request
{{#returnType}}
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}{{returnType}}{{#asyncNative}}&gt;{{/asyncNative}}
{{/returnType}}
{{^returnType}}
{{#asyncNative}}
* @return CompletableFuture&lt;Void&gt;
{{/asyncNative}}
{{/returnType}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Map<String, String> headers) throws ApiException {
{{^asyncNative}}
{{#returnType}}ApiResponse<{{{.}}}> localVarResponse = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
{{#returnType}}ApiResponse<{{{.}}}> localVarResponse = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
{{#returnType}}
return localVarResponse.getData();
{{/returnType}}
{{/asyncNative}}
{{#asyncNative}}
try {
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
return memberVarHttpClient.sendAsync(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
Expand Down Expand Up @@ -244,8 +347,32 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}null);
}

/**
* {{summary}}
* {{notes}}
{{#allParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}
{{/allParams}}
* @param headers Optional headers to include in the request
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}ApiResponse&lt;{{returnType}}{{^returnType}}Void{{/returnType}}&gt;{{#asyncNative}}&gt;{{/asyncNative}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Map<String, String> headers) throws ApiException {
{{^asyncNative}}
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
Expand Down Expand Up @@ -327,7 +454,7 @@ public class {{classname}} {
{{/asyncNative}}
{{#asyncNative}}
try {
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
return memberVarHttpClient.sendAsync(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
Expand Down Expand Up @@ -364,7 +491,7 @@ public class {{classname}} {
{{/asyncNative}}
}

private HttpRequest.Builder {{operationId}}RequestBuilder({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
private HttpRequest.Builder {{operationId}}RequestBuilder({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Map<String, String> headers) throws ApiException {
{{#allParams}}
{{#required}}
// verify the required parameter '{{paramName}}' is set
Expand Down Expand Up @@ -569,6 +696,8 @@ public class {{classname}} {
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
// Add custom headers if provided
localVarRequestBuilder = HttpRequestBuilderExtensions.withAdditionalHeaders(localVarRequestBuilder, headers);
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.15.0-SNAPSHOT")
public class AuthApi {
/**
* Utility class for extending HttpRequest.Builder functionality.
*/
private static class HttpRequestBuilderExtensions {
/**
* Adds additional headers to the provided HttpRequest.Builder. Useful for adding method/endpoint specific headers.
*
* @param builder the HttpRequest.Builder to which headers will be added
* @param headers a map of header names and values to add; may be null
* @return the same HttpRequest.Builder instance with the additional headers set
*/
static HttpRequest.Builder withAdditionalHeaders(HttpRequest.Builder builder, Map<String, String> headers) {
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}
return builder;
}
}
private final HttpClient memberVarHttpClient;
private final ObjectMapper memberVarObjectMapper;
private final String memberVarBaseUri;
Expand All @@ -68,6 +88,7 @@ public AuthApi(ApiClient apiClient) {
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
}


protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
String body = response.body() == null ? null : new String(response.body().readAllBytes());
String message = formatExceptionMessage(operationId, response.statusCode(), body);
Expand All @@ -88,7 +109,18 @@ private String formatExceptionMessage(String operationId, int statusCode, String
* @throws ApiException if fails to make API call
*/
public String testAuthHttpBasic() throws ApiException {
ApiResponse<String> localVarResponse = testAuthHttpBasicWithHttpInfo();
return testAuthHttpBasic(null);
}

/**
* To test HTTP basic authentication
* To test HTTP basic authentication
* @param headers Optional headers to include in the request
* @return String
* @throws ApiException if fails to make API call
*/
public String testAuthHttpBasic(Map<String, String> headers) throws ApiException {
ApiResponse<String> localVarResponse = testAuthHttpBasicWithHttpInfo(headers);
return localVarResponse.getData();
}

Expand All @@ -99,7 +131,18 @@ public String testAuthHttpBasic() throws ApiException {
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testAuthHttpBasicWithHttpInfo() throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBasicRequestBuilder();
return testAuthHttpBasicWithHttpInfo(null);
}

/**
* To test HTTP basic authentication
* To test HTTP basic authentication
* @param headers Optional headers to include in the request
* @return ApiResponse&lt;String&gt;
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testAuthHttpBasicWithHttpInfo(Map<String, String> headers) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBasicRequestBuilder(headers);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
Expand Down Expand Up @@ -135,7 +178,7 @@ public ApiResponse<String> testAuthHttpBasicWithHttpInfo() throws ApiException {
}
}

private HttpRequest.Builder testAuthHttpBasicRequestBuilder() throws ApiException {
private HttpRequest.Builder testAuthHttpBasicRequestBuilder(Map<String, String> headers) throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

Expand All @@ -149,6 +192,8 @@ private HttpRequest.Builder testAuthHttpBasicRequestBuilder() throws ApiExceptio
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
// Add custom headers if provided
localVarRequestBuilder = HttpRequestBuilderExtensions.withAdditionalHeaders(localVarRequestBuilder, headers);
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
Expand All @@ -162,7 +207,18 @@ private HttpRequest.Builder testAuthHttpBasicRequestBuilder() throws ApiExceptio
* @throws ApiException if fails to make API call
*/
public String testAuthHttpBearer() throws ApiException {
ApiResponse<String> localVarResponse = testAuthHttpBearerWithHttpInfo();
return testAuthHttpBearer(null);
}

/**
* To test HTTP bearer authentication
* To test HTTP bearer authentication
* @param headers Optional headers to include in the request
* @return String
* @throws ApiException if fails to make API call
*/
public String testAuthHttpBearer(Map<String, String> headers) throws ApiException {
ApiResponse<String> localVarResponse = testAuthHttpBearerWithHttpInfo(headers);
return localVarResponse.getData();
}

Expand All @@ -173,7 +229,18 @@ public String testAuthHttpBearer() throws ApiException {
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testAuthHttpBearerWithHttpInfo() throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBearerRequestBuilder();
return testAuthHttpBearerWithHttpInfo(null);
}

/**
* To test HTTP bearer authentication
* To test HTTP bearer authentication
* @param headers Optional headers to include in the request
* @return ApiResponse&lt;String&gt;
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testAuthHttpBearerWithHttpInfo(Map<String, String> headers) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBearerRequestBuilder(headers);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
Expand Down Expand Up @@ -209,7 +276,7 @@ public ApiResponse<String> testAuthHttpBearerWithHttpInfo() throws ApiException
}
}

private HttpRequest.Builder testAuthHttpBearerRequestBuilder() throws ApiException {
private HttpRequest.Builder testAuthHttpBearerRequestBuilder(Map<String, String> headers) throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

Expand All @@ -223,6 +290,8 @@ private HttpRequest.Builder testAuthHttpBearerRequestBuilder() throws ApiExcepti
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
// Add custom headers if provided
localVarRequestBuilder = HttpRequestBuilderExtensions.withAdditionalHeaders(localVarRequestBuilder, headers);
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
Expand Down
Loading
Loading