gradle-swagger-generator-plugin icon indicating copy to clipboard operation
gradle-swagger-generator-plugin copied to clipboard

Custom generator no longer seems to work

Open bjconlan-fc opened this issue 6 years ago • 4 comments

We are trying to migrate from 2.11.0 to 2.15.1 and found that the custom code generator logic no longer seems to work as expected (using a 2.x based custom generator). Currently in 2.15.1 its returning:

Can't load config class with name net.test.build.HotelsSpringCodegen Available: ada (continues to list supported codeGen config strings.

bjconlan-fc avatar Dec 18 '18 06:12 bjconlan-fc

Would you provide the root cause of stack trace? You can see stack trace by ./gradlew -s option. For example,

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':generateSwaggerCodePetstore'.
(snip)

Caused by: java.lang.RuntimeException: Error while running swagger-codegen-cli
(snip)

Caused by: java.lang.RuntimeException: Can't load config class with name generators.MySpring Available: ...
(snip)

Caused by: java.lang.ClassCastException: generators.MySpring cannot be cast to io.swagger.codegen.v3.CodegenConfig
	at io.swagger.codegen.v3.CodegenConfigLoader.forName(CodegenConfigLoader.java:29)
	... 2 more

int128 avatar Dec 19 '18 02:12 int128

I had to do this customization in the config:

...
code.configuration = project.buildscript.configurations.getByName("classpath")
...

This makes the java exec of the swagger codegen cli use the buildscript classpath, which contained my generator class.

drmaas avatar Aug 31 '19 05:08 drmaas

it's been a while on this thread, but...did you solve the issue? I tried the code.configuration =...but the problem with that is it seems to override the dependency information too, so then it fails because it thinks swagger-codegen-cli isn't a dependency

happen to know how can i get it on the classpath but also the other dependency?

sreich avatar Mar 14 '23 20:03 sreich

Got it working, using swagger2 codegen, adding the following was the trick, ultimately. the @jar syntax, in particular...(sample here is truncated, so groupid/etc may not be uniform)

implementation("io.swagger:swagger-codegen:$swaggerVersion") { exclude("org.slf4", "sl4j-simple") }
swaggerCodegen("io.swagger:swagger-codegen-cli:$swaggerVersion")

//// .....


dependencies {
    swaggerCodegen("com.myorg.generator:my-swagger-generator:3.7.0-SNAPSHOT@jar")
}` 


for tests (using junit & testkit), i did:

`    val SWAGGER_TEST_CASE = """
import org.hidetake.gradle.swagger.generator.GenerateSwaggerCode
plugins {
    `kotlin-dsl`
    `maven-publish`
    id("com.myorg.service-conventions") version "+"
}

repositories {
    gradlePluginPortal()
    mavenCentral()
}

dependencies {
    swaggerCodegen("com.myorg:my-swagger-generator:3.7.0-SNAPSHOT@jar")
}

swaggerSources {
    register("swagger-test-plugin") {
        // Need to save a reference of it here because the `this` scope changes below.
        val validationTask = validation

        val yamlResourceDir = sourceSets.test.get().resources.srcDirs.first().path

        println("test using resource dir ${'$'}yamlResourceDir ")
        val yamlDir = "${'$'}yamlResourceDir/yaml"

        println("test yamlDir is ${'$'}yamlDir")
        setInputFile(file("${'$'}yamlDir/test.yaml"))

        code(delegateClosureOf<GenerateSwaggerCode> {
            language = "com.myorg.generator.swagger.MySpringCodegen"
            outputDir = file("build/generated")
            components = listOf("models")

            dependsOn(validationTask, tasks.named("build")
             , project.getTasksByName("my-swagger-generator:jar", true))
        })
    }
}

@bjconlan-fc close this issue please if you don't need it open still. it works for me or @int128 can you close this, trying to clean up..

the 'this reference' thing i don't think is actually needed, you can reference it via the this@ syntax to reference out of the closure

sreich avatar Apr 24 '23 15:04 sreich