redoc icon indicating copy to clipboard operation
redoc copied to clipboard

Properties marked as readOnly are shown in POST payload examples

Open Perni1984 opened this issue 4 years ago • 3 comments

I am using the latest version from I am trying to mark some of my properties in the schema as readOnly:

openapi: 3.0.0
info:
    title: Readonly Example
    version: 0.0.1
servers:
    - url: https://myserver.com/api/v1
      description: Test Environment
paths:
    /backend/article:
        get:
            summary: Returns a list with all articles in the system
            responses:
                '200':
                    description: A list of all event series
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/ArrayOfArticles'
            tags:
                - Backend/Article
        post:
            summary: Creates a new article
            requestBody:
                content:
                    application/json:
                        schema:
                            $ref: '#/components/schemas/Article'
                required: true
            responses:
                '200':
                    description: Success
            tags:
                - Backend/Article
components:
    schemas:
        ArrayOfArticles:
            type: array
            items:
                $ref: '#/components/schemas/Article'
        Article:
            type: object
            properties:
                id:
                    type: string
                    readOnly: true
                title:
                    type: string
                content:
                    type: string
                createdAt:
                    type: string
                    readOnly: true
                updatedAt:
                    type: string
                    readOnly: true
            required:
                - id
                - title
                - content
                - createdAt
                - updatedAt
            example:
                id: 1e8b472d03s6ae24457f15a2
                title: My Article 01
                content: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean arcu arcu, finibus sed mattis in, placerat in justo. Proin feugiat semper laoreet.
                updatedAt: 2020-04-06T17:14:37+02:00
                createdAt: 2020-04-06T17:14:37+02:00

This gets rendered as follows: image

In the POST request body schema the properties id, updatedAt, createdAt are correctly stripped out. image

Unfortunately the sample values are getting displayed in the sample payload of the POST request. image

My expectation would be that those properties are getting stripped out from the sample payload in the POST request aswell. Am I missing something?

Perni1984 avatar Apr 21 '20 05:04 Perni1984

A viable workaround as for now seems to be to provide the examples directly in the GET response and POST request definitions and not in the components section:

openapi: 3.0.0
info:
    title: Readonly Example
    version: 0.0.1
servers:
    - url: https://myserver.com/api/v1
      description: Test Environment
paths:
    /backend/article:
        get:
            summary: Returns a list with all articles in the system
            responses:
                '200':
                    description: A list of all event series
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/ArrayOfArticles'
                            example:
                                id: 1e8b472d03s6ae24457f15a2
                                title: My Article 01
                                content: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean arcu arcu, finibus sed mattis in, placerat in justo. Proin feugiat semper laoreet.
                                updatedAt: 2020-04-06T17:14:37+02:00
                                createdAt: 2020-04-06T17:14:37+02:00
            tags:
                - Backend/Article
        post:
            summary: Creates a new article
            requestBody:
                content:
                    application/json:
                        schema:
                            $ref: '#/components/schemas/Article'
                        example:
                            title: My Article 01
                            content: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean arcu arcu, finibus sed mattis in, placerat in justo. Proin feugiat semper laoreet.
                required: true
            responses:
                '200':
                    description: Success
            tags:
                - Backend/Article
components:
    schemas:
        ArrayOfArticles:
            type: array
            items:
                $ref: '#/components/schemas/Article'
        Article:
            type: object
            properties:
                id:
                    type: string
                    readOnly: true
                title:
                    type: string
                content:
                    type: string
                createdAt:
                    type: string
                    readOnly: true
                updatedAt:
                    type: string
                    readOnly: true
            required:
                - id
                - title
                - content
                - createdAt
                - updatedAt

image image

The POST request payload example now correctly does not render id, updatedAt and createdAt fields: image image

Perni1984 avatar Apr 21 '20 06:04 Perni1984

@Perni1984 when there is a implicit example on a schema Redoc uses it directly without any changes.

As a workaround, you can define examples on each field vs on the schema.

RomanHotsiy avatar May 09 '20 12:05 RomanHotsiy

~~I created a fix for this at https://github.com/Redocly/openapi-sampler/pull/139 if somebody is still waiting for it.~~

~~Edit: it seems to me that this was already fixed. I thought I fixed it with my PR coincidentally. That was wrong. So this Issue can actually be closed.~~

NickUfer avatar Jun 11 '22 23:06 NickUfer