Skip to content

Commit

Permalink
fix: soften the response-example-provided rule to look only at succes…
Browse files Browse the repository at this point in the history
…s responses (#273)

Purpose:
- Do not necessarily expect response examples for non-2xx and default responses. Expected for 2xx responses.

Changes:
- Soften the check to look only at success responses.

Tests:
- Update tests to ensure errors only issued for success responses without examples.
  • Loading branch information
barrett-schonefeld committed Apr 14, 2021
1 parent 8d86983 commit b5dac3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/spectral/rulesets/.defaultsForSpectral.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ rules:
# custom Spectral rule to ensure response example provided
response-example-provided:
message: "{{error}}"
given: $.paths[*][*].responses[*].content.application/json
given: $.paths[*][*].responses[?(@property >= 200 && @property < 300)].content.application/json
severity: warn
resolved: true
then:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('spectral - test validation that schema provided in content object', fu
expect(expectedWarnings.length).toBe(0);
});

it('should error when a response example is not provided', async () => {
it('should only error when a response example is not provided in a success response', async () => {
const spec = {
openapi: '3.0.0',
paths: {
Expand All @@ -78,6 +78,26 @@ describe('spectral - test validation that schema provided in content object', fu
}
}
}
},
'300': {
content: {
'application/json': {
// schema provided
schema: {
type: 'string'
}
}
}
},
default: {
content: {
'application/json': {
// schema provided
schema: {
type: 'string'
}
}
}
}
}
}
Expand Down

0 comments on commit b5dac3e

Please sign in to comment.