Skip to content

Commit

Permalink
fix, java: leverage callTimeout instead of readTimeout for RequestOpt…
Browse files Browse the repository at this point in the history
…ions timeout configuration (#3031)

* change java timeout to use callTimeout and remove other default timeouts

* bump version to rc
  • Loading branch information
armandobelardo authored Feb 24, 2024
1 parent 0a60759 commit fb0814d
Show file tree
Hide file tree
Showing 135 changed files with 681 additions and 646 deletions.
9 changes: 9 additions & 0 deletions generators/java/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.8.4] - 2024-02-23
- Improvement: The timeout specified on the RequestOptions object now sets the timeout on the entire call, not just the read timeout of the request.
As a refresher, a timeout can be added per request like so:
```java
RequestOptions ro = RequestOptions.builder().timeout(90).build(); // Creates a timeout of 90 seconds for the request
// You could also specify the timeunit, similar to as if you were using OkHttp directly
// RequestOptions ro = RequestOptions.builder().timeout(2, TimeUnit.MINUTES).build();
client.films.list(ro);
```

## [0.8.3] - 2024-02-23
- Fix: The SDK generator now always creates a valid name for union discriminator wrapper classes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public abstract class GeneratedClientOptions extends AbstractGeneratedJavaFile {

public abstract MethodSpec httpClient();

public abstract MethodSpec httpClientWithTimeout();

public abstract Map<VariableId, MethodSpec> variableGetters();

public abstract ClassName builderClassName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -151,8 +152,29 @@ public GeneratedClientOptions generateFile() {
clientOptionsBuilder.addMethod(headersFromIdempotentRequestOptions.get());
}

MethodSpec httpClientWithTimeoutGetter = MethodSpec.methodBuilder("httpClientWithTimeout")
.addModifiers(Modifier.PUBLIC)
.addParameter(
clientGeneratorContext.getPoetClassNameFactory().getRequestOptionsClassName(),
REQUEST_OPTIONS_PARAMETER_NAME)
.returns(OKHTTP_CLIENT_FIELD.type)
.addStatement(
"return $L.newBuilder().callTimeout($N.getTimeout().get(), $N.getTimeoutTimeUnit())" +
".connectTimeout(0, $T.SECONDS)" +
".writeTimeout(0, $T.SECONDS)" +
".readTimeout(0, $T.SECONDS).build()",
OKHTTP_CLIENT_FIELD.name,
REQUEST_OPTIONS_PARAMETER_NAME,
REQUEST_OPTIONS_PARAMETER_NAME,
TimeUnit.class,
TimeUnit.class,
TimeUnit.class)
.build();


TypeSpec clientOptions = clientOptionsBuilder
.addMethod(httpClientGetter)
.addMethod(httpClientWithTimeoutGetter)
.addMethods(variableGetters.values())
.addMethod(MethodSpec.methodBuilder("builder")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
Expand All @@ -163,11 +185,13 @@ public GeneratedClientOptions generateFile() {
.build();
JavaFile environmentsFile =
JavaFile.builder(className.packageName(), clientOptions).build();

return GeneratedClientOptions.builder()
.className(className)
.javaFile(environmentsFile)
.environment(environmentGetter)
.httpClient(httpClientGetter)
.httpClientWithTimeout(httpClientWithTimeoutGetter)
.builderClassName(builderClassName)
.putAllVariableGetters(variableGetters)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -288,12 +289,12 @@ public final CodeBlock getResponseParserCodeBlock() {
clientOptionsField,
generatedClientOptions.httpClient())
.beginControlFlow("if ($L.getTimeout().isPresent())", REQUEST_OPTIONS_PARAMETER_NAME)
// Set the client's readTimeout if requestOptions overrides it has one
// Set the client's callTimeout if requestOptions overrides it has one
.addStatement(
"$L = $L.newBuilder().readTimeout($N.getTimeout().get(), $N.getTimeoutTimeUnit()).build()",
"$L = $N.$N($L)",
defaultedClientName,
defaultedClientName,
REQUEST_OPTIONS_PARAMETER_NAME,
clientOptionsField,
generatedClientOptions.httpClientWithTimeout(),
REQUEST_OPTIONS_PARAMETER_NAME)
.endControlFlow()
.addStatement(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fb0814d

Please sign in to comment.