gpt4 book ai didi

java - SWAGGER 2 从同一基础对象继承请求和响应对象

转载 作者:行者123 更新时间:2023-12-02 10:32:03 25 4
gpt4 key购买 nike

在使用 Swagger 2.0 设计的 Spring API 中,我尝试使用 swagger 创建继承。我想创建一个基础对象,它具有请求和响应对象的共同属性。我尝试像下面的例子那样做:

CategoryResponse:
allOf:
- $ref: '#/definitions/Category'
- type: object
properties:
id:
type: integer
example: '1'
CategoryRequest:
type: object
allOf:
- $ref: '#/definitions/Category'
Category:
discriminator: nameCategory
type: object
properties:
nameCategory:
type: string
example: Games

问题是我在尝试 POST 或 PUT 新的 CategoryRequest 对象时收到错误请求错误。它甚至没有到达 API Controller ,所以我猜问题可能出在上面的模型定义中。我尝试了很多变体,但没有一个有效。但是,当我尝试获取类别列表或按 id 获取一个类别时,我能够这样做(我的 CategoryResponse 正在工作并很好地扩展了类别)。

有谁知道使用通用基础模型的继承来创建此结构的正确方法(对于请求和响应对象)?

提前致谢!

最佳答案

id 看起来像一个自动生成的只读属性。在这种情况下,您不需要继承 - 您可以使用单个 Category 架构并将 id 标记为 readOnly: true

Category:
type: object
properties:
nameCategory:
type: string
example: Games
id:
type: integer
readOnly: true # <-----
example: 1

来自OpenAPI Specification :

readOnly

Declares the property as "read only". This means that it MAY be sent as part of a response but MUST NOT be sent as part of the request.

关于java - SWAGGER 2 从同一基础对象继承请求和响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com