Skip to content

[Kotlin][Client] Added new option to enable moshi code generator #4781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2019
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 bin/kotlin-client-all.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh

./bin/kotlin-client-gson.sh
./bin/kotlin-client-moshi-codegen.sh
./bin/kotlin-client-nonpublic.sh
./bin/kotlin-client-okhttp3.sh
./bin/kotlin-client-petstore-multiplatform.sh
Expand Down
32 changes: 32 additions & 0 deletions bin/kotlin-client-moshi-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=$(ls -ld "$SCRIPT")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=$(dirname "$SCRIPT")/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=$(dirname "$SCRIPT")/..
APP_DIR=$(cd "${APP_DIR}"; pwd)
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn -B clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin --artifact-id kotlin-petstore-moshi-codegen --additional-properties serializationLibrary=moshi,moshiCodeGen=true -o samples/client/petstore/kotlin-moshi-codegen $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}
1 change: 1 addition & 0 deletions bin/windows/kotlin-client-all.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
call powershell -command "& '%~dp0\kotlin-client-gson.bat'"
call powershell -command "& '%~dp0\kotlin-client-moshi-codegen.bat'"
call powershell -command "& '%~dp0\kotlin-client-nonpublic.bat'"
call powershell -command "& '%~dp0\kotlin-client-okhttp3.bat'"
call powershell -command "& '%~dp0\kotlin-client-petstore-multiplatform.bat'"
Expand Down
10 changes: 10 additions & 0 deletions bin/windows/kotlin-client-moshi-codegen.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
set ags=generate --artifact-id "kotlin-petstore-moshi-codegen" -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g kotlin --additional-properties serializationLibrary=moshi,moshiCodeGen=true -o samples\client\petstore\kotlin-moshi-codegen

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ wrapper {
}

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.3.61'

repositories {
mavenCentral()
Expand All @@ -18,6 +18,9 @@ buildscript {
}

apply plugin: 'kotlin'
{{#moshiCodeGen}}
apply plugin: 'kotlin-kapt'
{{/moshiCodeGen}}

repositories {
mavenCentral()
Expand All @@ -31,8 +34,14 @@ dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
{{#moshi}}
compile "com.squareup.moshi:moshi-kotlin:1.8.0"
compile "com.squareup.moshi:moshi-adapters:1.8.0"
{{^moshiCodeGen}}
compile "com.squareup.moshi:moshi-kotlin:1.9.2"
{{/moshiCodeGen}}
compile "com.squareup.moshi:moshi-adapters:1.9.2"
{{#moshiCodeGen}}
compile "com.squareup.moshi:moshi:1.9.2"
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.9.2"
{{/moshiCodeGen}}
{{/moshi}}
{{#gson}}
compile "com.google.code.gson:gson:2.8.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.google.gson.annotations.SerializedName
{{/gson}}
{{#moshi}}
import com.squareup.moshi.Json
{{#moshiCodeGen}}
import com.squareup.moshi.JsonClass
{{/moshiCodeGen}}
{{/moshi}}
{{#parcelizeModels}}
import android.os.Parcelable
Expand All @@ -27,7 +30,7 @@ import java.io.Serializable
{{#parcelizeModels}}
@Parcelize
{{/parcelizeModels}}
{{#multiplatform}}@Serializable{{/multiplatform}}
{{#multiplatform}}@Serializable{{/multiplatform}}{{#moshi}}{{#moshiCodeGen}}@JsonClass(generateAdapter = true){{/moshiCodeGen}}{{/moshi}}
{{#nonPublicApi}}internal {{/nonPublicApi}}data class {{classname}} (
{{#requiredVars}}
{{>data_class_req_var}}{{^-last}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package {{packageName}}.infrastructure
{{#moshi}}
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
{{^moshiCodeGen}}
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
{{/moshiCodeGen}}
{{/moshi}}
{{#gson}}
import com.google.gson.Gson
Expand All @@ -29,7 +31,9 @@ import java.util.Date
.add(LocalDateAdapter())
.add(UUIDAdapter())
.add(ByteArrayAdapter())
{{^moshiCodeGen}}
.add(KotlinJsonAdapterFactory())
{{/moshiCodeGen}}
.build()
{{/moshi}}
{{#gson}}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/kotlin-gson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ wrapper {
}

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.3.61'

repositories {
mavenCentral()
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 @@
4.2.3-SNAPSHOT
90 changes: 90 additions & 0 deletions samples/client/petstore/kotlin-moshi-codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# org.openapitools.client - Kotlin client library for OpenAPI Petstore

## Requires

* Kotlin 1.3.41
* Gradle 4.9

## 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 name="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 name="documentation-for-models"></a>
## Documentation for Models

- [org.openapitools.client.models.ApiResponse](docs/ApiResponse.md)
- [org.openapitools.client.models.Category](docs/Category.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 name="documentation-for-authorization"></a>
## Documentation for Authorization

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

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

<a name="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

39 changes: 39 additions & 0 deletions samples/client/petstore/kotlin-moshi-codegen/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
group 'org.openapitools'
version '1.0.0'

wrapper {
gradleVersion = '4.9'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}

buildscript {
ext.kotlin_version = '1.3.61'

repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'

repositories {
mavenCentral()
}

test {
useJUnitPlatform()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "com.squareup.moshi:moshi-adapters:1.9.2"
compile "com.squareup.moshi:moshi:1.9.2"
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.9.2"
compile "com.squareup.okhttp3:okhttp:4.2.2"
testCompile "io.kotlintest:kotlintest-runner-junit5:3.1.0"
}
12 changes: 12 additions & 0 deletions samples/client/petstore/kotlin-moshi-codegen/docs/ApiResponse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# ApiResponse

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**code** | **kotlin.Int** | | [optional]
**type** | **kotlin.String** | | [optional]
**message** | **kotlin.String** | | [optional]



11 changes: 11 additions & 0 deletions samples/client/petstore/kotlin-moshi-codegen/docs/Category.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Category

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | **kotlin.Long** | | [optional]
**name** | **kotlin.String** | | [optional]



22 changes: 22 additions & 0 deletions samples/client/petstore/kotlin-moshi-codegen/docs/Order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Order

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | **kotlin.Long** | | [optional]
**petId** | **kotlin.Long** | | [optional]
**quantity** | **kotlin.Int** | | [optional]
**shipDate** | [**java.time.LocalDateTime**](java.time.LocalDateTime.md) | | [optional]
**status** | [**inline**](#StatusEnum) | Order Status | [optional]
**complete** | **kotlin.Boolean** | | [optional]


<a name="StatusEnum"></a>
## Enum: status
Name | Value
---- | -----
status | placed, approved, delivered



22 changes: 22 additions & 0 deletions samples/client/petstore/kotlin-moshi-codegen/docs/Pet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Pet

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | **kotlin.Long** | | [optional]
**category** | [**Category**](Category.md) | | [optional]
**name** | **kotlin.String** | |
**photoUrls** | **kotlin.Array&lt;kotlin.String&gt;** | |
**tags** | [**kotlin.Array&lt;Tag&gt;**](Tag.md) | | [optional]
**status** | [**inline**](#StatusEnum) | pet status in the store | [optional]


<a name="StatusEnum"></a>
## Enum: status
Name | Value
---- | -----
status | available, pending, sold



Loading