Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAS 3.0: readOnly properties are included in request body examples #3445

Closed
hkosova opened this issue Jul 24, 2017 · 6 comments
Closed

OAS 3.0: readOnly properties are included in request body examples #3445

hkosova opened this issue Jul 24, 2017 · 6 comments

Comments

@hkosova
Copy link
Contributor

hkosova commented Jul 24, 2017

Version: ft/oas3 branch, commit a1ce0e7

In 3.0 specs, request body examples generated from the schema include read-only properties -- but they should not be included in requests. The issue does not exist in 2.0 specs.

Spec:

openapi: 3.0.0
info:
  version: 0.0.0
  title: test

paths:
  /users:
    post:
      summary: Create a user
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
          example: 5
          readOnly: true
        name:
          type: string
          example: Bob

Expected result: Request body example is:

{
  "name": "Bob"
}

Actual result: Request body example includes the read-only property id:

{
  "id": 5,
  "name": "Bob"
}
@owenconti
Copy link
Contributor

@hkosova Running the latest master locally, this is what I am seeing with your example spec:

screen shot 2017-09-14 at 2 27 32 pm

screen shot 2017-09-14 at 2 27 38 pm

screen shot 2017-09-14 at 2 27 46 pm

I don't see the id property in any of the example objects, however I do see it in the Model tab of the request body field.

@hkosova
Copy link
Contributor Author

hkosova commented Sep 15, 2017

@owenconti, yes, this seems to be fixed already. 🎉
Closing.

@hkosova hkosova closed this as completed Sep 15, 2017
@amochohan
Copy link

amochohan commented Oct 26, 2017

It looks like this is still an issue for readOnly attributes which refer to objects. If I expand on the original example:

paths:
  /users:
    post:
      summary: Create a user
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
          example: 5
          readOnly: true
        name:
          type: string
          example: Bob
        example_attribute:
            type: object
            $ref: '#/components/schemas/AnotherModel'
            readOnly: true

The example_attribute will still be displayed in the requestBody example.

@hkosova
Copy link
Contributor Author

hkosova commented Oct 26, 2017

@amochohan, $ref works by overwriting all of its sibling attributes, so readOnly does not actually have effect in your example. You'll need to use something like:

        example_attribute:
          readOnly: true
          allOf:
            - $ref: '#/components/schemas/AnotherModel'

Here's a related discussion about combining $ref with other attributes: #3325 (comment).

@amochohan
Copy link

Thanks @hkosova, apologies if this wasn't the correct medium through which to have raised this.

@Adeynack
Copy link

Adeynack commented May 8, 2019

EDIT: Nevermind this.

Just realised it is an issue with the SwaggerDoc, not OpenAPI. The online editor display examples properly. The version we have offline does not. My apologies

Original post

I still have the same problem, observed in SwaggerDoc.
I changed some properties to readOnly: true. They disappeared from the POST example, but they still show up in the PATCH one. Since PATCH is a write verb, I expect them not to show up in the example.

Should I post this here, since it is a closed issue (to be reopened) or should I create a new one?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants