我想在我的 API 中定义两种响应:一种是在我返回域对象时成功,另一种是在错误情况下。
遵循此处的文档:https ://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/
我首先测试了文档中给出的示例并将其添加到我的.yaml
文件中:
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
responses:
'200':
description: Updated
components:
schemas:
Dog:
type: object
properties:
bark:
type: boolean
breed:
type: string
enum: [Dingo, Husky, Retriever, Shepherd]
Cat:
type: object
properties:
hunts:
type: boolean
age:
type: integer
编译时出现以下错误:
cannot find symbol [ERROR] symbol: class OneOfCatDog
似乎它在我的组件中期待一个模式,OneOfCatDog
这正是我试图不拥有的。
有人知道有什么问题吗?
更新:代码生成
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.2.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatorName>spring</generatorName>
<inputSpec>${project.basedir}/src/main/resources/static/MyApi.yaml</inputSpec>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<openApiNullable>false</openApiNullable>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>