OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.82k stars 6.33k forks source link

[REQ] Option to generate Gradle project with `build.gradle.kts` (Kotlin DSL) instead of `build.gradle` (Groovy DSL) #17889

Open volkert-fastned opened 5 months ago

volkert-fastned commented 5 months ago

Is your feature request related to a problem? Please describe.

When using the openapi-generator to generate a Kotlin project from an OpenAPI spec, it generates a Gradle project with build.gradle and settings.gradle files in the older Groovy format. It would be nice if there were a configuration option to have such a project generated with build.gradle.kts and settings.gradle.kts files (in the newer Gradle Kotlin DSL format) instead, since some of us prefer that, especially in Kotlin projects. I've searched existing GitHub issues for this, and I'm surprised that no one else has submitted this feature request yet.

Describe the solution you'd like

When invoking the generator, I'd like to be able to do something like this:

openApiGenerate {
    generatorName.set("kotlin")
    configOptions.set(mapOf(
        "gradleBuildDsl" to "kotlin" // as opposed to "groovy"
    ))
}

Describe alternatives you've considered

First of all, I checked whether such an option already existed in the documentation of the OpenAPI Kotlin Generator, but I found no mention of such a feature there.

I've looked for tools and plugins to automatically convert the generated Gradle project from Groovy DSL to Kotlin DSL, and I found GradleKotlinConverter, but it hasn't had a new release in almost 4 years, so it doesn't appear to be actively maintained. Also, having to run such a converter separately after having the automatic project generation would require additional setup work, would be slower and might potentially lead to conversion errors.

Additional context

The Kotlin DSL is increasingly becoming the preferred DSL for Gradle projects, and even officially so in the case of Android projects:

Kotlin is preferred over the Groovy for writing Gradle scripts because Kotlin is more readable and offers better compile-time checking and IDE support.

Although Kotlin currently offers better integration in Android Studio’s code editor when compared to Groovy, builds using Kotlin tend to be slower than builds using Groovy, so consider build performance when deciding whether to migrate.

Source: https://developer.android.com/build/migrate-to-kotlin-dsl

Also, when you're working on a Kotlin project anyway, it's more convenient to have the build configuration in the same language you're using for the source code in the project, instead of having to mentally switch back and forth between Kotlin and Groovy. And although there is a tradeoff between convenience and performance, it would be good to offer us the choice between the two, through use of such a config option. Also, the Kotlin DSL performance has already been improved in newer Gradle versions.

Thank you kindly for considering this.

volkert-fastned commented 5 months ago

Hmm... This is a bit embarrassing... But when I updated the openapi-generator Gradle plugin from 7.2.0 to 7.3.0, it actually started to generate the Gradle config in Kotlin DSL format. 😅 Good work!

However, two things:

JiangHongTiao commented 3 months ago

@volkert-fastned How have you resolved this? I'm generating kotlin with jvm-retrofit2 and the script still ignores my files if I rename them to build.gradle.kts.mustache (settings the same). I have tried version 7.3.0 but also 7.5.0 and still the same. Is there any hidden parameter how I can get kotlin support for gradle?

I think it should not matter, if the template is named build.gradle.mustache or build.gradle.kts.mustache, it's sill generating only a file from template.

wing328 commented 3 months ago

. It would be nice if there were a configuration option to have such a project generated with build.gradle.kts and settings.gradle.kts files (in the newer Gradle Kotlin DSL format) instead,

we definitely welcome contributions to generate build.gradle.kts and setting.gradle.kts.

If I read correctly, some kotlin generators already support generating both at the moment but you would like an option to skip one or the other format.

one way to skip the file from being generated is using .openapi-generator-ignore: https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#ignore-file-format, and one can also pre-populate this file as part of the code generation process using the option openapiGeneatorIgnoreList (e.g. --openapi-generator-ignore-list in CLI)