Skip to content

Commit a268fb0

Browse files
committed
[java][okhttp] Fix defects found in review of the new library
Correctness fixes to the forked templates: - GzipRequestInterceptor: restore the forceContentLength() wrapper dropped from okhttp-gson. Without it the gzip body reports contentLength() == -1, OkHttp falls back to Transfer-Encoding: chunked, and every compressed request fails against servers that reject chunked request bodies. The unused okio.Buffer import was the tell that the removal was accidental. - ProgressResponseBody: restore the memoized bufferedSource field. OkHttp calls source() again on close(), so without caching each call built a fresh ForwardingSource over a partially consumed stream, resetting the progress counter and risking double reads. - ProgressRequestBody: CountingSink no longer reports done, so a known-length body gets exactly one terminal callback instead of two (writeTo still emits the terminal event, which is what unknown-length bodies rely on). Dropped the unreachable private countingSink(Sink) helper. - AWS4Auth: sign x-amz-* headers. They were never added to the signable request, so SigV4 omitted them from SignedHeaders and AWS rejected the signature. Content-Type is deliberately left unsigned because OkHttp's BridgeInterceptor appends the multipart boundary after signing. This also wires up the two-pass auth ordering in ApiClient, which until now had no effect. - JSON: register RFC3339JavaTimeModule for Jackson 2. The codegen already emitted RFC3339InstantDeserializer and RFC3339JavaTimeModule, but nothing installed them, so the space-separated RFC3339 normalization never ran. Every other Jackson library registers it. Jackson 3 does not emit the classes and is left untouched. - additional_properties: replace Java `transient` with @JsonbTransient. The transient keyword was added by this fork and made Java serialization drop all undeclared properties when serializableModel=true; @JsonbTransient keeps JSON-B from emitting the backing map without that side effect. - ApiResponse: lowercase header keys in the caseInsensitiveResponseHeaders branch, matching okhttp-gson, native, jersey2 and jersey3. - pojo: stop emitting five Jackson imports that JavaClientCodegen already contributes via model.imports, which produced duplicate import lines. JsonTypeName is only contributed for Jackson 2, so it is still emitted from the template under useJackson3. Build files: - build.gradle: add the com.google.android:android compileOnly dependency under parcelableModel, mirroring the pom. Generated Parcelable models now compile with a plain Gradle build. - build.sbt: add the swagger-parser-v3 dependency under dynamicOperations and the jakarta.validation-api dependency under useBeanValidation, both of which the pom and Gradle builds already declared; gate jackson-annotations on Jackson 2 so Jackson 3 clients take the version aligned by jackson-databind. All 27 samples regenerated; each compiles and passes mvn test, and JavaClientCodegenTest still passes 288 tests. okhttp-gson output is unchanged.
1 parent b7334df commit a268fb0

504 files changed

