-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dont validate schema properties called 'responses' as responses (#…
…194)
- Loading branch information
Showing
8 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module.exports = (path, isOAS3 = true) => { | ||
const operations = [ | ||
'get', | ||
'put', | ||
'post', | ||
'delete', | ||
'options', | ||
'head', | ||
'patch', | ||
'trace' | ||
]; | ||
|
||
const pathLength = path.length; | ||
|
||
// a necessary but not sufficient check for a response object | ||
// without these, a schema property named "responses" | ||
// may be validated as a response | ||
const isResponsesProperty = path[pathLength - 1] === 'responses'; | ||
|
||
// three scenarios: | ||
// 1) inside of an operation | ||
const isOperationResponse = operations.includes(path[pathLength - 2]); | ||
|
||
// 2) inside of components -> responses (oas 3) | ||
const isResponseInComponents = | ||
pathLength === 2 && path[pathLength - 2] === 'components' && isOAS3; | ||
|
||
// 3) top level responses (swagger 2) | ||
const isTopLevelResponse = pathLength === 1 && !isOAS3; | ||
|
||
return ( | ||
isResponsesProperty && | ||
(isOperationResponse || isResponseInComponents || isTopLevelResponse) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const expect = require('expect'); | ||
const { isResponseObject } = require('../../../src/plugins/utils'); | ||
|
||
describe('is response object - util', () => { | ||
describe('OpenAPI 3', () => { | ||
const isOas3 = true; | ||
|
||
it('should return false for top-level responses objects', () => { | ||
const path = ['responses']; | ||
expect(isResponseObject(path, isOas3)).toBe(false); | ||
}); | ||
|
||
it('should return true for components responses objects', () => { | ||
const path = ['components', 'responses']; | ||
|
||
// the second argument, `isOas3`, is optional. test that optionality | ||
expect(isResponseObject(path)).toBe(true); | ||
}); | ||
|
||
it('should return true for operation responses', () => { | ||
const path = ['paths', '/resource', 'post', 'responses']; | ||
expect(isResponseObject(path, isOas3)).toBe(true); | ||
}); | ||
|
||
it('should return false for non responses', () => { | ||
const path = ['paths', '/resource', 'post', 'parameters']; | ||
expect(isResponseObject(path, isOas3)).toBe(false); | ||
}); | ||
|
||
it('should return false for schemas with properties named responses', () => { | ||
const path = [ | ||
'components', | ||
'schemas', | ||
'MySchema', | ||
'properties', | ||
'responses' | ||
]; | ||
expect(isResponseObject(path, isOas3)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Swagger 2', () => { | ||
const isOas3 = false; | ||
|
||
it('should return true for top-level responses objects', () => { | ||
const path = ['responses']; | ||
expect(isResponseObject(path, isOas3)).toBe(true); | ||
}); | ||
|
||
it('should return false for components responses objects', () => { | ||
const path = ['components', 'responses']; | ||
expect(isResponseObject(path, isOas3)).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters