Skip to content
Open
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
123 changes: 107 additions & 16 deletions languages/java/templates/ApiClient.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! This template was customized to initialize the ApiClient with the CoreConfiguration to easily setup the KeyFlow Authentication and custom endpoints. }}
{{! Original template: https://github.com/OpenAPITools/openapi-generator/blob/v7.14.0/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache }}
{{! Original template: https://github.com/OpenAPITools/openapi-generator/blob/v7.19.0/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache }}

{{>licenseInfo}}

Expand Down Expand Up @@ -75,10 +75,12 @@ import {{invokerPackage}}.auth.OAuthFlow;
import {{invokerPackage}}.auth.AWS4Auth;
{{/withAWSV4Signature}}

{{! BEGIN - Added imports for core module }}
import cloud.stackit.sdk.core.auth.SetupAuth;
import cloud.stackit.sdk.core.config.CoreConfiguration;
import cloud.stackit.sdk.core.exception.ApiException;
import cloud.stackit.sdk.core.KeyFlowAuthenticator;
{{! END - Added imports for core module }}

/**
* <p>ApiClient class.</p>
Expand Down Expand Up @@ -118,6 +120,10 @@ public class ApiClient {
protected Map<String, String> defaultCookieMap = new HashMap<String, String>();
protected String tempFolderPath = null;

{{! BEGIN - deleted field authentications, we use our own Authenticator
protected Map<String, Authentication> authentications;
}}

protected DateFormat dateFormat;
protected DateFormat datetimeFormat;
protected boolean lenientDatetimeFormat;
Expand All @@ -126,22 +132,25 @@ public class ApiClient {
protected InputStream sslCaCert;
protected boolean verifyingSsl;
protected KeyManager[] keyManagers;
protected String tlsServerName;

protected OkHttpClient httpClient;
protected JSON json;

protected HttpLoggingInterceptor loggingInterceptor;

{{! BEGIN - Added CoreConfiguration field to ApiClient }}
protected CoreConfiguration configuration;
{{! END - Added CoreConfiguration field to ApiClient }}

{{#dynamicOperations}}
protected Map<String, ApiOperation> operationLookupMap = new HashMap<>();

{{/dynamicOperations}}
{{! BEGIN - Removed ApiClient constructor and replaced it with a custom constructors which create the ApiClient with the CoreConfiguration }}
/**
* Basic constructor for ApiClient.
*
/**
* Basic constructor for ApiClient.
*
* Not recommended for production use, use the one with the OkHttpClient parameter instead.
*
* @throws IOException thrown when a file can not be found
Expand All @@ -151,8 +160,8 @@ public class ApiClient {
}

/**
* Basic constructor for ApiClient
*
* Basic constructor for ApiClient
*
* Not recommended for production use, use the one with the OkHttpClient parameter instead.
*
* @param config a {@link cloud.stackit.sdk.core.config.CoreConfiguration} object
Expand Down Expand Up @@ -303,6 +312,9 @@ public class ApiClient {
// Set default User-Agent.
setUserAgent("{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{{artifactVersion}}}/java{{/httpUserAgent}}");

{{! BEGIN - delete line setting authentications
authentications = new HashMap<String, Authentication>();
}}
{{#dynamicOperations}}

OpenAPI openAPI = new OpenAPIV3Parser().read("openapi/openapi.yaml");
Expand All @@ -322,8 +334,8 @@ public class ApiClient {
/**
* Set base path
*
* @param basePath Base path of the URL (e.g {{{basePath}}}
* @return An instance of OkHttpClient
* @param basePath Base path of the URL (e.g {{{basePath}}})
* @return An instance of ApiClient
*/
public ApiClient setBasePath(String basePath) {
this.basePath = basePath;
Expand Down Expand Up @@ -456,6 +468,29 @@ public class ApiClient {
return this;
}

/**
* Get TLS server name for SNI (Server Name Indication).
*
* @return The TLS server name
*/
public String getTlsServerName() {
return tlsServerName;
}

/**
* Set TLS server name for SNI (Server Name Indication).
* This is used to verify the server certificate against a specific hostname
* instead of the hostname in the URL.
*
* @param tlsServerName The TLS server name to use for certificate verification
* @return ApiClient
*/
public ApiClient setTlsServerName(String tlsServerName) {
this.tlsServerName = tlsServerName;
applySslSettings();
return this;
}

/**
* <p>Getter for the field <code>dateFormat</code>.</p>
*
Expand Down Expand Up @@ -533,6 +568,26 @@ public class ApiClient {
JSON.setLenientOnJson(lenientOnJson);
return this;
}
{{! BEGIN - delete authentications getter, we use our own Authenticator
/**
* Get authentications (key: authentication name, value: authentication).
*
* @return Map of authentication objects
*/
public Map<String, Authentication> getAuthentications() {
return authentications;
}

/**
* Get authentication for the given name.
*
* @param authName The authentication name
* @return The authentication, null if not found
*/
public Authentication getAuthentication(String authName) {
return authentications.get(authName);
}
}}

{{#hasHttpBearerMethods}}
/**
Expand All @@ -559,6 +614,15 @@ public class ApiClient {
}
{{/hasHttpBearerMethods}}

{{! BEGIN - delete auth related methods, we use our own Authenticator
deleted methods:
setUsername
setPassword
setApiKey
setApiKeyPrefix
setAccessToken
setAWS4Configuration
}}
/**
* Set the User-Agent header's value (by adding to the default header map).
*
Expand Down Expand Up @@ -791,7 +855,7 @@ public class ApiClient {
* @param value The value of the parameter.
* @return A list of {@code Pair} objects.
*/
public List<Pair> parameterToPairs(String collectionFormat, String name, Collection value) {
public List<Pair> parameterToPairs(String collectionFormat, String name, Collection<?> value) {
List<Pair> params = new ArrayList<Pair>();

// preconditions
Expand Down Expand Up @@ -1060,7 +1124,17 @@ public class ApiClient {
}
try {
if (isJsonMime(contentType)) {
return JSON.deserialize(respBody.byteStream(), returnType);
if (returnType.equals(String.class)) {
String respBodyString = respBody.string();
if (respBodyString.isEmpty()) {
return null;
}
// Use String-based deserialize for String return type with fallback
return JSON.deserialize(respBodyString, returnType);
} else {
// Use InputStream-based deserialize which supports responses > 2GB
return JSON.deserialize(respBody.byteStream(), returnType);
}
} else if (returnType.equals(String.class)) {
String respBodyString = respBody.string();
if (respBodyString.isEmpty()) {
Expand Down Expand Up @@ -1387,6 +1461,10 @@ public class ApiClient {

List<Pair> updatedQueryParams = new ArrayList<>(queryParams);

{{! BEGIN - delete updateParamsForAuth, we use our own Authenticator
// update parameters with authentication settings
updateParamsForAuth(authNames, updatedQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url));
}}
final Request.Builder reqBuilder = new Request.Builder().url(buildUrl(baseUrl, path, updatedQueryParams, collectionQueryParams));
processHeaderParams(headerParams, reqBuilder);
processCookieParams(cookieParams, reqBuilder);
Expand Down Expand Up @@ -1425,7 +1503,8 @@ public class ApiClient {
if (serverIndex != null) {
if (serverIndex < 0 || serverIndex >= servers.size()) {
throw new ArrayIndexOutOfBoundsException(String.format(
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
java.util.Locale.ROOT,
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
));
}
baseURL = servers.get(serverIndex).URL(serverVariables);
Expand Down Expand Up @@ -1497,15 +1576,17 @@ public class ApiClient {
*/
public void processCookieParams(Map<String, String> cookieParams, Request.Builder reqBuilder) {
for (Entry<String, String> param : cookieParams.entrySet()) {
reqBuilder.addHeader("Cookie", String.format("%s=%s", param.getKey(), param.getValue()));
reqBuilder.addHeader("Cookie", String.format(java.util.Locale.ROOT, "%s=%s", param.getKey(), param.getValue()));
}
for (Entry<String, String> param : defaultCookieMap.entrySet()) {
if (!cookieParams.containsKey(param.getKey())) {
reqBuilder.addHeader("Cookie", String.format("%s=%s", param.getKey(), param.getValue()));
reqBuilder.addHeader("Cookie", String.format(java.util.Locale.ROOT, "%s=%s", param.getKey(), param.getValue()));
}
}
}

{{! BEGIN - delete updateParamsForAuth method, we use our own Authenticator }}

/**
* Build a form-encoding request body with the given form parameters.
*
Expand Down Expand Up @@ -1567,10 +1648,10 @@ public class ApiClient {
/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
protected void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down Expand Up @@ -1675,7 +1756,17 @@ public class ApiClient {
trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
if (tlsServerName != null && !tlsServerName.isEmpty()) {
hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Verify the certificate against tlsServerName instead of the actual hostname
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
}
};
} else {
hostnameVerifier = OkHostnameVerifier.INSTANCE;
}
}

SSLContext sslContext = SSLContext.getInstance("TLS");
Expand Down
6 changes: 3 additions & 3 deletions languages/java/templates/api.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! This template was customized to initialize the DefaultApi with the CoreConfiguration to easily setup the KeyFlow Authentication and custom endpoints. }}
{{! Original template: https://github.com/OpenAPITools/openapi-generator/blob/v7.14.0/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api.mustache }}
{{! Original template: https://github.com/OpenAPITools/openapi-generator/blob/v7.19.0/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api.mustache }}

{{>licenseInfo}}

Expand Down Expand Up @@ -478,11 +478,11 @@ class {{classname}} {

public class API{{operationId}}Request {
{{#requiredParams}}
{{>nullable_var_annotations}}
{{>nullable_var_annotations}}{{! prevent indent}}
private final {{{dataType}}} {{paramName}};
{{/requiredParams}}
{{#optionalParams}}
{{>nullable_var_annotations}}
{{>nullable_var_annotations}}{{! prevent indent}}
private {{{dataType}}} {{paramName}};
{{/optionalParams}}

Expand Down
6 changes: 3 additions & 3 deletions languages/java/templates/build.gradle.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! This template was customized to allow the services to be a subprojects in one big gradle project. }}
{{! Original template: https://github.com/OpenAPITools/openapi-generator/blob/v7.14.0/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/build.gradle.mustache }}
{{! Original template: https://github.com/OpenAPITools/openapi-generator/blob/v7.19.0/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/build.gradle.mustache }}

ext {
{{#swagger1AnnotationLibrary}}
Expand Down Expand Up @@ -28,15 +28,15 @@ dependencies {
implementation 'io.gsonfire:gson-fire:1.9.0'
implementation 'jakarta.ws.rs:jakarta.ws.rs-api:2.1.6'
{{#openApiNullable}}
implementation 'org.openapitools:jackson-databind-nullable:0.2.6'
implementation 'org.openapitools:jackson-databind-nullable:0.2.8'
{{/openApiNullable}}
{{#withAWSV4Signature}}
implementation 'software.amazon.awssdk:auth:2.20.157'
{{/withAWSV4Signature}}
{{#hasOAuthMethods}}
implementation group: 'org.apache.oltu.oauth2', name: 'org.apache.oltu.oauth2.client', version: '1.0.2'
{{/hasOAuthMethods}}
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.17.0'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.18.0'
{{#joda}}
implementation 'joda-time:joda-time:2.9.9'
{{/joda}}
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-sdk/generate-sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ python)
java)
# When the GENERATOR_VERSION changes, migrate also the templates in templates/java
# Renovate: datasource=github-tags depName=OpenAPITools/openapi-generator versioning=semver
GENERATOR_VERSION="v7.15.0"
GENERATOR_VERSION="v7.19.0"
;;
*)
echo "SDK language not supported."
Expand Down
Loading