@@ -111,10 +111,36 @@ A `SerializersModule` is auto-generated when discriminated polymorphic types are
111111| ` application/json ` request body | Supported |
112112| Form data / multipart | Not supported |
113113
114+ ### Security Schemes
115+
116+ The plugin reads security schemes defined in the OpenAPI spec and generates authentication handling automatically.
117+ Only schemes referenced in the top-level ` security ` requirement are included.
118+
119+ Parameter names are derived as ` {schemeName}{specTitle}{Suffix} ` where ` schemeName ` and ` specTitle ` are camel/PascalCased
120+ from the OpenAPI scheme key and ` info.title ` respectively. This scoping prevents collisions when multiple specs define
121+ schemes with the same name.
122+
123+ | Scheme type | Location | Generated constructor parameter(s) |
124+ | -------------| ----------| --------------------------------------------------------------------------------|
125+ | HTTP Bearer | Header | ` {name}{title}Token: () -> String ` |
126+ | HTTP Basic | Header | ` {name}{title}Username: () -> String ` , ` {name}{title}Password: () -> String ` |
127+ | API Key | Header | ` {name}{title}: () -> String ` |
128+ | API Key | Query | ` {name}{title}: () -> String ` |
129+
130+ All auth parameters are ` () -> String ` lambdas, called on every request. This lets you supply providers that refresh
131+ credentials automatically.
132+
133+ Each generated client overrides an ` applyAuth() ` method that applies all credentials to each request:
134+
135+ - Bearer tokens are sent as ` Authorization: Bearer {token} ` headers
136+ - Basic auth is sent as ` Authorization: Basic {base64(username:password)} ` headers
137+ - Header API keys are appended to request headers using the parameter name from the spec
138+ - Query API keys are appended to URL query parameters
139+
114140### Not Supported
115141
116- Callbacks, links, webhooks, XML content types, and OpenAPI vendor extensions (` x-* ` ) are not processed. The plugin logs
117- warnings for callbacks and links found in a spec.
142+ Callbacks, links, webhooks, XML content types, OpenAPI vendor extensions (` x-* ` ), OAuth 2.0, OpenID Connect, and
143+ cookie-based API keys are not processed. The plugin logs warnings for callbacks and links found in a spec.
118144
119145## Generated Code Structure
120146
@@ -221,60 +247,75 @@ Here is how to use them.
221247
222248### Dependencies
223249
224- Add the required runtime dependencies and enable the experimental context parameters compiler flag :
250+ Add the required runtime dependencies:
225251
226252``` kotlin
227- kotlin {
228- compilerOptions {
229- freeCompilerArgs.add(" -Xcontext-parameters" )
230- }
231- }
232-
233253dependencies {
234254 implementation(" io.ktor:ktor-client-core:3.1.1" )
235255 implementation(" io.ktor:ktor-client-cio:3.1.1" ) // or another engine (OkHttp, Apache, etc.)
236256 implementation(" io.ktor:ktor-client-content-negotiation:3.1.1" )
237257 implementation(" io.ktor:ktor-serialization-kotlinx-json:3.1.1" )
238258 implementation(" org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1" )
239- implementation(" io.arrow-kt:arrow-core:2.2.1.1" )
240259}
241260```
242261
243262### Creating the Client
244263
245264Each generated client extends ` ApiClientBase ` and creates its own pre-configured ` HttpClient ` internally.
246- You only need to provide the base URL and authentication credentials.
265+ You only need to provide the base URL and authentication credentials (if the spec defines security schemes) .
247266
248267Class names are derived from OpenAPI tags as ` <Tag>Api ` (e.g., a ` pets ` tag produces ` PetsApi ` ). Untagged endpoints go
249268to ` DefaultApi ` .
250269
270+ ** Bearer token** (spec title "Petstore", scheme name "BearerAuth"):
271+
251272``` kotlin
252273val client = PetsApi (
253274 baseUrl = " https://api.example.com" ,
254- token = { " your-bearer-token" },
275+ bearerAuthPetstoreToken = { " your-bearer-token" },
255276)
256277```
257278
258- The ` token ` parameter is a ` () -> String ` lambda called on every request and sent as a ` Bearer ` token in the
259- ` Authorization ` header. This lets you supply a provider that refreshes automatically:
279+ Auth parameters are ` () -> String ` lambdas called on every request, so you can supply a provider that refreshes
280+ automatically:
260281
261282``` kotlin
262283val client = PetsApi (
263284 baseUrl = " https://api.example.com" ,
264- token = { tokenStore.getAccessToken() },
285+ bearerAuthPetstoreToken = { tokenStore.getAccessToken() },
265286)
266287```
267288
289+ ** Multiple security schemes** -- parameters are scoped by scheme name and spec title:
290+
291+ ``` kotlin
292+ val client = PetsApi (
293+ baseUrl = " https://api.example.com" ,
294+ bearerAuthPetstoreToken = { tokenStore.getAccessToken() },
295+ internalApiKeyPetstore = { secrets.getApiKey() },
296+ )
297+ ```
298+
299+ ** Basic auth** (scheme name "BasicAuth"):
300+
301+ ``` kotlin
302+ val client = PetsApi (
303+ baseUrl = " https://api.example.com" ,
304+ basicAuthPetstoreUsername = { " user" },
305+ basicAuthPetstorePassword = { " pass" },
306+ )
307+ ```
308+
309+ See [ Security Schemes] ( #security-schemes ) for the full mapping of scheme types to constructor parameters.
310+
268311The client implements ` Closeable ` -- call ` client.close() ` when done to release HTTP resources.
269312
270313### Making Requests
271314
272- Every endpoint becomes a ` suspend ` function on the client. Functions use
273- Arrow's [ Raise] ( https://arrow-kt.io/docs/typed-errors/ ) for structured error handling -- they require a
274- ` context(Raise<HttpError>) ` and return ` HttpSuccess<T> ` on success:
315+ Every endpoint becomes a ` suspend ` function on the client that returns ` HttpSuccess<T> ` on success and throws
316+ ` HttpError ` on failure:
275317
276318``` kotlin
277- // Inside a Raise<HttpError> context (e.g., within either { ... })
278319val result: HttpSuccess <List <Pet >> = client.listPets(limit = 10 )
279320println (result.body) // the deserialized response body
280321println (result.code) // the HTTP status code
@@ -288,27 +329,21 @@ val result = client.findPets(status = "available", limit = 20)
288329
289330### Error Handling
290331
291- Generated endpoints use [ Arrow's Raise ] ( https://arrow-kt.io/docs/typed-errors/ ) -- errors are raised, not returned as
292- ` Either ` . Use Arrow's ` either { ... } ` block to obtain an ` Either<HttpError, HttpSuccess<T>> ` :
332+ Generated endpoints throw ` HttpError ` (a ` RuntimeException ` subclass) for non-2xx responses and network failures.
333+ Use standard ` try ` / ` catch ` to handle errors :
293334
294335``` kotlin
295- val result: Either <HttpError , HttpSuccess <Pet >> = either {
296- client.getPet(petId = 123 )
297- }
298-
299- result.fold(
300- ifLeft = { error ->
301- when (error.type) {
302- HttpErrorType .Client -> println (" Client error ${error.code} : ${error.message} " )
303- HttpErrorType .Server -> println (" Server error ${error.code} : ${error.message} " )
304- HttpErrorType .Redirect -> println (" Redirect ${error.code} " )
305- HttpErrorType .Network -> println (" Connection failed: ${error.message} " )
306- }
307- },
308- ifRight = { success ->
309- println (" Found: ${success.body.name} " )
336+ try {
337+ val success = client.getPet(petId = 123 )
338+ println (" Found: ${success.body.name} " )
339+ } catch (e: HttpError ) {
340+ when (e.type) {
341+ HttpErrorType .Client -> println (" Client error ${e.code} : ${e.message} " )
342+ HttpErrorType .Server -> println (" Server error ${e.code} : ${e.message} " )
343+ HttpErrorType .Redirect -> println (" Redirect ${e.code} " )
344+ HttpErrorType .Network -> println (" Connection failed: ${e.message} " )
310345 }
311- )
346+ }
312347```
313348
314349` HttpError ` is a data class with the following fields:
@@ -329,7 +364,7 @@ result.fold(
329364| ` Network ` | I/O failures, timeouts, DNS issues |
330365
331366Network errors (connection timeouts, DNS failures) are caught and reported as
332- ` HttpError(code = 0, ..., type = HttpErrorType.Network) ` instead of propagating exceptions.
367+ ` HttpError(code = 0, ..., type = HttpErrorType.Network) ` instead of propagating raw exceptions.
333368
334369## Publishing
335370
0 commit comments