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
21.74k stars 6.56k forks source link

OpenApi Generator gradle plugin generates new gradle project #9602

Open AnonymousVovus opened 3 years ago

AnonymousVovus commented 3 years ago
Description

Hello!

I'm trying to generate code via OpenAPI Generator Gradle Plugin version 5.1.1 I can't understand behavior of this plugin, it generates new gradle project, but not only kotlin classes

my task configuration is

openApiGenerate {
    //verbose.set(true)
    generatorName.set("kotlin")
    outputDir.set(project.file("${buildDir.name}/generated").absolutePath)
    inputSpec.set(project.file("schema/openapi.json").absolutePath)
    packageName.set("org.camunda.bpm.engine.rest")
    apiPackage.set("org.camunda.bpm.engine.rest.api")
    modelPackage.set("org.camunda.bpm.engine.rest.model")
}

As far as i know, maven plugin generates just classes, not new project Looks like gradle plugins works as generator cli

Why openapi generator gradle plugin generates new project? photo_2021-05-27_12-14-42

openapi-generator version

5.1.1

OpenAPI declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement
AnonymousVovus commented 3 years ago

up )

ChristianCiach commented 3 years ago

I second this. To this day I do not understand the purpose of the gradle plugin. One would expect this plugin to automatically include the generated code to /src/gen/java (not literally, but make the generated code part of a java sourceSet) of the gradle project that uses the plugin, and also extend the compileClasspath configuration to include the necessary dependencies (like jackson and stuff). But in fact, the Gradle plugin is a very thin wrapper around the CLI. You can just as well call this directly via javaExec.

To actually use the generated code inside your own gradle project, you have to do hacky stuff like sourceSets.main.java.srcDir "${buildDir}/generated/src/main/java" and also manually (!) copy the dependencies from the generated build.gradle file into your own.

We have tens of developers in our company, and every single one of them misunderstands the purpose of the gradle plugin. All one of them, myself included, expect this plugin to make it so that the generated code becomes directly usable inside your own gradle project. But unfortunately, this is not the case.

keesvandieren commented 3 years ago

What we would like to have:

Just to confirm: this is not something that is already possible right?

IARI commented 3 years ago

I'm trying to at least include the generated project as a sub-project, but that fails. I adjusted my kotlin version to match the currently used 5.1, but thats not enough. The main project uses gradle 7.1.1 vs gradle 6.8.3 on the generated project. Managed to make it run with an extra task that removes the wrapper { .... } portion of the build.gradle file.

Clearly people who want to use the gradle plugin will have use-cases like me and the others in this issue where we are not interested in generating a separate gradle project.

However I would assume that it's not trivial to solve this issue, simply due to the fact that other people might want use it to build for other targets than kotlin or java (where gradle projects are generated).

Vampire commented 3 years ago

Here an example how to make it work with the Java generator:

val openApiGeneratorIgnoreFile = layout.projectDirectory.file(".openapi-generator-ignore")

openApiGenerate {
    generatorName.set("java")
    outputDir.set(layout.buildDirectory.dir("generated/sources/openapi/main/java").map {
        it.asFile.absolutePath
    })
    inputSpec.set(downloadNexusOpenApiSpec.map {
        it.outputs.files.singleFile.absolutePath
    })
    generateApiDocumentation.set(false)
    generateApiTests.set(false)
    generateModelDocumentation.set(false)
    generateModelTests.set(false)
    invokerPackage.set("org.sonatype.nexus.client")
    apiPackage.set("org.sonatype.nexus.client.api")
    modelPackage.set("org.sonatype.nexus.client.model")
    configOptions.set(mapOf(
        "sourceFolder" to "",
        "dateLibrary" to "java8"
    ))
    ignoreFileOverride.set(
        openApiGeneratorIgnoreFile.asFile.absolutePath
    )
}

// TODO: work-around for https://github.com/OpenAPITools/openapi-generator/issues/10214
tasks.openApiGenerate {
    inputs
        .files(openApiGeneratorIgnoreFile)
        .withPathSensitivity(NONE)
        .withPropertyName("openApiGeneratorIgnoreFile")
}
/build/generated/sources/openapi/main/java/**
!build/generated/sources/openapi/main/java/org/**
Vampire commented 3 years ago

Or like this for Kotlin:

val openApiGeneratorIgnoreFile = layout.projectDirectory.file(".openapi-generator-ignore")

openApiGenerate {
    generatorName.set("kotlin")
    outputDir.set(layout.buildDirectory.dir("generated/sources/openapi/main/kotlin").map {
        it.asFile.absolutePath
    })
    inputSpec.set(downloadNexusOpenApiSpec.map {
        it.outputs.files.singleFile.absolutePath
    })
    generateApiDocumentation.set(false)
    generateApiTests.set(false)
    generateModelDocumentation.set(false)
    generateModelTests.set(false)
    packageName.set("org.sonatype.nexus.client")
    apiPackage.set("org.sonatype.nexus.client.api")
    modelPackage.set("org.sonatype.nexus.client.model")
    configOptions.set(mapOf(
        "sourceFolder" to "",
        "enumPropertyNaming" to "original"
    ))
    ignoreFileOverride.set(
        openApiGeneratorIgnoreFile.asFile.absolutePath
    )
}

// TODO: work-around for https://github.com/OpenAPITools/openapi-generator/issues/10214
tasks.openApiGenerate {
    inputs
        .files(openApiGeneratorIgnoreFile)
        .withPathSensitivity(NONE)
        .withPropertyName("openApiGeneratorIgnoreFile")
}

sourceSets {
    main {
        java {
            srcDir(tasks.openApiGenerate)
        }
    }
}
/build/generated/sources/openapi/main/kotlin/**
!build/generated/sources/openapi/main/kotlin/org/**
AnonymousVovus commented 3 years ago

Or like this for Kotlin:

val openApiGeneratorIgnoreFile = layout.projectDirectory.file(".openapi-generator-ignore")

openApiGenerate {
    generatorName.set("kotlin")
    outputDir.set(layout.buildDirectory.dir("generated/sources/openapi/main/kotlin").map {
        it.asFile.absolutePath
    })
    inputSpec.set(downloadNexusOpenApiSpec.map {
        it.outputs.files.singleFile.absolutePath
    })
    generateApiDocumentation.set(false)
    generateApiTests.set(false)
    generateModelDocumentation.set(false)
    generateModelTests.set(false)
    packageName.set("org.sonatype.nexus.client")
    apiPackage.set("org.sonatype.nexus.client.api")
    modelPackage.set("org.sonatype.nexus.client.model")
    configOptions.set(mapOf(
        "sourceFolder" to "",
        "enumPropertyNaming" to "original"
    ))
    ignoreFileOverride.set(
        openApiGeneratorIgnoreFile.asFile.absolutePath
    )
}

// TODO: @bkautler work-around for https://github.com/OpenAPITools/openapi-generator/issues/10214
tasks.openApiGenerate {
    inputs
        .files(openApiGeneratorIgnoreFile)
        .withPathSensitivity(NONE)
        .withPropertyName("openApiGeneratorIgnoreFile")
}

sourceSets {
    main {
        java {
            srcDir(tasks.openApiGenerate)
        }
    }
}
/build/generated/sources/openapi/main/kotlin/**
!build/generated/sources/openapi/main/kotlin/org/**

Thanks for your example, i hope it helps )