Help: Using the codegen via gradle plugin and cli #72

Closed
opened 2021-06-29 09:25:03 +02:00 by xevenheaven · 6 comments
xevenheaven commented 2021-06-29 09:25:03 +02:00 (Migrated from github.com)

Hi, I'd like some help using this codegen tool.

I wanted to use the openapi-generator-gradle-plugin with this tool but am not sure of how to configure the gradle plugin to use this as a custom generator. I've added the following in build.gradle:

plugins {
    id("org.openapi.generator") version "5.1.1"
}

dependencies {
    implementation("io.micronaut:micronaut-http-netty-server")
    implementation("io.micronaut:micronaut-runtime")
    implementation("io.micronaut:micronaut-validation")
    implementation("io.kokuwa.micronaut:micronaut-openapi-codegen:2.1.6")
    implementation("org.openapitools:openapi-generator-cli:5.1.1")
}

openApiGenerate {
    generatorName = "micronaut"
    inputSpec = "${projectDir}/specs/test.yml"
    outputDir = buildDir.toString()
    packageName = "com.autodesk.test.codegen"
    apiPackage = "com.autodesk.test.codegen.handler.client"
    modelPackage = "com.autodesk.test.codegen.model"
    invokerPackage = "com.autodesk.test.codegen"
    validateSpec = true
}

And of course, getting the error: > Can't load config class with name 'micronaut'. Can someone please help?


Since I got stuck with the above, I decided to try using the openapi-generator-cli instead, using the following command:

java -cp libs/micronaut-openapi-codegen-2.1.6.jar:libs/openapi-generator-cli-5.1.1.jar \
    org.openapitools.codegen.OpenAPIGenerator generate -g micronaut \
    -i specs/test.yml \
    -o . \
    -p packageName=com.autodesk.test.codegen \
    -p apiPackage=com.autodesk.test.codegen.handler.client \
    -p modelPackage=com.autodesk.test.codegen.model \
    -p invokerPackage=com.autodesk.test.codegen

However, I'm getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: io/micronaut/http/HttpStatus
	at org.openapitools.codegen.languages.MicronautCodegen.fromOperation(MicronautCodegen.java:320)
	at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1113)
	at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1036)
	at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:561)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:878)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.ClassNotFoundException: io.micronaut.http.HttpStatus
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 8 more

I have double checked that io.micronaut.http and the HttpStatus.java file is available, so I can't figure out why I'm getting the NoClassDefFoundError. Does anyone have a clue?

I'm on Gradle 7.0 and Java 11.

Thanks for the help!

Hi, I'd like some help using this codegen tool. I wanted to use the openapi-generator-gradle-plugin with this tool but am not sure of how to configure the gradle plugin to use this as a custom generator. I've added the following in build.gradle: ```groovy plugins { id("org.openapi.generator") version "5.1.1" } dependencies { implementation("io.micronaut:micronaut-http-netty-server") implementation("io.micronaut:micronaut-runtime") implementation("io.micronaut:micronaut-validation") implementation("io.kokuwa.micronaut:micronaut-openapi-codegen:2.1.6") implementation("org.openapitools:openapi-generator-cli:5.1.1") } openApiGenerate { generatorName = "micronaut" inputSpec = "${projectDir}/specs/test.yml" outputDir = buildDir.toString() packageName = "com.autodesk.test.codegen" apiPackage = "com.autodesk.test.codegen.handler.client" modelPackage = "com.autodesk.test.codegen.model" invokerPackage = "com.autodesk.test.codegen" validateSpec = true } ``` And of course, getting the error: `> Can't load config class with name 'micronaut'`. Can someone please help? ---- Since I got stuck with the above, I decided to try using the openapi-generator-cli instead, using the following command: ```bash java -cp libs/micronaut-openapi-codegen-2.1.6.jar:libs/openapi-generator-cli-5.1.1.jar \ org.openapitools.codegen.OpenAPIGenerator generate -g micronaut \ -i specs/test.yml \ -o . \ -p packageName=com.autodesk.test.codegen \ -p apiPackage=com.autodesk.test.codegen.handler.client \ -p modelPackage=com.autodesk.test.codegen.model \ -p invokerPackage=com.autodesk.test.codegen ``` However, I'm getting the following error: ``` Exception in thread "main" java.lang.NoClassDefFoundError: io/micronaut/http/HttpStatus at org.openapitools.codegen.languages.MicronautCodegen.fromOperation(MicronautCodegen.java:320) at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1113) at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1036) at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:561) at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:878) at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441) at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32) at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66) Caused by: java.lang.ClassNotFoundException: io.micronaut.http.HttpStatus at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ... 8 more ``` I have double checked that io.micronaut.http and the HttpStatus.java file is available, so I can't figure out why I'm getting the NoClassDefFoundError. Does anyone have a clue? I'm on Gradle 7.0 and Java 11. Thanks for the help!
rpahli commented 2021-06-29 10:32:05 +02:00 (Migrated from github.com)

can you try to add this to your build file:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "org.openapitools:openapi-generator:5.1.1"
        classpath "io.kokuwa.micronaut:micronaut-openapi-codegen:2.1.6"
    }
}

