Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/spicy-melons-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": patch
"@redocly/cli": patch
---

Fixed an issue where the `no-invalid-media-type-examples` rule did not respect `readOnly` and `writeOnly` properties in schemas.
Original file line number Diff line number Diff line change
Expand Up @@ -734,4 +734,392 @@ describe('no-invalid-media-type-examples', () => {
]
`);
});

describe('readonly and writeonly properties', () => {
it('should not report if examples respect readonly and writeonly properties', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.1.0
paths:
/inline-examples:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
value:
id: 1
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
value:
password: secret
/inline-example:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
example:
id: 1
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
example:
password: secret
/ref:
get:
responses:
'200':
$ref: '#/components/responses/ObjectResponse'
post:
requestBody:
$ref: '#/components/requestBodies/ObjectRequest'
components:
schemas:
Object:
type: object
properties:
id:
type: integer
readOnly: true
password:
type: string
writeOnly: true
required:
- id
- password
responses:
ObjectResponse:
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
value:
id: 1
requestBodies:
ObjectRequest:
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
value:
password: secret
`,
'foobar.yaml'
);

const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await createConfig({
rules: {
'no-invalid-media-type-examples': 'error',
},
}),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`);
});

it('should not report if examples respect readonly and writeonly properties with referenced examples', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.1.0
paths:
/objects:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
$ref: '#/components/examples/ObjectResponse'
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
$ref: '#/components/examples/ObjectRequest'
components:
examples:
ObjectRequest:
value:
password: secret
ObjectResponse:
value:
id: 1
schemas:
Object:
type: object
properties:
id:
type: integer
readOnly: true
password:
type: string
writeOnly: true
required:
- id
- password
`,
'foobar.yaml'
);

const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await createConfig({
rules: {
'no-invalid-media-type-examples': 'error',
},
}),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`);
});

it('should not report if examples respect readonly and writeonly properties with nested referenced schemas', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.1.0
paths:
/objects:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
value:
id: 1
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
value:
password: "secret"
components:
schemas:
Object:
type: object
properties:
id:
$ref: '#/components/schemas/Id'
password:
$ref: '#/components/schemas/Password'
CyclicRef:
$ref: '#/components/schemas/Object'
required:
- id
- password
Id:
type: integer
readOnly: true
Password:
type: string
writeOnly: true
`,
'foobar.yaml'
);

const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await createConfig({
rules: {
'no-invalid-media-type-examples': 'error',
},
}),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`);
});

it('should report if examples include contextually invalid values', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.1.0
paths:
/inline-examples:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
value:
id: 1
# invalid: password is writeOnly
password: secret
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
examples:
example1:
value:
# invalid: id is readOnly
id: 1
password: secret
/inline-example:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
example:
id: 1
# invalid: password is writeOnly
password: secret
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
example:
# invalid: id is readOnly
id: 1
password: secret
components:
schemas:
Object:
type: object
properties:
id:
type: integer
readOnly: true
password:
type: string
writeOnly: true
required:
- id
- password
`,
'foobar.yaml'
);

const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await createConfig({
rules: {
'no-invalid-media-type-examples': 'error',
},
}),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
[
{
"from": {
"pointer": "#/paths/~1inline-examples/get/responses/200/content/application~1json",
"source": "foobar.yaml",
},
"location": [
{
"pointer": "#/paths/~1inline-examples/get/responses/200/content/application~1json/examples/example1/value/password",
"reportOnKey": true,
"source": "foobar.yaml",
},
],
"message": "Example value must conform to the schema: must NOT have unevaluated properties \`password\`.",
"ruleId": "no-invalid-media-type-examples",
"severity": "error",
"suggest": [],
},
{
"from": {
"pointer": "#/paths/~1inline-examples/post/requestBody/content/application~1json",
"source": "foobar.yaml",
},
"location": [
{
"pointer": "#/paths/~1inline-examples/post/requestBody/content/application~1json/examples/example1/value/id",
"reportOnKey": true,
"source": "foobar.yaml",
},
],
"message": "Example value must conform to the schema: must NOT have unevaluated properties \`id\`.",
"ruleId": "no-invalid-media-type-examples",
"severity": "error",
"suggest": [],
},
{
"from": {
"pointer": "#/paths/~1inline-example/get/responses/200/content/application~1json",
"source": "foobar.yaml",
},
"location": [
{
"pointer": "#/paths/~1inline-example/get/responses/200/content/application~1json/example/password",
"reportOnKey": true,
"source": "foobar.yaml",
},
],
"message": "Example value must conform to the schema: must NOT have unevaluated properties \`password\`.",
"ruleId": "no-invalid-media-type-examples",
"severity": "error",
"suggest": [],
},
{
"from": {
"pointer": "#/paths/~1inline-example/post/requestBody/content/application~1json",
"source": "foobar.yaml",
},
"location": [
{
"pointer": "#/paths/~1inline-example/post/requestBody/content/application~1json/example/id",
"reportOnKey": true,
"source": "foobar.yaml",
},
],
"message": "Example value must conform to the schema: must NOT have unevaluated properties \`id\`.",
"ruleId": "no-invalid-media-type-examples",
"severity": "error",
"suggest": [],
},
]
`);
});
});
});
Loading