如何解决Swagger 不解析部分引用或解析一个对象,但不解析另一个对象
我正在尝试使用 swagger 生成客户端。我有一个 yaml 文件,其中引用了另一个 yaml 文件。所有这些文件都在同一目录中。问题是部分引用解决了,但部分没有解决。
例如,在我的 yaml 文件中有 2 个对象具有相同的引用:
$ref: TS29571_CommonData.yaml#/components/schemas/Snssai
在 TS29571_CommonData.yaml 中它看起来像:
Snssai:
type: object
properties:
sst:
type: integer
minimum: 0
maximum: 255
sd:
type: string
pattern: '^[A-Fa-f0-9]{6}$'
required:
- sst
在一个已解决的生成文件中,正确导入 import generated.model.Snssai;
并正确使用此对象。但在另一个文件中,它看起来像 import generated.model.TS29571CommonDataYamlcomponentsschemasSnssai;
并且是红色的。
这是我的 pom.xml
<?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>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SwaggerMultipleYaml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SwaggerMultipleYaml</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.9.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>0.7.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${project.parent.version}</version>
</plugin>
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.25</version>
<executions>
<execution>
<id>c</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/specs/TS29508_Nsmf_EventExposure.yaml</inputSpec>
<language>java</language>
<modelPackage>generated.model</modelPackage>
<apiPackage>generated.api</apiPackage>
<invokerPackage>generated.invoker</invokerPackage>
<configOptions>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<artifactVersion>${project.version}</artifactVersion>
<library>okhttp-gson</library>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
可能是什么原因?
解决方法
有两个问题。
-
问题出在 okhttp 上。 Swagger 使用 okhttp 生成客户端,并在 pom.xml 文件中添加了对 okhttp3 的依赖。因此,在生成的文件中,编译器无法解析
import com.squareup.okhttp.OkHttpClient;
,这在构建过程中给了我错误。 我变了<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.1</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>logging-interceptor</artifactId> <version>4.9.1</version> </dependency>
到
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
- 一些 yaml 文件引用了另一个文件,另一个文件引用了更多文件等等。所以我加载了所有的 yaml 文件,甚至那些我不需要的文件。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。