Skip to content

[Kotlin] Add ability to use modelNamePrefix/modelNameSuffix#2349

Merged
wing328 merged 1 commit intoOpenAPITools:masterfrom
karismann:kotlin-modelNamePrefix-modelNameSuffix
Mar 12, 2019
Merged

[Kotlin] Add ability to use modelNamePrefix/modelNameSuffix#2349
wing328 merged 1 commit intoOpenAPITools:masterfrom
karismann:kotlin-modelNamePrefix-modelNameSuffix

Conversation

@karismann
Copy link
Copy Markdown
Contributor

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh, ./bin/security/{LANG}-petstore.sh and ./bin/openapi3/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 3.4.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.
    @jimschubert (2017/09) , @dr4ke616 (2018/08)

Description of the PR

fix #1885
add some missing testcase on toModelName function in abstract Kotlin CodeGen package

assertEquals(codegen.toModelName("name"), "Name");
assertEquals(codegen.toModelName("$name"), "Dollarname");
assertEquals(codegen.toModelName("nam#e"), "NamHashe");
assertEquals(codegen.toModelName("$another-fake?"), "DollaranotherMinusfakeQuestionMark");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@karismann quick question if you don't mind. Should it be DollarAnotherMinusFakeQuestionMark (capital letter A and F) instead of DollaranotherMinusfakeQuestionMark?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@wing328 I think that's happening because of camelize, after which point we would lose context of what should be capitalized here, I would think.

See

Copy link
Copy Markdown
Member

@wing328 wing328 Mar 12, 2019

Choose a reason for hiding this comment

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

OK. If my memory serves me well, we've something like $prefix_$name before going through camelize to handle the case mentioned above in some other generators (e.g. Perl). We can definitely go with this implementation in Kotlin and see if that's what the user expects.

@jimschubert
Copy link
Copy Markdown
Member

Tested with prefixes and suffixes, and this seems to work as expected:

Prefix=Jim

$ openapi-generator.local generate --model-name-prefix Jim -g kotlin -i .bak/openapi/openapi.json -o .bak/1885/

$ cat src/main/kotlin/org/openapitools/client/models/JimTag.kt
/**
* 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.
*
* OpenAPI spec version: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.client.models


/**
 * A tag for a pet
 * @param id
 * @param name
 */
data class JimTag (
    val id: kotlin.Long? = null,
    val name: kotlin.String? = null
) {

}

Suffix=Cats

$ openapi-generator.local generate --model-name-suffix Cats -g kotlin -i .bak/openapi/openapi.json -o .bak/1885/

$ cat .bak/1885/src/main/kotlin/org/openapitools/client/models/PetCats.kt
/**
* 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.
*
* OpenAPI spec version: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.client.models

import org.openapitools.client.models.CategoryCats
import org.openapitools.client.models.TagCats

import com.squareup.moshi.Json
/**
 * 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 PetCats (
    val name: kotlin.String,
    val photoUrls: kotlin.Array<kotlin.String>,
    val id: kotlin.Long? = null,
    val category: CategoryCats? = null,
    val tags: kotlin.Array<TagCats>? = null,
    /* pet status in the store */
    val status: PetCats.Status? = null
) {

    /**
    * pet status in the store
    * Values: available,pending,sold
    */
    enum class Status(val value: kotlin.String){

        @Json(name = "available") available("available"),

        @Json(name = "pending") pending("pending"),

        @Json(name = "sold") sold("sold");

    }

}

Copy link
Copy Markdown
Member

@jimschubert jimschubert left a comment

Choose a reason for hiding this comment

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

Thanks for doing this up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REQ] Add ability to use modelNamePrefix/modelNameSuffix for Kotlin language

3 participants