Swagger-ui 不会从示例正文中隐藏只读嵌套对象 [英] Swagger-ui does not hide readOnly nested object from example body
问题描述
我正在使用 swagger-ui 和 lombok 开发 spring-boot 应用程序.目前我正在尝试从请求正文中隐藏嵌套对象,但它仍然显示在 swagger-ui 页面上的示例 json 中.
I'm developing spring-boot app with swagger-ui and lombok. Currently I'm trying to hide nested object from request body, but it still shows in example json on swagger-ui page.
我的课程带有注释(简化为仅相关内容):
I have my class with annotations(simplified to only related stuff):
@Data
@ApiModel(description = "Character model")
public class Character {
@ApiModelProperty(readOnly = true)
private Long id;
@ApiModelProperty(readOnly = true)
private SearchAnnouncement searchAnnouncement;
}
注意:@Data 是 Lombok 注释,用于生成 getter、setter 和其他一些东西
Note: @Data is Lombok annotation that generates getters, setters and few other things
当我在 swagger 页面id"上访问示例模型时;归档正确隐藏在示例 json 中,并在响应模型中可见.但是搜索公告"未隐藏在示例 json 中.
When I access example model on swagger page "id" filed is properly hidden in example json, and visible in response model. But "searchAnnouncement" is not hidden in example json.
- 使用 readOnly = true
- 使用 accessMode = ApiModelProperty.AccessMode.READ_ONLY
- 使用@Setter(AccessLevel.NONE),这会阻止为该字段生成 setter,因为我在某处读到某处正在检查 setter 是否存在并根据该设置 readOnly
- 以上的不同组合
但在所有情况下,示例 json 看起来像这样(隐藏了 id,并且可以看到 searchAnnouncement):
but in all cases example json looks like this (id hidden, and searchAnnouncement visible):
{
"searchAnnouncement": {
"id": 0,
},
}
简而言之,我的问题是:
So in short my question is:
忘了提我目前有 swagger2 &在我的依赖项中使用 2.9.2 版本的 swagger-ui,但我从 2.8.0 升级希望隐藏这个嵌套对象
Forgot to mention I currently have swagger2 & swagger-ui with version 2.9.2 in my dependencies, but I moved up from 2.8.0 hoping to hide this nested object
推荐答案
我遇到了同样的问题,我通过在我的 setter 上设置 jsonIgnore
来修复它.我使用的是依赖版本 2.9.2.
I had the same issue, I fixed it by setting jsonIgnore
on my setters. I'm using dependency version 2.9.2.
这篇关于Swagger-ui 不会从示例正文中隐藏只读嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!