Skip to content
Open
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 @@ -69,6 +69,7 @@ jobs:
- samples/client/petstore/kotlin-bigdecimal-default-okhttp4
- samples/client/petstore/kotlin-jvm-jackson
- samples/client/petstore/kotlin-jvm-ktor-jackson
- samples/client/petstore/kotlin-jvm-ktor-jackson3
Comment thread
denysgaievskyi marked this conversation as resolved.
Resolved
- samples/client/petstore/kotlin-jvm-ktor-gson
- samples/client/petstore/kotlin-jvm-ktor-kotlinx_serialization
- samples/client/petstore/kotlin-jvm-vertx-gson
Expand Down
10 changes: 10 additions & 0 deletions bin/configs/kotlin-jvm-ktor-jackson3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-jvm-ktor-jackson3
library: jvm-ktor
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-petstore-jvm-ktor-jackson3
enumUnknownDefaultCase: true
serializationLibrary: jackson
useJackson3: true
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,9 @@ public void processOpts() {
&& Boolean.parseBoolean(additionalProperties.get("openApiNullable").toString())) {
throw new IllegalArgumentException("openApiNullable cannot be set with useJackson3");
}
if (!JVM_OKHTTP4.equals(getLibrary()) && !JVM_SPRING_RESTCLIENT.equals(getLibrary())) {
throw new IllegalArgumentException("useJackson3 is only supported for the jvm-okhttp4 and jvm-spring-restclient libraries at this time.");
if (!JVM_OKHTTP4.equals(getLibrary()) && !JVM_SPRING_RESTCLIENT.equals(getLibrary())
&& !JVM_KTOR.equals(getLibrary())) {
throw new IllegalArgumentException("useJackson3 is only supported for the jvm-okhttp4, jvm-spring-restclient and jvm-ktor libraries at this time.");
}
if (JVM_SPRING_RESTCLIENT.equals(getLibrary()) && !useSpringBoot4) {
throw new IllegalArgumentException("useJackson3 with jvm-spring-restclient requires useSpringBoot4=true.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,13 @@ dependencies {
implementation "io.ktor:ktor-client-gson:$ktor_version"
{{/gson}}
{{#jackson}}
{{^useJackson3}}
implementation "io.ktor:ktor-client-jackson:$ktor_version"
implementation "io.ktor:ktor-serialization-jackson:$ktor_version"
{{/useJackson3}}
{{#useJackson3}}
implementation "io.ktor:ktor-serialization-jackson3:$ktor_version"
{{/useJackson3}}
{{/jackson}}
{{/jvm-ktor}}
{{#jvm-okhttp4}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import com.google.gson.GsonBuilder
import java.text.DateFormat
{{/gson}}
{{#jackson}}
{{^useJackson3}}
import com.fasterxml.jackson.databind.ObjectMapper
{{/useJackson3}}
{{#useJackson3}}
import tools.jackson.databind.json.JsonMapper
{{/useJackson3}}
{{/jackson}}

{{#operations}}
Expand All @@ -27,7 +32,12 @@ import com.fasterxml.jackson.databind.ObjectMapper
jsonBlock: GsonBuilder.() -> Unit = ApiClient.JSON_DEFAULT,
{{/gson}}
{{#jackson}}
{{^useJackson3}}
jsonBlock: ObjectMapper.() -> Unit = ApiClient.JSON_DEFAULT,
{{/useJackson3}}
{{#useJackson3}}
jsonBlock: JsonMapper.Builder.() -> Unit = ApiClient.JSON_DEFAULT,
{{/useJackson3}}
{{/jackson}}
) : ApiClient(
baseUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,22 @@ import com.google.gson.GsonBuilder
import java.text.DateFormat
{{/gson}}
{{#jackson}}
{{^useJackson3}}
import io.ktor.serialization.jackson.*
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.core.util.DefaultIndenter
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
{{/useJackson3}}
{{#useJackson3}}
import io.ktor.serialization.jackson3.*
import tools.jackson.databind.json.JsonMapper
import tools.jackson.databind.SerializationFeature
import tools.jackson.core.util.DefaultIndenter
import tools.jackson.core.util.DefaultPrettyPrinter
{{/useJackson3}}
{{/jackson}}
import {{packageName}}.auth.*

Expand All @@ -46,7 +55,12 @@ import {{packageName}}.auth.*
jsonBlock: GsonBuilder.() -> Unit = JSON_DEFAULT,
{{/gson}}
{{#jackson}}
{{^useJackson3}}
jsonBlock: ObjectMapper.() -> Unit = JSON_DEFAULT,
{{/useJackson3}}
{{#useJackson3}}
jsonBlock: JsonMapper.Builder.() -> Unit = JSON_DEFAULT,
{{/useJackson3}}
{{/jackson}}
) {

Expand Down Expand Up @@ -90,6 +104,7 @@ import {{packageName}}.auth.*
}
{{/gson}}
{{#jackson}}
{{^useJackson3}}
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val JSON_DEFAULT: ObjectMapper.() -> Unit = {
configure(SerializationFeature.INDENT_OUTPUT, true)
setDefaultPrettyPrinter(DefaultPrettyPrinter().apply {
Expand All @@ -98,6 +113,15 @@ import {{packageName}}.auth.*
})
registerModule(JavaTimeModule())
}
{{/useJackson3}}
{{#useJackson3}}
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val JSON_DEFAULT: JsonMapper.Builder.() -> Unit = {
configure(SerializationFeature.INDENT_OUTPUT, true)
defaultPrettyPrinter(DefaultPrettyPrinter()
.withArrayIndenter(DefaultPrettyPrinter.FixedSpaceIndenter.instance())
.withObjectIndenter(DefaultIndenter(" ", "\n")))
}
{{/useJackson3}}
{{/jackson}}
protected val UNSAFE_HEADERS: List<String> = listOf(HttpHeaders.ContentType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ public void shouldRefuseOpenApiNullableWithJackson3() throws IOException {
() -> generator.opts(configurator.toClientOptInput()).generate());
}

@Test(description = "useJackson3 is only wired up for jvm-okhttp4 and jvm-spring-restclient so far")
@Test(description = "useJackson3 is only wired up for jvm-okhttp4, jvm-spring-restclient and jvm-ktor so far")
public void shouldRefuseJackson3WithUnsupportedLibrary() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();
Expand All @@ -1325,6 +1325,61 @@ public void shouldRefuseJackson3WithUnsupportedLibrary() throws IOException {
() -> generator.opts(configurator.toClientOptInput()).generate());
}

@Test(description = "useJackson3 with jvm-ktor generates a Jackson 3 ApiClient, API classes and Gradle dependencies")
public void shouldGenerateJvmKtorWithJackson3() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("kotlin")
.setLibrary("jvm-ktor")
.setInputSpec("src/test/resources/3_0/petstore.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"))
.addAdditionalProperty(CodegenConstants.SERIALIZATION_LIBRARY, "jackson")
.addAdditionalProperty(KotlinClientCodegen.USE_JACKSON_3, true);

DefaultGenerator generator = new DefaultGenerator();
generator.opts(configurator.toClientOptInput()).generate();

Path apiClientPath = Paths.get(output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt");
TestUtils.assertFileContains(apiClientPath, "import io.ktor.serialization.jackson3.*");
TestUtils.assertFileContains(apiClientPath, "import tools.jackson.databind.json.JsonMapper");
TestUtils.assertFileContains(apiClientPath, "jsonBlock: JsonMapper.Builder.() -> Unit = JSON_DEFAULT");
TestUtils.assertFileNotContains(apiClientPath, "com.fasterxml.jackson");
TestUtils.assertFileNotContains(apiClientPath, "JavaTimeModule");

Path apiPath = Paths.get(output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/client/apis/PetApi.kt");
TestUtils.assertFileContains(apiPath, "jsonBlock: JsonMapper.Builder.() -> Unit = ApiClient.JSON_DEFAULT");
TestUtils.assertFileNotContains(apiPath, "com.fasterxml.jackson.databind");

Path buildGradlePath = Paths.get(output.getAbsolutePath() + "/build.gradle");
TestUtils.assertFileContains(buildGradlePath, "io.ktor:ktor-serialization-jackson3:");
TestUtils.assertFileContains(buildGradlePath, "tools.jackson.module:jackson-module-kotlin");
TestUtils.assertFileNotContains(buildGradlePath, "io.ktor:ktor-serialization-jackson:");
TestUtils.assertFileNotContains(buildGradlePath, "io.ktor:ktor-client-jackson:");
}

@Test(description = "jvm-ktor without useJackson3 keeps the Jackson 2 ApiClient")
public void shouldKeepJackson2ForJvmKtorByDefault() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("kotlin")
.setLibrary("jvm-ktor")
.setInputSpec("src/test/resources/3_0/petstore.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"))
.addAdditionalProperty(CodegenConstants.SERIALIZATION_LIBRARY, "jackson");

DefaultGenerator generator = new DefaultGenerator();
generator.opts(configurator.toClientOptInput()).generate();

Path apiClientPath = Paths.get(output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt");
TestUtils.assertFileContains(apiClientPath, "import io.ktor.serialization.jackson.*");
TestUtils.assertFileContains(apiClientPath, "jsonBlock: ObjectMapper.() -> Unit = JSON_DEFAULT");
TestUtils.assertFileNotContains(apiClientPath, "tools.jackson");
}

@Test(description = "useJackson3 switches the serializer and generated model imports to the tools.jackson package")
public void shouldGenerateJackson3Imports() throws IOException {
File output = Files.createTempDirectory("test").toFile();
Expand Down
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,35 @@
README.md
build.gradle
docs/ApiResponse.md
docs/Category.md
docs/Order.md
docs/Pet.md
docs/PetApi.md
docs/StoreApi.md
docs/Tag.md
docs/User.md
docs/UserApi.md
gradle/wrapper/gradle-wrapper.jar
gradle/wrapper/gradle-wrapper.properties
gradlew
gradlew.bat
settings.gradle
src/main/kotlin/org/openapitools/client/apis/PetApi.kt
src/main/kotlin/org/openapitools/client/apis/StoreApi.kt
src/main/kotlin/org/openapitools/client/apis/UserApi.kt
src/main/kotlin/org/openapitools/client/auth/ApiKeyAuth.kt
src/main/kotlin/org/openapitools/client/auth/Authentication.kt
src/main/kotlin/org/openapitools/client/auth/HttpBasicAuth.kt
src/main/kotlin/org/openapitools/client/auth/HttpBearerAuth.kt
src/main/kotlin/org/openapitools/client/auth/OAuth.kt
src/main/kotlin/org/openapitools/client/infrastructure/ApiAbstractions.kt
src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt
src/main/kotlin/org/openapitools/client/infrastructure/HttpResponse.kt
src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
src/main/kotlin/org/openapitools/client/infrastructure/RequestMethod.kt
src/main/kotlin/org/openapitools/client/models/Category.kt
src/main/kotlin/org/openapitools/client/models/ModelApiResponse.kt
src/main/kotlin/org/openapitools/client/models/Order.kt
src/main/kotlin/org/openapitools/client/models/Pet.kt
src/main/kotlin/org/openapitools/client/models/Tag.kt
src/main/kotlin/org/openapitools/client/models/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.28.0-SNAPSHOT
102 changes: 102 additions & 0 deletions samples/client/petstore/kotlin-jvm-ktor-jackson3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# org.openapitools.client - Kotlin client library for OpenAPI Petstore

This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.

## 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: 1.0.0
- Package version:
- Generator version: 7.28.0-SNAPSHOT
- Build package: org.openapitools.codegen.languages.KotlinClientCodegen

## Requires

* Kotlin 2.4.0
* Gradle 8.14

## Build

First, create the gradle wrapper script:

```
gradle wrapper
```

Then, run:

```
./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.
* Implementation of ApiClient is intended to reduce method counts, specifically to benefit Android targets.

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

All URIs are relative to *http://petstore.swagger.io/v2*

| Class | Method | HTTP request | Description |
| ------------ | ------------- | ------------- | ------------- |
| *PetApi* | [**addPet**](docs/PetApi.md#addpet) | **POST** /pet | Add a new pet to the store |
| *PetApi* | [**deletePet**](docs/PetApi.md#deletepet) | **DELETE** /pet/{petId} | Deletes a pet |
| *PetApi* | [**findPetsByStatus**](docs/PetApi.md#findpetsbystatus) | **GET** /pet/findByStatus | Finds Pets by status |
| *PetApi* | [**findPetsByTags**](docs/PetApi.md#findpetsbytags) | **GET** /pet/findByTags | Finds Pets by tags |
| *PetApi* | [**getPetById**](docs/PetApi.md#getpetbyid) | **GET** /pet/{petId} | Find pet by ID |
| *PetApi* | [**updatePet**](docs/PetApi.md#updatepet) | **PUT** /pet | Update an existing pet |
| *PetApi* | [**updatePetWithForm**](docs/PetApi.md#updatepetwithform) | **POST** /pet/{petId} | Updates a pet in the store with form data |
| *PetApi* | [**uploadFile**](docs/PetApi.md#uploadfile) | **POST** /pet/{petId}/uploadImage | uploads an image |
| *StoreApi* | [**deleteOrder**](docs/StoreApi.md#deleteorder) | **DELETE** /store/order/{orderId} | Delete purchase order by ID |
| *StoreApi* | [**getInventory**](docs/StoreApi.md#getinventory) | **GET** /store/inventory | Returns pet inventories by status |
| *StoreApi* | [**getOrderById**](docs/StoreApi.md#getorderbyid) | **GET** /store/order/{orderId} | Find purchase order by ID |
| *StoreApi* | [**placeOrder**](docs/StoreApi.md#placeorder) | **POST** /store/order | Place an order for a pet |
| *UserApi* | [**createUser**](docs/UserApi.md#createuser) | **POST** /user | Create user |
| *UserApi* | [**createUsersWithArrayInput**](docs/UserApi.md#createuserswitharrayinput) | **POST** /user/createWithArray | Creates list of users with given input array |
| *UserApi* | [**createUsersWithListInput**](docs/UserApi.md#createuserswithlistinput) | **POST** /user/createWithList | Creates list of users with given input array |
| *UserApi* | [**deleteUser**](docs/UserApi.md#deleteuser) | **DELETE** /user/{username} | Delete user |
| *UserApi* | [**getUserByName**](docs/UserApi.md#getuserbyname) | **GET** /user/{username} | Get user by user name |
| *UserApi* | [**loginUser**](docs/UserApi.md#loginuser) | **GET** /user/login | Logs user into the system |
| *UserApi* | [**logoutUser**](docs/UserApi.md#logoutuser) | **GET** /user/logout | Logs out current logged in user session |
| *UserApi* | [**updateUser**](docs/UserApi.md#updateuser) | **PUT** /user/{username} | Updated user |


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

- [org.openapitools.client.models.Category](docs/Category.md)
- [org.openapitools.client.models.ModelApiResponse](docs/ModelApiResponse.md)
- [org.openapitools.client.models.Order](docs/Order.md)
- [org.openapitools.client.models.Pet](docs/Pet.md)
- [org.openapitools.client.models.Tag](docs/Tag.md)
- [org.openapitools.client.models.User](docs/User.md)


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


Authentication schemes defined for the API:
<a id="petstore_auth"></a>
### petstore_auth

- **Type**: OAuth
- **Flow**: implicit
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
- **Scopes**:
- write:pets: modify pets in your account
- read:pets: read your pets

<a id="api_key"></a>
### api_key

- **Type**: API key
- **API key parameter name**: api_key
- **Location**: HTTP header

Loading