question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

@Setter
@Getter
@ApiModel(description = "Character model")
public class Character {

    @ApiModelProperty(readOnly = true)
    private Long id;

    @ApiModelProperty(readOnly = true)
    private SearchAnnouncement searchAnnouncement;
}

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 hidden = 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?

Q&A (please complete the following information)

  • OS: [e.g. windows]
  • Browser: [Chrome ]
  • Version: [74]
  • Swagger-UI version: [2.9.2]

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:10
  • Comments:10

github_iconTop GitHub Comments

4reactions
ospikovetscommented, May 8, 2020

Is there any way to remove readOnly property from the UI Request body -> Schema section?

Even though id has readOnly notation, it is still weird to have it in the request body schema. image

1reaction
flmelodycommented, Jul 12, 2021

I eventually got around this by creating multiple schemas per model: read, write, add and edit. These were hidden from swagger by placing them in a vendor location like #/x-my-application/components/schemas.

hidden schemas:

      Actor-Write:
        properties:
          first_name:
            minLength: 1
            maxLength: 64
            description: Actor First Name
            type: string
          last_name:
            minLength: 1
            maxLength: 64
            description: Actor Last Name
            type: string
        description: Actor Entity
        type: object
      Actor-Add:
        required:
          - first_name
          - last_name
        allOf:
          - $ref: '#/x-swagger-bake/components/schemas/Actor-Write'
        description: Actor Entity
        type: object
      Actor-Edit:
        allOf:
          - $ref: '#/x-swagger-bake/components/schemas/Actor-Write'
        description: Actor Entity
        type: object

regular schema:

    Actor:
      properties:
        id:
          readOnly: true
          type: integer
          format: int64
        first_name:
          minLength: 1
          maxLength: 64
          description: Actor First Name
          type: string
        last_name:
          minLength: 1
          maxLength: 64
          description: Actor Last Name
          type: string
        modified:
          readOnly: true
          minLength: 1
          description: Last modified date/time
          type: string
          format: date-time
      description: Actor Entity
      type: object

request body:

      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/x-swagger-bake/components/schemas/FilmActor-Add'
          application/json:
            schema:
              $ref: '#/x-swagger-bake/components/schemas/FilmActor-Add'
          application/xml:
            schema:
              $ref: '#/x-swagger-bake/components/schemas/FilmActor-Add'

This is a bit of work to do manually so I built this into a library that does the work for me: https://github.com/cnizzardini/cakephp-swagger-bake and a demo is available here: https://demo.mixerapi.com/admin

thanks for reply,I may create another model for it current time,your theme color is beautiful!

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Swagger-ui does not hide readOnly nested object from ...
Currently I'm trying to hide nested object from request body, but it still shows in example json on swagger-ui page.
Read more >
Hide a Request Field in Swagger API - Baeldung
A quick and practical guide to hiding a request field in Swagger UI.
Read more >
Swagger RESTful API Documentation Specification
The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be ......
Read more >
Springfox Reference Documentation - GitHub Pages
For non-boot applications springfox-swagger-ui is no longer automatically ... we use spring-restdocs to provide response body examples.
Read more >
Jackson Readonly properties and swagger UI
Sometimes when you are building a REST API, you don't want the user to be able to set some specific p... Tagged with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found