From ef07a02a0128c78da48aaa40a4135241ddfb4d23 Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Sun, 4 Jun 2017 12:04:58 -0400 Subject: [PATCH] [kotlin] Support nested enums in models (#5769) * [kotlin] Add model enum support Model variables marked isEnum=true are nested class enums. Top-level enums will not be isEnum=true, but rather have a datatype specific to the enum's type. * [kotlin] Regenerate client sample --- .../kotlin-client/data_class.mustache | 22 ++++++++++ .../kotlin-client/data_class_opt_var.mustache | 4 ++ .../kotlin-client/data_class_req_var.mustache | 4 ++ .../kotlin-client/enum_class.mustache | 9 ++++ .../resources/kotlin-client/model.mustache | 15 +------ .../io/swagger/client/models/ApiResponse.kt | 20 +++++---- .../io/swagger/client/models/Category.kt | 16 +++---- .../kotlin/io/swagger/client/models/Order.kt | 41 +++++++++++------- .../kotlin/io/swagger/client/models/Pet.kt | 41 +++++++++++------- .../kotlin/io/swagger/client/models/Tag.kt | 16 +++---- .../kotlin/io/swagger/client/models/User.kt | 42 ++++++++++--------- .../swagger/client/functional/EvaluateTest.kt | 2 +- 12 files changed, 142 insertions(+), 90 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/kotlin-client/data_class.mustache create mode 100644 modules/swagger-codegen/src/main/resources/kotlin-client/data_class_opt_var.mustache create mode 100644 modules/swagger-codegen/src/main/resources/kotlin-client/data_class_req_var.mustache create mode 100644 modules/swagger-codegen/src/main/resources/kotlin-client/enum_class.mustache diff --git a/modules/swagger-codegen/src/main/resources/kotlin-client/data_class.mustache b/modules/swagger-codegen/src/main/resources/kotlin-client/data_class.mustache new file mode 100644 index 00000000000..82a4b0514d0 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/kotlin-client/data_class.mustache @@ -0,0 +1,22 @@ +/** + * {{{description}}} +{{#vars}} + * @param {{name}} {{{description}}} +{{/vars}} + */ +data class {{classname}} ( +{{#requiredVars}} +{{>data_class_req_var}}{{^-last}}, +{{/-last}}{{/requiredVars}}{{#hasRequired}}, +{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}}, +{{/-last}}{{/optionalVars}} +) { +{{#hasEnums}}{{#vars}}{{#isEnum}} + enum class {{nameInCamelCase}}(val value: {{datatype}}) { + {{#_enum}} + {{{this}}}({{#isString}}"{{/isString}}{{{this}}}{{#isString}}"{{/isString}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} + {{/_enum}} + } + +{{/isEnum}}{{/vars}}{{/hasEnums}} +} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/kotlin-client/data_class_opt_var.mustache b/modules/swagger-codegen/src/main/resources/kotlin-client/data_class_opt_var.mustache new file mode 100644 index 00000000000..a88761ea900 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/kotlin-client/data_class_opt_var.mustache @@ -0,0 +1,4 @@ +{{#description}} + /* {{{description}}} */ +{{/description}} + val {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/kotlin-client/data_class_req_var.mustache b/modules/swagger-codegen/src/main/resources/kotlin-client/data_class_req_var.mustache new file mode 100644 index 00000000000..8a33a15a188 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/kotlin-client/data_class_req_var.mustache @@ -0,0 +1,4 @@ +{{#description}} + /* {{{description}}} */ +{{/description}} + val {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/kotlin-client/enum_class.mustache b/modules/swagger-codegen/src/main/resources/kotlin-client/enum_class.mustache new file mode 100644 index 00000000000..42ce17d35cb --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/kotlin-client/enum_class.mustache @@ -0,0 +1,9 @@ +/** +* {{{description}}} +* Values: {{#allowableValues}}{{#enumVars}}{{name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} +*/ +enum class {{classname}}(val value: {{dataType}}){ +{{#allowableValues}}{{#enumVars}} + {{name}}({{#isString}}"{{/isString}}{{{value}}}{{#isString}}"{{/isString}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} +{{/enumVars}}{{/allowableValues}} +) \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/kotlin-client/model.mustache b/modules/swagger-codegen/src/main/resources/kotlin-client/model.mustache index 442753f7ef2..780dd84b97e 100644 --- a/modules/swagger-codegen/src/main/resources/kotlin-client/model.mustache +++ b/modules/swagger-codegen/src/main/resources/kotlin-client/model.mustache @@ -6,19 +6,6 @@ package {{modelPackage}} {{#models}} {{#model}} -/** -* {{{description}}} -{{#vars}} -* @param {{name}} {{{description}}} -{{/vars}} -*/ -data class {{classname}} ( - {{#vars}} - {{#description}} - /* {{{description}}} */ - {{/description}} - val {{{name}}}: {{{datatype}}}{{^required}}?{{/required}}{{#hasMore}},{{/hasMore}} - {{/vars}} -) +{{#isEnum}}{{>enum_class}}{{/isEnum}}{{^isEnum}}{{>data_class}}{{/isEnum}} {{/model}} {{/models}} diff --git a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/ApiResponse.kt b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/ApiResponse.kt index 82521f53eb8..8b2325ac4d0 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/ApiResponse.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/ApiResponse.kt @@ -13,13 +13,15 @@ package io.swagger.client.models /** -* Describes the result of uploading an image resource -* @param code -* @param type -* @param message -*/ + * Describes the result of uploading an image resource + * @param code + * @param type + * @param message + */ data class ApiResponse ( - val code: kotlin.Int?, - val type: kotlin.String?, - val message: kotlin.String? -) + val code: kotlin.Int? = null, + val type: kotlin.String? = null, + val message: kotlin.String? = null +) { + +} diff --git a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Category.kt b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Category.kt index e5cbf21efda..60c0b058dd1 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Category.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Category.kt @@ -13,11 +13,13 @@ package io.swagger.client.models /** -* A category for a pet -* @param id -* @param name -*/ + * A category for a pet + * @param id + * @param name + */ data class Category ( - val id: kotlin.Long?, - val name: kotlin.String? -) + val id: kotlin.Long? = null, + val name: kotlin.String? = null +) { + +} diff --git a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Order.kt b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Order.kt index 324908a7b1c..383d5e155ea 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Order.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Order.kt @@ -13,20 +13,29 @@ package io.swagger.client.models /** -* An order for a pets from the pet store -* @param id -* @param petId -* @param quantity -* @param shipDate -* @param status Order Status -* @param complete -*/ + * An order for a pets from the pet store + * @param id + * @param petId + * @param quantity + * @param shipDate + * @param status Order Status + * @param complete + */ data class Order ( - val id: kotlin.Long?, - val petId: kotlin.Long?, - val quantity: kotlin.Int?, - val shipDate: java.time.LocalDateTime?, - /* Order Status */ - val status: kotlin.String?, - val complete: kotlin.Boolean? -) + val id: kotlin.Long? = null, + val petId: kotlin.Long? = null, + val quantity: kotlin.Int? = null, + val shipDate: java.time.LocalDateTime? = null, + /* Order Status */ + val status: Order.Status? = null, + val complete: kotlin.Boolean? = null +) { + + enum class Status(val value: kotlin.String) { + placed("placed"), + approved("approved"), + delivered("delivered"); + } + + +} diff --git a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Pet.kt b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Pet.kt index 31842fa7499..5fe800afe77 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Pet.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Pet.kt @@ -15,20 +15,29 @@ import io.swagger.client.models.Category import io.swagger.client.models.Tag /** -* A pet for sale in the pet store -* @param id -* @param category -* @param name -* @param photoUrls -* @param tags -* @param status pet status in the store -*/ + * A pet for sale in the pet store + * @param id + * @param category + * @param name + * @param photoUrls + * @param tags + * @param status pet status in the store + */ data class Pet ( - val id: kotlin.Long?, - val category: Category?, - val name: kotlin.String, - val photoUrls: kotlin.collections.List, - val tags: kotlin.collections.List?, - /* pet status in the store */ - val status: kotlin.String? -) + val name: kotlin.String, + val photoUrls: kotlin.collections.List, + val id: kotlin.Long? = null, + val category: Category? = null, + val tags: kotlin.collections.List? = null, + /* pet status in the store */ + val status: Pet.Status? = null +) { + + enum class Status(val value: kotlin.String) { + available("available"), + pending("pending"), + sold("sold"); + } + + +} diff --git a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Tag.kt b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Tag.kt index ec9fb55e576..a584da9bdf9 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Tag.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/Tag.kt @@ -13,11 +13,13 @@ package io.swagger.client.models /** -* A tag for a pet -* @param id -* @param name -*/ + * A tag for a pet + * @param id + * @param name + */ data class Tag ( - val id: kotlin.Long?, - val name: kotlin.String? -) + val id: kotlin.Long? = null, + val name: kotlin.String? = null +) { + +} diff --git a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/User.kt b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/User.kt index 4bb9a160458..bf3dff18f94 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/User.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/io/swagger/client/models/User.kt @@ -13,24 +13,26 @@ package io.swagger.client.models /** -* A User who is purchasing from the pet store -* @param id -* @param username -* @param firstName -* @param lastName -* @param email -* @param password -* @param phone -* @param userStatus User Status -*/ + * A User who is purchasing from the pet store + * @param id + * @param username + * @param firstName + * @param lastName + * @param email + * @param password + * @param phone + * @param userStatus User Status + */ data class User ( - val id: kotlin.Long?, - val username: kotlin.String?, - val firstName: kotlin.String?, - val lastName: kotlin.String?, - val email: kotlin.String?, - val password: kotlin.String?, - val phone: kotlin.String?, - /* User Status */ - val userStatus: kotlin.Int? -) + val id: kotlin.Long? = null, + val username: kotlin.String? = null, + val firstName: kotlin.String? = null, + val lastName: kotlin.String? = null, + val email: kotlin.String? = null, + val password: kotlin.String? = null, + val phone: kotlin.String? = null, + /* User Status */ + val userStatus: kotlin.Int? = null +) { + +} diff --git a/samples/client/petstore/kotlin/src/test/kotlin/io/swagger/client/functional/EvaluateTest.kt b/samples/client/petstore/kotlin/src/test/kotlin/io/swagger/client/functional/EvaluateTest.kt index 8983641ce38..955560fb798 100644 --- a/samples/client/petstore/kotlin/src/test/kotlin/io/swagger/client/functional/EvaluateTest.kt +++ b/samples/client/petstore/kotlin/src/test/kotlin/io/swagger/client/functional/EvaluateTest.kt @@ -9,7 +9,7 @@ class EvaluateTest : ShouldSpec() { init { should("query against pet statuses") { val api = PetApi() - val results = api.findPetsByStatus(listOf("available", "pending")) + val results = api.findPetsByStatus(listOf("sold")) results.size should beGreaterThan(1) }