Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/samples-kotlin-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
- samples/client/petstore/kotlin-default-values-jvm-okhttp4
- samples/client/petstore/kotlin-default-values-jvm-retrofit2
- samples/client/petstore/kotlin-default-values-multiplatform
- samples/client/petstore/kotlin-enum-integers-multiplatform
- samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4
- samples/client/petstore/kotlin-array-simple-string-multiplatform
- samples/client/petstore/kotlin-bigdecimal-default-multiplatform
Expand Down
9 changes: 9 additions & 0 deletions bin/configs/kotlin-enum-integers-multiplatform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-enum-integers-multiplatform
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_21204_enum_integers.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-enum-integers-multiplatform
library: multiplatform
dateLibrary: kotlinx-datetime
sortParamsByRequiredFlag: false
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ import kotlinx.serialization.encoding.Encoder
{{/multiplatform}}
{{#multiplatform}}
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
{{/multiplatform}}

/**
* {{{description}}}
*
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
{{#multiplatform}}@Serializable{{/multiplatform}}{{#kotlinx_serialization}}@Serializable{{#enumUnknownDefaultCase}}(with = {{classname}}Serializer::class){{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}{{^isString}}(with = {{classname}}Serializer::class){{/isString}}{{/enumUnknownDefaultCase}}{{/kotlinx_serialization}}
{{#multiplatform}}@Serializable{{^isString}}(with = {{classname}}Serializer::class){{/isString}}{{/multiplatform}}{{#kotlinx_serialization}}@Serializable{{#enumUnknownDefaultCase}}(with = {{classname}}Serializer::class){{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}{{^isString}}(with = {{classname}}Serializer::class){{/isString}}{{/enumUnknownDefaultCase}}{{/kotlinx_serialization}}
{{^multiplatform}}
{{#moshi}}
@JsonClass(generateAdapter = false)
Expand All @@ -74,7 +77,9 @@ import kotlinx.serialization.*
{{/kotlinx_serialization}}
{{/multiplatform}}
{{#multiplatform}}
{{#isString}}
@SerialName(value = {{#lambda.doublequote}}{{{value}}}{{/lambda.doublequote}})
{{/isString}}
{{/multiplatform}}
{{#isArray}}
{{#isList}}
Expand Down Expand Up @@ -172,3 +177,20 @@ internal object {{classname}}Serializer : KSerializer<{{classname}}> {
}
{{/isString}}{{/enumUnknownDefaultCase}}
{{/kotlinx_serialization}}
{{#multiplatform}}
{{^isString}}
internal object {{classname}}Serializer : KSerializer<{{classname}}> {
override val descriptor = {{dataType}}.serializer().descriptor

override fun deserialize(decoder: Decoder): {{classname}} {
val value = decoder.decodeSerializableValue({{{dataType}}}.serializer())
return {{classname}}.values().firstOrNull { it.value == value }
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Use entries instead of values() in enum deserialization to avoid per-call array allocation.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache, line 187:

<comment>Use `entries` instead of `values()` in enum deserialization to avoid per-call array allocation.</comment>

<file context>
@@ -172,3 +177,20 @@ internal object {{classname}}Serializer : KSerializer<{{classname}}> {
+
+    override fun deserialize(decoder: Decoder): {{classname}} {
+        val value = decoder.decodeSerializableValue({{{dataType}}}.serializer())
+        return {{classname}}.values().firstOrNull { it.value == value }
+            ?: throw IllegalArgumentException("Unknown enum value: $value")
+    }
</file context>
Suggested change
return {{classname}}.values().firstOrNull { it.value == value }
return {{classname}}.entries.firstOrNull { it.value == value }
Fix with Cubic

?: throw IllegalArgumentException("Unknown enum value: $value")
}

override fun serialize(encoder: Encoder, value: {{classname}}) {
encoder.encodeSerializableValue({{{dataType}}}.serializer(), value.value)
}
}
{{/isString}}
{{/multiplatform}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.0.3
info:
title: Demo
version: v1

components:
schemas:
Code:
type: integer
enum:
- 0
- 1
- 2

paths:
/do:
get:
summary: Do something
responses:
200:
description: Successful response
content:
application/json:
schema:
type: object
required:
- code
properties:
code:
$ref: '#/components/schemas/Code'
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
README.md
build.gradle.kts
docs/Code.md
docs/DefaultApi.md
docs/DoGet200Response.md
gradle/wrapper/gradle-wrapper.jar
gradle/wrapper/gradle-wrapper.properties
gradlew
gradlew.bat
settings.gradle.kts
src/commonMain/kotlin/org/openapitools/client/apis/DefaultApi.kt
src/commonMain/kotlin/org/openapitools/client/auth/ApiKeyAuth.kt
src/commonMain/kotlin/org/openapitools/client/auth/Authentication.kt
src/commonMain/kotlin/org/openapitools/client/auth/HttpBasicAuth.kt
src/commonMain/kotlin/org/openapitools/client/auth/HttpBearerAuth.kt
src/commonMain/kotlin/org/openapitools/client/auth/OAuth.kt
src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiAbstractions.kt
src/commonMain/kotlin/org/openapitools/client/infrastructure/ApiClient.kt
src/commonMain/kotlin/org/openapitools/client/infrastructure/Base64ByteArray.kt
src/commonMain/kotlin/org/openapitools/client/infrastructure/HttpResponse.kt
src/commonMain/kotlin/org/openapitools/client/infrastructure/OctetByteArray.kt
src/commonMain/kotlin/org/openapitools/client/infrastructure/PartConfig.kt
src/commonMain/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
src/commonMain/kotlin/org/openapitools/client/infrastructure/RequestMethod.kt
src/commonMain/kotlin/org/openapitools/client/models/Code.kt
src/commonMain/kotlin/org/openapitools/client/models/DoGet200Response.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.21.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# org.openapitools.client - Kotlin client library for Demo

No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

## Overview
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [openapi-spec](https://github.com/OAI/OpenAPI-Specification) from a remote server, you can easily generate an API client.

- API version: v1
- Package version:
- Generator version: 7.21.0-SNAPSHOT
- Build package: org.openapitools.codegen.languages.KotlinClientCodegen

## Requires

* Kotlin 2.2.20

## Build

```
./gradlew check assemble
```

This runs all tests and packages the library.

## Features/Implementation Notes

* Supports JSON inputs/outputs, File inputs, and Form inputs.
* Supports collection formats for query parameters: csv, tsv, ssv, pipes.
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions.


<a id="documentation-for-api-endpoints"></a>
## Documentation for API Endpoints

All URIs are relative to *http://localhost*

| Class | Method | HTTP request | Description |
| ------------ | ------------- | ------------- | ------------- |
| *DefaultApi* | [**doGet**](docs/DefaultApi.md#doget) | **GET** /do | Do something |


<a id="documentation-for-models"></a>
## Documentation for Models

- [org.openapitools.client.models.Code](docs/Code.md)
- [org.openapitools.client.models.DoGet200Response](docs/DoGet200Response.md)


<a id="documentation-for-authorization"></a>
## Documentation for Authorization

Endpoints do not require authorization.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
kotlin("multiplatform") version "2.2.20" // kotlin_version
kotlin("plugin.serialization") version "2.2.20" // kotlin_version
}

group = "org.openapitools"
version = "1.0.0"

val kotlin_version = "2.2.20"
val coroutines_version = "1.10.2"
val serialization_version = "1.9.0"
val ktor_version = "3.2.3"

repositories {
mavenCentral()
}

kotlin {
jvm()
iosX64()
iosArm64()
iosSimulatorArm64()
js {
browser()
nodejs()
}

sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serialization_version")

api("io.ktor:ktor-client-core:$ktor_version")
api("io.ktor:ktor-client-serialization:$ktor_version")
api("io.ktor:ktor-client-content-negotiation:$ktor_version")
api("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")

api("org.jetbrains.kotlinx:kotlinx-datetime:0.7.1")
}
}

commonTest {
dependencies {
implementation(kotlin("test"))
implementation("io.ktor:ktor-client-mock:$ktor_version")
}
}

jvmMain {
dependencies {
implementation(kotlin("stdlib-jdk7"))
implementation("io.ktor:ktor-client-cio-jvm:$ktor_version")
}
}

jvmTest {
dependencies {
implementation(kotlin("test-junit"))
}
}

iosMain {
dependencies {
api("io.ktor:ktor-client-ios:$ktor_version")
}
}

jsMain {
dependencies {
api("io.ktor:ktor-client-js:$ktor_version")
}
}

all {
languageSettings {
optIn("kotlin.time.ExperimentalTime")
}
}
}
}

tasks {
register<Exec>("iosTest") {
val device = project.findProperty("device")?.toString() ?: "iPhone 8"
dependsOn("linkDebugTestIosX64")
group = JavaBasePlugin.VERIFICATION_GROUP
description = "Execute unit tests on ${device} simulator"
val binary = kotlin.targets.getByName<KotlinNativeTarget>("iosX64").binaries.getTest("DEBUG")
commandLine("xcrun", "simctl", "spawn", device, binary.outputFile)
}
register("test") {
dependsOn("allTests")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# Code

## Enum


* `_0` (value: `0`)

* `_1` (value: `1`)

* `_2` (value: `2`)



Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# DefaultApi

All URIs are relative to *http://localhost*

| Method | HTTP request | Description |
| ------------- | ------------- | ------------- |
| [**doGet**](DefaultApi.md#doGet) | **GET** /do | Do something |


<a id="doGet"></a>
# **doGet**
> DoGet200Response doGet()

Do something

### Example
```kotlin
// Import classes:
//import org.openapitools.client.infrastructure.*
//import org.openapitools.client.models.*

val apiInstance = DefaultApi()
try {
val result : DoGet200Response = apiInstance.doGet()
println(result)
} catch (e: ClientException) {
println("4xx response calling DefaultApi#doGet")
e.printStackTrace()
} catch (e: ServerException) {
println("5xx response calling DefaultApi#doGet")
e.printStackTrace()
}
```

### Parameters
This endpoint does not need any parameter.

### Return type

[**DoGet200Response**](DoGet200Response.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: application/json

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# DoGet200Response

## Properties
| Name | Type | Description | Notes |
| ------------ | ------------- | ------------- | ------------- |
| **code** | [**Code**](Code.md) | | |



Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading