Skip to content

Commit 3c5fb1d

Browse files
ejithonwing328
authored andcommitted
[kotlin] Add json annotation to each enum value. (#7908)
* [kotlin] Add moshi.Json annotation. * [kotlin] update petstore samples. * [kotlin] Remove extra new lines.
1 parent e2c58fa commit 3c5fb1d

File tree

16 files changed

+50
-39
lines changed

16 files changed

+50
-39
lines changed

modules/swagger-codegen/src/main/resources/kotlin-client/data_class.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
{{#hasEnums}}
2+
import com.squareup.moshi.Json
3+
{{/hasEnums}}
14
/**
25
* {{{description}}}
36
{{#vars}}
@@ -18,7 +21,7 @@ data class {{classname}} (
1821
*/
1922
enum class {{nameInCamelCase}}(val value: {{datatype}}){
2023
{{#allowableValues}}{{#enumVars}}
21-
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
24+
@Json(name = {{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
2225
{{/enumVars}}{{/allowableValues}}
2326
}
2427
{{/isEnum}}{{/vars}}{{/hasEnums}}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import com.squareup.moshi.Json
2+
13
/**
24
* {{{description}}}
35
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
46
*/
57
enum class {{classname}}(val value: {{dataType}}){
68
{{#allowableValues}}{{#enumVars}}
7-
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
9+
@Json(name = {{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
810
{{/enumVars}}{{/allowableValues}}
911
}

samples/client/petstore/kotlin-string/docs/UserApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Get user by user name
213213
//import io.swagger.client.models.*
214214

215215
val apiInstance = UserApi()
216-
val username : kotlin.String = username_example // kotlin.String | The name that needs to be fetched. Use user1 for testing.
216+
val username : kotlin.String = username_example // kotlin.String | The name that needs to be fetched. Use user1 for testing.
217217
try {
218218
val result : User = apiInstance.getUserByName(username)
219219
println(result)
@@ -230,7 +230,7 @@ try {
230230

231231
Name | Type | Description | Notes
232232
------------- | ------------- | ------------- | -------------
233-
**username** | **kotlin.String**| The name that needs to be fetched. Use user1 for testing. |
233+
**username** | **kotlin.String**| The name that needs to be fetched. Use user1 for testing. |
234234

235235
### Return type
236236

samples/client/petstore/kotlin-string/src/main/kotlin/io/swagger/client/apis/UserApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
144144
/**
145145
* Get user by user name
146146
*
147-
* @param username The name that needs to be fetched. Use user1 for testing.
147+
* @param username The name that needs to be fetched. Use user1 for testing.
148148
* @return User
149149
*/
150150
@Suppress("UNCHECKED_CAST")

samples/client/petstore/kotlin-string/src/main/kotlin/io/swagger/client/infrastructure/ApiClient.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ open class ApiClient(val baseUrl: String) {
8585
RequestMethod.DELETE -> Request.Builder().url(url).delete()
8686
RequestMethod.GET -> Request.Builder().url(url)
8787
RequestMethod.HEAD -> Request.Builder().url(url).head()
88-
RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(body!!, contentType))
89-
RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(body!!, contentType))
90-
RequestMethod.POST -> Request.Builder().url(url).post(requestBody(body!!, contentType))
88+
RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(body, contentType))
89+
RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(body, contentType))
90+
RequestMethod.POST -> Request.Builder().url(url).post(requestBody(body, contentType))
9191
RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
9292
}
9393

samples/client/petstore/kotlin-string/src/main/kotlin/io/swagger/client/models/Order.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package io.swagger.client.models
1313

1414

15+
import com.squareup.moshi.Json
1516
/**
1617
* An order for a pets from the pet store
1718
* @param id
@@ -35,13 +36,13 @@ data class Order (
3536
* Order Status
3637
* Values: placed,approved,delivered
3738
*/
38-
enum class Status(val value: kotlin.Any){
39+
enum class Status(val value: kotlin.String){
3940

40-
placed("placed"),
41+
@Json(name = "placed") placed("placed"),
4142

42-
approved("approved"),
43+
@Json(name = "approved") approved("approved"),
4344

44-
delivered("delivered");
45+
@Json(name = "delivered") delivered("delivered");
4546

4647
}
4748

samples/client/petstore/kotlin-string/src/main/kotlin/io/swagger/client/models/Pet.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package io.swagger.client.models
1414
import io.swagger.client.models.Category
1515
import io.swagger.client.models.Tag
1616

17+
import com.squareup.moshi.Json
1718
/**
1819
* A pet for sale in the pet store
1920
* @param id
@@ -37,13 +38,13 @@ data class Pet (
3738
* pet status in the store
3839
* Values: available,pending,sold
3940
*/
40-
enum class Status(val value: kotlin.Any){
41+
enum class Status(val value: kotlin.String){
4142

42-
available("available"),
43+
@Json(name = "available") available("available"),
4344

44-
pending("pending"),
45+
@Json(name = "pending") pending("pending"),
4546

46-
sold("sold");
47+
@Json(name = "sold") sold("sold");
4748

4849
}
4950

samples/client/petstore/kotlin-threetenbp/docs/UserApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Get user by user name
213213
//import io.swagger.client.models.*
214214

215215
val apiInstance = UserApi()
216-
val username : kotlin.String = username_example // kotlin.String | The name that needs to be fetched. Use user1 for testing.
216+
val username : kotlin.String = username_example // kotlin.String | The name that needs to be fetched. Use user1 for testing.
217217
try {
218218
val result : User = apiInstance.getUserByName(username)
219219
println(result)
@@ -230,7 +230,7 @@ try {
230230

231231
Name | Type | Description | Notes
232232
------------- | ------------- | ------------- | -------------
233-
**username** | **kotlin.String**| The name that needs to be fetched. Use user1 for testing. |
233+
**username** | **kotlin.String**| The name that needs to be fetched. Use user1 for testing. |
234234

235235
### Return type
236236

samples/client/petstore/kotlin-threetenbp/src/main/kotlin/io/swagger/client/apis/UserApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
145145
/**
146146
* Get user by user name
147147
*
148-
* @param username The name that needs to be fetched. Use user1 for testing.
148+
* @param username The name that needs to be fetched. Use user1 for testing.
149149
* @return User
150150
*/
151151
@Suppress("UNCHECKED_CAST")

samples/client/petstore/kotlin-threetenbp/src/main/kotlin/io/swagger/client/infrastructure/ApiClient.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ open class ApiClient(val baseUrl: String) {
8585
RequestMethod.DELETE -> Request.Builder().url(url).delete()
8686
RequestMethod.GET -> Request.Builder().url(url)
8787
RequestMethod.HEAD -> Request.Builder().url(url).head()
88-
RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(body!!, contentType))
89-
RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(body!!, contentType))
90-
RequestMethod.POST -> Request.Builder().url(url).post(requestBody(body!!, contentType))
88+
RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(body, contentType))
89+
RequestMethod.PUT -> Request.Builder().url(url).put(requestBody(body, contentType))
90+
RequestMethod.POST -> Request.Builder().url(url).post(requestBody(body, contentType))
9191
RequestMethod.OPTIONS -> Request.Builder().url(url).method("OPTIONS", null)
9292
}
9393

0 commit comments

Comments
 (0)