I didn't test it but maybe this can help

can you try to add this to your build file: ``` buildscript { repositories { mavenLocal() mavenCentral() } dependencies { classpath "org.openapitools:openapi-generator:5.1.1" classpath "io.kokuwa.micronaut:micronaut-openapi-codegen:2.1.6" } } ``` I didn't test it but maybe this can help
xevenheaven commented 2021-06-29 11:41:34 +02:00 (Migrated from github.com)

Hi @rpahli, thanks for your quick response! I just tried what you suggested and I'm not getting any errors! 🎉

However, the generated code is in the folders "generated-sources" and "generated-test-sources":

image

Is there any way to override the folders? I would like the generated code to be in "src/main/java/com/autodesk/test/codegen". When I tried using openapi-gen previously, I believe I used use the sourceFolder option to configure that, but I just tried doing so here and it didn't seem to work:

openApiGenerate {
    generatorName = "micronaut"
    inputSpec = "${projectDir}/specs/test.yml"
    outputDir = "${projectDir}"
    packageName = "com.autodesk.test.codegen"
    modelPackage = "com.autodesk.test.codegen.model"
    apiPackage = "com.autodesk.test.codegen.api"
    invokerPackage = "com.autodesk.test.codegen"
    configOptions = [
            sourceFolder: "src/main/java",
            addCompileSourceRoot: "true",
            swaggerAnnotations: "false",
            useOptional: "true"
    ]
    validateSpec = true
}
Hi @rpahli, thanks for your quick response! I just tried what you suggested and I'm not getting any errors! 🎉 However, the generated code is in the folders "generated-sources" and "generated-test-sources": ![image](https://user-images.githubusercontent.com/6725396/123774827-62751600-d900-11eb-870c-570631acd499.png) Is there any way to override the folders? I would like the generated code to be in "src/main/java/com/autodesk/test/codegen". When I tried using openapi-gen previously, I believe I used use the `sourceFolder` option to configure that, but I just tried doing so here and it didn't seem to work: ```groovy openApiGenerate { generatorName = "micronaut" inputSpec = "${projectDir}/specs/test.yml" outputDir = "${projectDir}" packageName = "com.autodesk.test.codegen" modelPackage = "com.autodesk.test.codegen.model" apiPackage = "com.autodesk.test.codegen.api" invokerPackage = "com.autodesk.test.codegen" configOptions = [ sourceFolder: "src/main/java", addCompileSourceRoot: "true", swaggerAnnotations: "false", useOptional: "true" ] validateSpec = true } ```
rpahli commented 2021-06-29 12:04:08 +02:00 (Migrated from github.com)

I think you can use this to change the output dir:

openApiGenerate {
    outputDir = "$buildDir/generated".toString()
}
I think you can use this to change the output dir: ``` openApiGenerate { outputDir = "$buildDir/generated".toString() } ```
xevenheaven commented 2021-06-29 12:24:53 +02:00 (Migrated from github.com)

While outputDir controls the main folder in which the generated code go to, what I meant is I wanted to remove the "generated-sources" and "generated-test-sources" default folders/paths. Right now, the generated code is in generated-sources/openapi/com/autodesk/test/codegen but I want to merge it into the existing src/main/java/com/autodesk/test package.

What I want to achieve is the following structure:

.
├── src
│   ├── main
│   │   ├── java
│   │   │   ├── com
│   │   │   │   ├── autodesk
│   │   │   │   │   ├── test
│   │   │   │   │   │   └── codegen (generated code goes directly here into this package)

Is this possible?
Thanks.

While `outputDir` controls the main folder in which the generated code go to, what I meant is I wanted to remove the "generated-sources" and "generated-test-sources" default folders/paths. Right now, the generated code is in `generated-sources/openapi/com/autodesk/test/codegen` but I want to merge it into the existing `src/main/java/com/autodesk/test` package. What I want to achieve is the following structure: ``` . ├── src │ ├── main │ │ ├── java │ │ │ ├── com │ │ │ │ ├── autodesk │ │ │ │ │ ├── test │ │ │ │ │ │ └── codegen (generated code goes directly here into this package) ``` Is this possible? Thanks.
sschnabe commented 2021-06-29 12:28:57 +02:00 (Migrated from github.com)

That is not possible yet: MicronautCodegen.java

Feel free to submit a pull request.

That is not possible yet: [MicronautCodegen.java](https://github.com/kokuwaio/micronaut-openapi-codegen/blob/2.1.7/src/main/java/org/openapitools/codegen/languages/MicronautCodegen.java#L234-L237) Feel free to submit a pull request.
xevenheaven commented 2021-06-29 15:20:36 +02:00 (Migrated from github.com)

I see, thank you for the confirmation. I'll go ahead and close this issue as my questions have been addressed.

Thanks for all the help @rpahli and @stephanschnabel!

I see, thank you for the confirmation. I'll go ahead and close this issue as my questions have been addressed. Thanks for all the help @rpahli and @stephanschnabel!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
kokuwaio/micronaut-openapi-codegen#72
No description provided.