Lines changed: 1437 additions & 1199 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/ApiResponse.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ApiResponse<T> {
3939
{{#caseInsensitiveResponseHeaders}}
4040
Map<String, List<String>> responseHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
4141
for(Entry<String, List<String>> entry : headers.entrySet()){
42-
responseHeaders.put(entry.getKey(), entry.getValue());
42+
responseHeaders.put(entry.getKey().toLowerCase(), entry.getValue());
4343
}
4444
{{/caseInsensitiveResponseHeaders}}
4545
this.headers = {{#caseInsensitiveResponseHeaders}}responseHeaders{{/caseInsensitiveResponseHeaders}}{{^caseInsensitiveResponseHeaders}}headers{{/caseInsensitiveResponseHeaders}};

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/GzipRequestInterceptor.mustache‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,37 @@ class GzipRequestInterceptor implements Interceptor {
2626

2727
Request compressedRequest = originalRequest.newBuilder()
2828
.header("Content-Encoding", "gzip")
29-
.method(originalRequest.method(), gzip(body))
29+
.method(originalRequest.method(), forceContentLength(gzip(body)))
3030
.build();
3131
return chain.proceed(compressedRequest);
3232
}
3333

34+
/**
35+
* The gzip body reports an unknown content length, which makes OkHttp fall back to
36+
* "Transfer-Encoding: chunked". Servers that reject chunked request bodies then fail every
37+
* compressed request, so buffer the compressed bytes and republish their known length.
38+
*/
39+
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
40+
final Buffer buffer = new Buffer();
41+
requestBody.writeTo(buffer);
42+
return new RequestBody() {
43+
@Override
44+
public MediaType contentType() {
45+
return requestBody.contentType();
46+
}
47+
48+
@Override
49+
public long contentLength() {
50+
return buffer.size();
51+
}
52+
53+
@Override
54+
public void writeTo(BufferedSink sink) throws IOException {
55+
sink.write(buffer.snapshot());
56+
}
57+
};
58+
}
59+
3460
private RequestBody gzip(final RequestBody body) {
3561
return new RequestBody() {
3662
@Override

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/JSON.mustache‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public class JSON {
191191
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
192192
.defaultDateFormat(new RFC3339DateFormat())
193193
.addModule(new JavaTimeModule())
194+
.addModule(new RFC3339JavaTimeModule())
194195
.build();
195196
{{#joda}}
196197
mapper.registerModule(new JodaModule());

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/ProgressRequestBody.mustache‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ public class ProgressRequestBody extends RequestBody {
4747
callback.onUploadProgress(bytesWritten, contentLength, true);
4848
}
4949

50-
private Sink countingSink(Sink sink) {
51-
return new CountingSink(sink);
52-
}
53-
5450
private final class CountingSink extends ForwardingSink {
5551
5652
private long bytesWritten = 0L;
@@ -68,7 +64,9 @@ public class ProgressRequestBody extends RequestBody {
6864
}
6965

7066
bytesWritten += byteCount;
71-
callback.onUploadProgress(bytesWritten, contentLength, bytesWritten == contentLength);
67+
// writeTo emits the single terminal callback once the body is fully written; reporting
68+
// done here as well would fire it twice for a known-length body.
69+
callback.onUploadProgress(bytesWritten, contentLength, false);
7270
}
7371
}
7472
}

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/ProgressResponseBody.mustache‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ProgressResponseBody extends ResponseBody {
1717
1818
private final ResponseBody responseBody;
1919
private final ApiCallback callback;
20+
private BufferedSource bufferedSource;
2021
2122
public ProgressResponseBody(ResponseBody responseBody, ApiCallback callback) {
2223
this.responseBody = responseBody;
@@ -35,7 +36,12 @@ public class ProgressResponseBody extends ResponseBody {
3536

3637
@Override
3738
public BufferedSource source() {
38-
return Okio.buffer(source(responseBody.source()));
39+
// Memoized: OkHttp calls source() again on close(), and a second ForwardingSource would
40+
// reset the progress counter and re-wrap an already partially consumed stream.
41+
if (bufferedSource == null) {
42+
bufferedSource = Okio.buffer(source(responseBody.source()));
43+
}
44+
return bufferedSource;
3945
}
4046

4147
private Source source(Source source) {

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/additional_properties.mustache‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
{{#isJackson}}
88
@JsonIgnore
99
{{/isJackson}}
10-
private transient Map<String, Object> additionalProperties;
10+
{{#isJsonb}}
11+
@JsonbTransient
12+
{{/isJsonb}}
13+
private Map<String, Object> additionalProperties;
1114

1215
/**
1316
* Set the additional (undeclared) property with the specified name and value.

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/auth/AWS4Auth.mustache‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public class AWS4Auth implements Authentication {
6363
SdkHttpFullRequest.Builder requestBuilder =
6464
SdkHttpFullRequest.builder().uri(uri).method(SdkHttpMethod.fromValue(method));
6565
66+
// SigV4 requires every x-amz-* header that is actually sent to be part of the canonical
67+
// request, otherwise AWS rejects the signature. Content-Type is deliberately not signed:
68+
// OkHttp's BridgeInterceptor rewrites multipart content types to append the boundary, so the
69+
// signed value would not match the value on the wire.
70+
for (Map.Entry<String, String> header : headerParams.entrySet()) {
71+
if (header.getKey().toLowerCase(java.util.Locale.ROOT).startsWith("x-amz-")) {
72+
requestBuilder.putHeader(header.getKey(), header.getValue());
73+
}
74+
}
75+
6676
ContentStreamProvider provider = new ContentStreamProvider() {
6777
@Override
6878
public InputStream newStream() {

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/build.gradle.mustache‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ dependencies {
114114
{{#dynamicOperations}}
115115
implementation 'io.swagger.parser.v3:swagger-parser-v3:2.0.30'
116116
{{/dynamicOperations}}
117+
{{#parcelableModel}}
118+
// Needed for Parcelable support
119+
compileOnly 'com.google.android:android:4.1.1.4'
120+
{{/parcelableModel}}
117121
compileOnly "jakarta.annotation:jakarta.annotation-api:$jakarta_annotation_version"
118122
{{#useBeanValidation}}
119123
compileOnly "jakarta.validation:jakarta.validation-api:$bean_validation_version"

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/build.sbt.mustache‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ lazy val root = (project in file(".")).
2929
{{/isGson}}
3030
{{#isJackson}}
3131
"{{jacksonPackage}}.core" % "jackson-core" % "{{#useJackson3}}3.2.1{{/useJackson3}}{{^useJackson3}}2.22.1{{/useJackson3}}",
32+
{{^useJackson3}}
3233
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.22",
34+
{{/useJackson3}}
3335
"{{jacksonPackage}}.core" % "jackson-databind" % "{{#useJackson3}}3.2.1{{/useJackson3}}{{^useJackson3}}2.22.1{{/useJackson3}}",
3436
{{^useJackson3}}
3537
"com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % "2.22.1",
@@ -56,6 +58,12 @@ lazy val root = (project in file(".")).
5658
{{#withAWSV4Signature}}
5759
"software.amazon.awssdk" % "auth" % "2.20.157",
5860
{{/withAWSV4Signature}}
61+
{{#dynamicOperations}}
62+
"io.swagger.parser.v3" % "swagger-parser-v3" % "2.0.30",
63+
{{/dynamicOperations}}
64+
{{#useBeanValidation}}
65+
"jakarta.validation" % "jakarta.validation-api" % "{{#useJakartaEe}}3.0.2{{/useJakartaEe}}{{^useJakartaEe}}2.0.2{{/useJakartaEe}}",
66+
{{/useBeanValidation}}
5967
"org.junit.jupiter" % "junit-jupiter-api" % "5.10.3" % "test",
6068
"com.novocode" % "junit-interface" % "0.10" % "test"
6169
)

‎modules/openapi-generator/src/main/resources/Java/libraries/okhttp/pojo.mustache‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import com.google.gson.annotations.JsonAdapter;
1616
import com.google.gson.annotations.SerializedName;
1717
{{/isGson}}
1818
{{#isJackson}}
19-
import com.fasterxml.jackson.annotation.JsonCreator;
20-
import com.fasterxml.jackson.annotation.JsonInclude;
21-
import com.fasterxml.jackson.annotation.JsonProperty;
22-
import com.fasterxml.jackson.annotation.JsonValue;
23-
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
19+
{{! JsonCreator, JsonInclude, JsonProperty and JsonValue are already contributed via model.imports
20+
by JavaClientCodegen.postProcessModelProperty; emitting them here too produced duplicate
21+
import lines. JsonTypeName is only contributed there for Jackson 2, so Jackson 3 still needs
22+
it from the template. }}
23+
{{#useJackson3}}
2424
import com.fasterxml.jackson.annotation.JsonTypeName;
25+
{{/useJackson3}}
26+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
2527
{{#isAdditionalPropertiesTrue}}
2628
import com.fasterxml.jackson.annotation.JsonAnyGetter;
2729
import com.fasterxml.jackson.annotation.JsonAnySetter;
@@ -31,6 +33,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
3133
{{#isJsonb}}
3234
import jakarta.json.bind.annotation.JsonbProperty;
3335
import jakarta.json.bind.adapter.JsonbAdapter;
36+
import jakarta.json.bind.annotation.JsonbTransient;
3437
import jakarta.json.bind.annotation.JsonbTypeAdapter;
3538
{{/isJsonb}}
3639
import java.io.IOException;

0 commit comments

Comments
 (0)