1

I tried to generate interfaces from a swagger file we need to implement. It's provided from an external source. I usually use contract first, but didn't do that in a long time with java/kotlin.

I already have a project set up and want to have as much control of my code as possible. I tried to use openapi-generator-gradle-plugin with kotlin-spring or spring generator, to generate the interfaces (the import part).

Best would be Interfaces with spring related annotations (RequestMappings etc.) and at least just the interfaces. But it seems, the generator also generates a "little application" (with gradle/maven file, SpringBootApp, README.md ...).

I don't have the code anymore, but was pretty sure, the last time I did this, it was easy possible to just generate interfaces, but can't remember which generator or plugin I used.

So my question is, did I miss something or is just creating interfaces not an issue anymore? Does everyone wants to do scaffolding here?

1

3 Answers 3

2

I use the openapi-generator-maven-plugin with <interfaceOnly>true</interfaceOnly> configOption. It should be similar in gradle with kotlin-spring generator

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>5.0.0-beta</version>
  <executions>
    <execution>
      <id>1</id>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>openapi.json</inputSpec>
        <generatorName>spring</generatorName>
        <modelPackage>modelPackage</modelPackage>
        <apiPackage>apiPackage</apiPackage>
        <invokerPackage>invokerPackage</invokerPackage>
        <configOptions>
          <sourceFolder>target</sourceFolder>
          <interfaceOnly>true</interfaceOnly>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>
4
  • This approach doesn't create the @SpringBootApplication Class, but also creates a pom.xml and a README.md. So the approach the generator takes is to create a whole project instead of just the interfaces. It literally ignores "interfaceOnly". Does the maven plugin has the same behaviour?
    – lrxw
    Nov 26, 2020 at 7:57
  • is the same behaviour, but i generate them in target folder and i import the classes in my project with @ComponentScan(basePackages = {"openapi.project","com.project"}) and then you can use the generated code Nov 26, 2020 at 11:34
  • This is an example without <interfaceOnly> github.com/Jiratech/boilerplates-java-openapi. If you don't add <interfeceOnly> it generates a project that will also have swagger-ui out of the box Nov 26, 2020 at 11:38
  • Ok, so you just ignore the other stuff? I think that could work for the spring generator. Unfortunately the kotlin-spring generator also generates the SpringBootApp which would also be compiled and packeged into the final JAR.
    – lrxw
    Nov 26, 2020 at 12:41
1

I added the following properties to my openApiGenerate task:

    globalProperties =[
            apis: '',
            models: '',
    ]

which solved the issue for me.

1
  • So simple and elegant Mar 23, 2022 at 9:03
1

If you are using gradle with a build.gradle.kts you can use this openapi-generator plugin task to generate the swagger code. It does the same thing as the swagger codegen plugin:

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

openApiGenerate {
    generatorName.set("spring")
    inputSpec.set("$rootDir/src/main/resources/petstore.yaml")
    outputDir.set("$buildDir/generated/")
    configFile.set("$rootDir/src/main/resources/api-config.json")


    globalProperties.set(mapOf(
        Pair("apis", ""), //no value or comma-separated api names
        Pair("models", ""), //no value or comma-separated api names
    ))
}

The apis generated from the paths in your swagger. The api-config.json has the settings of your generator. and then you need to add the generated classes to your sourceSet by doing:

configure<SourceSetContainer> {
    named("main") {
        java.srcDir("$buildDir/generated/src/main/java")
    }
}

Finally make sure you generate the swagger files before compiling:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    dependsOn("openApiGenerate")
    kotlinOptions.jvmTarget = "11"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.