-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-271991: IPA-104: Validate for Get methods the response is 200 #462
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
Changes from 10 commits
0f7c74c
34bd1b6
877ab05
26c1f80
f8f0928
d6af94f
42469fe
6d8f55c
bd7456a
12003f8
d210b0f
62be91a
99de49f
97c5bbf
7a1f68d
cadb9ca
586eede
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [ | ||
| { | ||
| name: 'valid methods', | ||
| document: { | ||
| paths: { | ||
| '/resource': { | ||
| get: { | ||
| responses: { | ||
| 200: {}, | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| '/resource/{id}': { | ||
| get: { | ||
| responses: { | ||
| 200: {}, | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| '/resource/{id}:customMethod': { | ||
| get: { | ||
| responses: { | ||
| 200: {}, | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| '/singleton': { | ||
| get: { | ||
| responses: { | ||
| 200: {}, | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid methods', | ||
| document: { | ||
| paths: { | ||
| '/resource1/{id}': { | ||
| get: { | ||
| responses: { | ||
| 201: {}, | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| '/resource2/{id}': { | ||
| get: { | ||
| responses: { | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| '/resource3/{id}': { | ||
| get: { | ||
| responses: { | ||
| 200: {}, | ||
| 201: {}, | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-104-get-method-response-code-is-200-OK', | ||
| message: | ||
| 'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/104', | ||
| path: ['paths', '/resource1/{id}', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-104-get-method-response-code-is-200-OK', | ||
| message: | ||
| 'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/104', | ||
| path: ['paths', '/resource2/{id}', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-104-get-method-response-code-is-200-OK', | ||
| message: | ||
| 'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/104', | ||
| path: ['paths', '/resource3/{id}', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid method with exception', | ||
| document: { | ||
| paths: { | ||
| '/resource1/{id}': { | ||
| get: { | ||
| responses: { | ||
| 201: {}, | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-104-get-method-response-code-is-200-OK': 'reason', | ||
| }, | ||
| }, | ||
| }, | ||
| '/resource2/{id}': { | ||
| get: { | ||
| responses: { | ||
| 400: {}, | ||
| 500: {}, | ||
| }, | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-104-get-method-response-code-is-200-OK': 'reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| ]); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-104-get-method-response-code-is-200-OK'; | ||
| const ERROR_MESSAGE = | ||
| 'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code.'; | ||
|
|
||
| export default (input, _, { path }) => { | ||
| if (hasException(input, RULE_NAME)) { | ||
| collectException(input, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| if (input['responses']) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something to consider here is that this test will cover IPA 105 List as well, since it's also a HTTP Also, this covers custom methods too, though IIRC we don't define that custom method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I modified the code as "If it is a custom method or collection resource (not a child with current glossary), the rule will be bypassed". Let me know what you think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
| const responses = input['responses']; | ||
|
|
||
| // If there is no 200 response, return a violation | ||
| if (!responses['200']) { | ||
| return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE); | ||
| } | ||
|
|
||
| // If there are other 2xx responses that are not 200, return a violation | ||
| if (Object.keys(responses).some((key) => key.startsWith('2') && key !== '200')) { | ||
| return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE); | ||
| } | ||
| } | ||
|
|
||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.