swagger-api / swagger-codegen-generators

Apache License 2.0
284 stars 421 forks source link

Problems that occur when using --library okhttp4-gson configuration,version:3.0.50 #1206

Open bai-xuan opened 10 months ago

bai-xuan commented 10 months ago

The version I'm using is:3.0.50 image

Set debug mode, httpClient should not add Interceptor through httpClient.interceptors().add()

image

Because httpClient.interceptors() returns views that cannot be modified. The specific situation is as follows:

image image image

. My solution is: image

HugoMario commented 10 months ago

Thanks @bai-xuan for filing this issue. I have a doubt about your solution. Wouldn't that code replace any interceptor added previously?

bai-xuan commented 10 months ago

Thanks @bai-xuan for filing this issue. I have a doubt about your solution. Wouldn't that code replace any interceptor added previously?

An immutable list will definitely not take effect, and he even reported an error

bai-xuan commented 10 months ago

Thanks @bai-xuan for filing this issue. I have a doubt about your solution. Wouldn't that code replace any interceptor added previously?

The code where the problem occurs: httpClient.interceptors().add(loggingInterceptor);

interceptors() The implementation is: @get:JvmName("interceptors") val interceptors: List = builder.interceptors.toImmutableList()

toImmutableList() The implementation is:

/* Returns an immutable copy of this. / fun List.toImmutableList(): List { return Collections.unmodifiableList(toMutableList()) }

bai-xuan commented 10 months ago

It has been explained in detail in unmodifiableList:

image

bai-xuan commented 10 months ago

Let me add one more thing, in okhttp-2.7.5 The implementation of interceptors is like this:

image image

What I'm talking about is using --library okhttp4-gson His okhttp version is 4.10.0: image

This is the implementation in version 4.10.0: image

Specific code: https://github.com/square/okhttp/blob/6b07f6227416506a44bb97012551929cd6a1dd79/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt#L134