kokuwaio/micronaut-openapi-codegen

Help: Using the codegen via gradle plugin and cli

xevenheaven opened this issue ยท 6 comments

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!

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

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
}

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

openApiGenerate {
    outputDir = "$buildDir/generated".toString()
}

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.

That is not possible yet: MicronautCodegen.java

Feel free to submit a pull request.

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!