Version: ft/oas3 branch, commit a1ce0e7b3c54d66534faf59599c6bce5f23656f2
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"
}
@hkosova Running the latest master locally, this is what I am seeing with your example spec:



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.
@owenconti, yes, this seems to be fixed already. :tada:
Closing.
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.
@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: https://github.com/swagger-api/swagger-ui/issues/3325#issuecomment-334139931.
Thanks @hkosova, apologies if this wasn't the correct medium through which to have raised 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
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?
Most helpful comment
@amochohan,
$refworks by overwriting all of its sibling attributes, soreadOnlydoes not actually have effect in your example. You'll need to use something like:Here's a related discussion about combining
$refwith other attributes: https://github.com/swagger-api/swagger-ui/issues/3325#issuecomment-334139931.