swagger-api / swagger-api/swagger-codegen

[kotlin] OkHttpClient is not customizable.

Open
#7,872 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

There is no chance to access response headers because

  1. It is impossible to add interceptors to OkHttpClient
  2. headers on ApiInfrastructureResponse is ignored .
open class ApiClient(val baseUrl: String) {
    companion object {
        @JvmStatic
        val client : OkHttpClient = OkHttpClient() // No chance to customize OkHttpClient
    }
abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
     abstract val statusCode: Int
     abstract val headers: Map<String,List<String>> // Set, but not be used anywhere.
}
// api.mustache
return when (response.responseType) {
    // Only use data property, headers is ignored.
    ResponseType.Success -> {{#returnType}}(response as Success<*>).data as {{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
    ResponseType.Informational -> TODO()
    ResponseType.Redirection -> TODO()
    ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
    ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
    else -> throw kotlin.IllegalStateException("Undefined ResponseType.")
}
Swagger-codegen version

2.3.1

Swagger declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/7602

Suggest a fix/enhancement

I think it would be better to set user customized OkHttpClient.Builder to ApiClient.Companion instead of OkHttpClient. I will try to fix these.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Kotlin ApiClient and ApiInfrastructureResponse examples, then inspect api.mustache where the response is converted to the return value. Trace how OkHttpClient is created and how response headers are represented. Done means a generated client can use a customized OkHttpClient.Builder and response headers are no longer ignored.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.