Package io.swagger.v3.oas.annotations.media does not exist (Swagger Codegen)
Asked Answered
C

2

7

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.

Competition answered 19/8, 2021 at 6:58 Comment(0)
C
12

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"
}
Competition answered 19/8, 2021 at 13:1 Comment(1)
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 <dependencyManagement><dependencies>(</master artifactId> and </groupid>) <exclusions> </exclusion>(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-annotationsThird
S
3

Add this dependency

<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>2.2.10</version>
</dependency>
Sassoon answered 22/5, 2023 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.