4

I'm generate a java client using swagger codegen cli tool (version 3.0.25) in gradle:

task doCodeGenSdk(type: JavaExec) {
main = "io.swagger.codegen.v3.Codegen"
classpath = configurations.codeGenCli
inputs.files file("${swaggerSpecDir}/*.json")
outputs.dir file("${codeGenDirSdk}")
args = ["generate", "--lang", "java", "--input-spec", "${swaggerSpecFile}",
        "--config", "${swaggerConfig}",
        "--template-dir", "${swaggerTemplateDir}",
        "--output", "${codeGenDirSdk}"]
systemProperty "apiTests", "false"
systemProperty "apiDocs", "false"
systemProperty "modelTests", "false"
systemProperty "modelDocs", "false"}

The swagger config file is as shown below:

{
"library": "resttemplate",
"artifactVersion": "__VERSION__",
"artifactId": "__ARTIFACT_ID__",
"modelPackage": "com.model",
"apiPackage": "com.api",
"invokerPackage": "com.invoker",
"dateLibrary": "java8",
"apiTests": false,
"java8": true,
"serializableModel": true,
"useBeanValidation": true,
"performBeanValidation": true}

The template-directory has the following mustache files:

ApiClient.mustache, generatedAnnotation.mustache, licenseInfo.mustach, pojo.mustache

The gradle file:

buildscript {
repositories {
    maven {
        url "https://plugins.gradle.org/m2/"
    }
}
dependencies {
    classpath "io.swagger.core.v3:swagger-gradle-plugin:2.1.10"
  }
}
apply plugin: io.swagger.v3.plugins.gradle.SwaggerPlugin
configurations {
    codeGenCli
}
dependencies {
    codeGenCli "io.swagger.codegen.v3:swagger-codegen-cli:3.0.25"
}

The swagger codegen generates the files in the build directory, however there are import error for some of the packages.

error: package io.swagger.v3.oas.annotations.media does not exist

We were using swagger codegen tool to generate client code using swagger spec file in 2.0.

This is the first time, we're trying to generate a client using a swagger spec file in 3.0.

Any help would be much appreciated.

Sander Verhagen
  • 8,540
  • 4
  • 41
  • 63
Alok Nath Saha
  • 297
  • 1
  • 4
  • 20

2 Answers2

11

I was able to get the dependency issue fixed by adding the swagger-annotations library during the compile time in the build.gradle file:

dependencies {
implementation "io.swagger.core.v3:swagger-annotations:2.1.10"
}
Alok Nath Saha
  • 297
  • 1
  • 4
  • 20
  • This did the job for me in a multimodule project, where the master-pom already had swagger configured. I disabled the master-pom swagger with exclusions inside ( and ) (groupid & artifactid) It will make sense for someone tangled in this Leaving me with two dependencies in the childpom: org.springdoc.springdoc-openapi-ui & io.swagger.core.v3.swagger-annotations – Esther Sep 20 '22 at 20:48
0

Add this dependency

 <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.2.10</version>
</dependency>```
Ahmad R. Nazemi
  • 775
  • 10
  • 26