英文:
Openapi generator maven plugin generate classes with missing symbol
问题
我使用 openapi-generator-maven-plugin
从 openapi yml 文件生成类,但在其中一些文件中遇到了以下错误:
无法找到符号
符号: 方法 requiredMode()
位置: @interface io.swagger.v3.oas.annotations.media.Schema
我检查了 io.swagger.v3.oas.annotations.media.Schema
,发现确实缺少了 requiredMode()
方法。
以下是我的 pom 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.maglione</groupId>
<artifactId>contactsapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contactsapi</name>
<description>Demo project </description>
<properties>
<java.version>17</java.version>
<openapi-generator.version>6.6.0</openapi-generator.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!-- 其他依赖项... -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/contactsapi.yml
</inputSpec>
<!-- 其他配置... -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我尝试在线搜索可能的错误,但未找到任何信息。
英文:
I use openapi-generator-maven-plugin
to generate classes from openapi yml files but in some of them I get this errors:
Cannot find symbol
symbol: method requiredMode()
location: @interface io.swagger.v3.oas.annotations.media.Schema
I checked in io.swagger.v3.oas.annotations.media.Schema
and method requiredMode() is really missing.
Below my pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.maglione</groupId>
<artifactId>contactsapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contactsapi</name>
<description>Demo project </description>
<properties>
<java.version>17</java.version>
<openapi-generator.version>6.6.0</openapi-generator.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.6</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/contactsapi.yml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>com.maglione.contactsapi.api</apiPackage>
<modelPackage>com.maglione.contactsapi.model</modelPackage>
<supportingFilesToGenerate>
ApiUtil.java
</supportingFilesToGenerate>
<configOptions>
<delegatePattern>true</delegatePattern>
<useSpringBoot3>true</useSpringBoot3>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I tried to search online for possible bug but did not find anything
答案1
得分: 2
欢迎来到StackOverflow,jack nik!
正如Helen所提到的,requiredMode()
只存在于较新版本的 swagger-annotations
中。
要检查项目使用的版本,运行 mvn dependency:tree
命令。你会发现通过 springfox-swagger2
间接包含的 swagger-annotations
版本为 2.1.2。
如果你从 pom.xml 中删除 supportingFilesToGenerate
部分,openapi-generator 将会创建一个文件 .target/generated-sources/openapi/pom.xml
,其中列出了要与生成的代码一起使用的预期依赖项。在你的情况下,这些依赖项包括通过 org.springdoc:springdoc-openapi-starter-webmvc-ui:jar:2.0.2
间接包含的 io.swagger.core.v3:swagger-core-jakarta:jar:2.2.7
。
要修复你的构建问题,你需要包含较新的 swagger 依赖项。你还需要排除旧的 swagger 依赖项,以便 Maven 知道必须使用新依赖项中的注解,而不能使用旧依赖项中的注解。
因此,要修复你的问题,请将此代码替换为:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
替换为这个代码:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core-jakarta</artifactId>
<version>2.2.7</version>
</dependency>
英文:
Welcome to StackOverflow, jack nik!
As Helen mentioned, requiredMode()
exists only in newer versions of swagger-annotations
.
To check what version your project is using, run mvn dependency:tree
. You'll find that swagger-annotations
version 2.1.2 is used, as transitively included via springfox-swagger2
.
If you remove the supportingFilesToGenerate
section from your pom.xml, openapi-generator will create a file .target/generated-sources/openapi/pom.xml
that tells you the intended dependencies to be used with the generated code. In your case, these dependencies include io.swagger.core.v3:swagger-core-jakarta:jar:2.2.7
, transitively included via org.springdoc:springdoc-openapi-starter-webmvc-ui:jar:2.0.2
.
To fix your build, you need to include the newer swagger dependency. And you also need to exclude the old swagger dependency, so that maven knows it has to use the annotations from the new dependency and cannot use the annotations from the old dependency.
So to fix your problem, replace this:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
by this:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core-jakarta</artifactId>
<version>2.2.7</version>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论