swagger-api / swagger-api/swagger-codegen

[Kotlin] cannot configure request headers neither per request nor per ApiClient instance

Open
#8,060 0 comments 7 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

I am trying to use generated swagger client for REST API integration test.
During the test I need to perform some steps as admin (e.g. create a new user)
And then to act under restricted user (e.g. perform CRUD operations)
So far I can provide user credentials by modifying static field like this:

ApiClient.defaultHeaders += mapOf("Authorization" to "Bearer $token")

But this approach seems to be incorrect because I cannot run multiple tests in parallel or must only use admin credentials for all requests.

QUESTION:
Is there a way to provide credentials / configure headers per request or per ApiClient Instance?

Swagger-codegen version

2.3.1

Command line used for generation

$ java -jar swagger-codegen-cli.jar generate -i swagger.json -c kotlin-codegen-config.json -l kotlin -o ./jbt-qa-webapp-swagger-client

kotlin-codegen-config.json
{ "packageName": "io.qa", "groupId" : "io.qa", "artifactId" : "webapp-swagger-client", "artifactVersion" : "1.2.31-autogen", "enumPropertyNaming" : "UPPERCASE" }

Suggest a fix/enhancement

Java client seems to have an option to configure ApiClient instance like this

    ApiClient apiClient = new ApiClient();

    apiClient.setUsername("admin");
    apiClient.setPassword("********");
    apiClient.addDefaultHeader("Authorization", "Bearer ########")

    ActionTemplatesControllerApi apiInstance = new ActionTemplatesControllerApi(apiClient);

It would be great to have such option in Kotlin client too.

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

Compare the generated Kotlin ApiClient with the Java ApiClient example, focusing on how credentials and default headers are stored and passed to API instances. Implement per-instance or per-request header configuration without relying on the static defaultHeaders field, then verify that separate clients can use different credentials concurrently.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.