-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-287245: IPA-109: Validate custom method must be GET or POST #313
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f3ae4cc
CLOUDP-287245: IPA-109: Validate custom method must be GET or POST
yelizhenden-mdb 6abdcb0
Merge branch 'main' into CLOUDP-287245
yelizhenden-mdb 2d1b379
prettier fixes
yelizhenden-mdb b16f606
import fix
yelizhenden-mdb 60c888c
rule fix
yelizhenden-mdb 1d0d868
address the comments
yelizhenden-mdb 2afb4c9
prettier fix
yelizhenden-mdb 5b191b2
address the comments
yelizhenden-mdb 439edfc
address the comments
yelizhenden-mdb a485904
prettier fix
yelizhenden-mdb 74cc372
address the comments
yelizhenden-mdb 5b05efc
Merge branch 'main' into CLOUDP-287245
yelizhenden-mdb df5a94a
prettier fix
yelizhenden-mdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js
This file contains hidden or 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,114 @@ | ||
import testRule from './__helpers__/testRule'; | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
||
testRule('xgen-IPA-109-custom-method-must-be-GET-or-POST', [ | ||
{ | ||
name: 'valid methods', | ||
document: { | ||
paths: { | ||
'/a/{exampleId}:method': { | ||
post: {}, | ||
}, | ||
'/a:method': { | ||
post: {}, | ||
}, | ||
'/b/{exampleId}:method': { | ||
get: {}, | ||
}, | ||
'/b:method': { | ||
get: {}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'invalid methods', | ||
document: { | ||
paths: { | ||
'/a/{exampleId}:method': { | ||
put: {}, | ||
}, | ||
'/a:method': { | ||
put: {}, | ||
}, | ||
'/b/{exampleId}:method': { | ||
get: {}, | ||
put: {}, | ||
}, | ||
'/b:method': { | ||
get: {}, | ||
put: {}, | ||
}, | ||
'/c/{exampleId}:method': { | ||
post: {}, | ||
get: {}, | ||
put: {}, | ||
}, | ||
'/c:method': { | ||
post: {}, | ||
get: {}, | ||
put: {}, | ||
}, | ||
'/d/{exampleId}:method': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/d:method': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST', | ||
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109', | ||
path: ['paths', '/a/{exampleId}:method'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST', | ||
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109', | ||
path: ['paths', '/a:method'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST', | ||
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109', | ||
path: ['paths', '/b/{exampleId}:method'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST', | ||
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109', | ||
path: ['paths', '/b:method'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST', | ||
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109', | ||
path: ['paths', '/c/{exampleId}:method'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST', | ||
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109', | ||
path: ['paths', '/c:method'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST', | ||
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109', | ||
path: ['paths', '/d/{exampleId}:method'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST', | ||
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109', | ||
path: ['paths', '/d:method'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
]); |
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
extends: | ||
- ./rulesets/IPA-102.yaml | ||
- ./rulesets/IPA-104.yaml | ||
- ./rulesets/IPA-109.yaml |
This file contains hidden or 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,14 @@ | ||
# IPA-109: Custom Methods | ||
# http://go/ipa/109 | ||
|
||
functions: | ||
- eachCustomMethodMustBeGetOrPost | ||
|
||
rules: | ||
xgen-IPA-109-custom-method-must-be-GET-or-POST: | ||
description: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109' | ||
message: '{{error}} http://go/ipa/109' | ||
severity: warn | ||
given: '$.paths[*]' | ||
then: | ||
function: 'eachCustomMethodMustBeGetOrPost' |
26 changes: 26 additions & 0 deletions
26
tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js
This file contains hidden or 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,26 @@ | ||
import { isCustomMethod } from './utils/resourceEvaluation.js'; | ||
|
||
const ERROR_MESSAGE = 'The HTTP method for custom methods must be GET or POST.'; | ||
const ERROR_RESULT = [{ message: ERROR_MESSAGE }]; | ||
const VALID_METHODS = ['get', 'post']; | ||
|
||
export default (input, opts, { path }) => { | ||
// Extract the path key (e.g., '/a/{exampleId}:method') from the JSONPath. | ||
let pathKey = path[1]; | ||
|
||
if (!isCustomMethod(pathKey)) return; | ||
|
||
const httpMethods = Object.keys(input); | ||
|
||
// Check for invalid methods | ||
if (httpMethods.some((method) => !VALID_METHODS.includes(method))) { | ||
return ERROR_RESULT; | ||
} | ||
|
||
// Check for multiple valid methods | ||
const validMethodCount = httpMethods.filter((method) => VALID_METHODS.includes(method)).length; | ||
|
||
if (validMethodCount > 1) { | ||
return ERROR_RESULT; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this return error if the path has an exception extension?
Suggestion: use
$.path[get,put,post,delete,options,head,patch,trace]
to ignore any other objects than the methods, kinda like we do here:openapi/tools/spectral/.spectral.yaml
Line 9 in 98f5b23
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it would return error with an exception extension. Good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"#PathItem[get,put,post,delete,options,head,patch,trace]"
gives the object inside HTTP method. I will excludex-xgen-IPA-exception