Swagger-ui does not hide readOnly nested object from example body

喜夏-厌秋 提交于 2020-06-27 08:41:35

问题


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;
}

Note: @Data is Lombok annotation that generates getters, setters and few other things

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.

I tried:
  • using readOnly = true
  • using accessMode = ApiModelProperty.AccessMode.READ_ONLY
  • using @Setter(AccessLevel.NONE), which prevents generation of setter for that field, as I read somewhere that something is checking if setter exist and sets readOnly based on that
  • different combinations of above

but in all cases example json looks like this (id hidden, and searchAnnouncement visible):

{
    "searchAnnouncement": {
         "id": 0,
    },
}

So in short my question is:

Is it possible to hide from example json nested object?

EDIT: 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


回答1:


I had the same issue, I fixed it by setting jsonIgnore on my setters. I'm using dependency version 2.9.2.



来源:https://stackoverflow.com/questions/53454357/swagger-ui-does-not-hide-readonly-nested-object-from-example-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!