From f3ae4cc7d767e6f0fb89eb0c21390cd3d83102e4 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Tue, 17 Dec 2024 16:18:11 +0000 Subject: [PATCH 01/11] CLOUDP-287245: IPA-109: Validate custom method must be GET or POST --- .../eachCustomMethodMustBeGetOrPost.test.js | 114 ++++++++++++++++++ tools/spectral/ipa/ipa-spectral.yaml | 1 + tools/spectral/ipa/rulesets/IPA-109.yaml | 19 +++ .../eachCustomMethodMustBeGetOrPost.js | 25 ++++ 4 files changed, 159 insertions(+) create mode 100644 tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js create mode 100644 tools/spectral/ipa/rulesets/IPA-109.yaml create mode 100644 tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js diff --git a/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js b/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js new file mode 100644 index 0000000000..308ec2cac6 --- /dev/null +++ b/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js @@ -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, + }, + ], + }, +]); diff --git a/tools/spectral/ipa/ipa-spectral.yaml b/tools/spectral/ipa/ipa-spectral.yaml index 893169f21d..66f816c0f2 100644 --- a/tools/spectral/ipa/ipa-spectral.yaml +++ b/tools/spectral/ipa/ipa-spectral.yaml @@ -1,2 +1,3 @@ extends: - ./rulesets/IPA-104.yaml + - ./rulesets/IPA-109.yaml diff --git a/tools/spectral/ipa/rulesets/IPA-109.yaml b/tools/spectral/ipa/rulesets/IPA-109.yaml new file mode 100644 index 0000000000..4487ff38ac --- /dev/null +++ b/tools/spectral/ipa/rulesets/IPA-109.yaml @@ -0,0 +1,19 @@ +# 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: + field: "@key" + function: "eachCustomMethodMustBeGetOrPost" + functionOptions: + requiredMethods: + - get + - post diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js new file mode 100644 index 0000000000..cc230caecd --- /dev/null +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -0,0 +1,25 @@ +import { isCustomMethod } from './utils/resourceEvaluation'; + +const ERROR_MESSAGE = 'The HTTP method for custom methods must be GET or POST.'; +const ERROR_RESULT = [{ message: ERROR_MESSAGE }]; +const validMethods = ["get", "post"]; + +export default (input, _, { documentInventory }) => { + if(!isCustomMethod(input)) { + return; + } + + const oas = documentInventory.resolved; + let httpMethods = Object.keys(oas.paths[input]); + + const invalidMethodsFound = httpMethods.some((key) => !validMethods.includes(key)); + if(invalidMethodsFound) { + return ERROR_RESULT; + } + + const validMethodsFound = httpMethods.filter((key) => validMethods.includes(key)); + if(validMethodsFound.length > 1) { + return ERROR_RESULT; + } + +} \ No newline at end of file From 2d1b3790fd10acc012a80f15ac1a4c5b5bbf3431 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Tue, 17 Dec 2024 17:14:22 +0000 Subject: [PATCH 02/11] prettier fixes --- .../__tests__/eachCustomMethodMustBeGetOrPost.test.js | 2 +- tools/spectral/ipa/rulesets/IPA-109.yaml | 10 +++++----- .../functions/eachCustomMethodMustBeGetOrPost.js | 11 +++++------ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js b/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js index 308ec2cac6..e48ec6e579 100644 --- a/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js +++ b/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js @@ -17,7 +17,7 @@ testRule('xgen-IPA-109-custom-method-must-be-GET-or-POST', [ }, '/b:method': { get: {}, - } + }, }, }, errors: [], diff --git a/tools/spectral/ipa/rulesets/IPA-109.yaml b/tools/spectral/ipa/rulesets/IPA-109.yaml index 4487ff38ac..74131ae77a 100644 --- a/tools/spectral/ipa/rulesets/IPA-109.yaml +++ b/tools/spectral/ipa/rulesets/IPA-109.yaml @@ -6,13 +6,13 @@ functions: 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" + 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" + given: '$.paths' then: - field: "@key" - function: "eachCustomMethodMustBeGetOrPost" + field: '@key' + function: 'eachCustomMethodMustBeGetOrPost' functionOptions: requiredMethods: - get diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js index cc230caecd..47f4b742e2 100644 --- a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -2,10 +2,10 @@ import { isCustomMethod } from './utils/resourceEvaluation'; const ERROR_MESSAGE = 'The HTTP method for custom methods must be GET or POST.'; const ERROR_RESULT = [{ message: ERROR_MESSAGE }]; -const validMethods = ["get", "post"]; +const validMethods = ['get', 'post']; export default (input, _, { documentInventory }) => { - if(!isCustomMethod(input)) { + if (!isCustomMethod(input)) { return; } @@ -13,13 +13,12 @@ export default (input, _, { documentInventory }) => { let httpMethods = Object.keys(oas.paths[input]); const invalidMethodsFound = httpMethods.some((key) => !validMethods.includes(key)); - if(invalidMethodsFound) { + if (invalidMethodsFound) { return ERROR_RESULT; } const validMethodsFound = httpMethods.filter((key) => validMethods.includes(key)); - if(validMethodsFound.length > 1) { + if (validMethodsFound.length > 1) { return ERROR_RESULT; } - -} \ No newline at end of file +}; From b16f6066f7937a6e3e78a3480117f1bf9a77f86e Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Tue, 17 Dec 2024 17:15:11 +0000 Subject: [PATCH 03/11] import fix --- .../ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js index 47f4b742e2..467c813dc5 100644 --- a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -1,4 +1,4 @@ -import { isCustomMethod } from './utils/resourceEvaluation'; +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 }]; From 60c888c493604974ec53cba910e2c625906f92d8 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Tue, 17 Dec 2024 17:19:25 +0000 Subject: [PATCH 04/11] rule fix --- tools/spectral/ipa/rulesets/IPA-109.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tools/spectral/ipa/rulesets/IPA-109.yaml b/tools/spectral/ipa/rulesets/IPA-109.yaml index 74131ae77a..0c6d86a4a0 100644 --- a/tools/spectral/ipa/rulesets/IPA-109.yaml +++ b/tools/spectral/ipa/rulesets/IPA-109.yaml @@ -13,7 +13,3 @@ rules: then: field: '@key' function: 'eachCustomMethodMustBeGetOrPost' - functionOptions: - requiredMethods: - - get - - post From 1d0d86880dfca2b4a5034ba4e5941e7c1656705c Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 18 Dec 2024 14:23:35 +0000 Subject: [PATCH 05/11] address the comments --- tools/spectral/ipa/rulesets/IPA-109.yaml | 1 - .../eachCustomMethodMustBeGetOrPost.js | 42 ++++++++++++------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/tools/spectral/ipa/rulesets/IPA-109.yaml b/tools/spectral/ipa/rulesets/IPA-109.yaml index 0c6d86a4a0..fc52c938b5 100644 --- a/tools/spectral/ipa/rulesets/IPA-109.yaml +++ b/tools/spectral/ipa/rulesets/IPA-109.yaml @@ -11,5 +11,4 @@ rules: severity: warn given: '$.paths' then: - field: '@key' function: 'eachCustomMethodMustBeGetOrPost' diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js index 467c813dc5..ee294c8609 100644 --- a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -1,24 +1,36 @@ 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 validMethods = ['get', 'post']; +const VALID_METHODS = ['get', 'post']; -export default (input, _, { documentInventory }) => { - if (!isCustomMethod(input)) { - return; - } +export default (paths) => { + // Collect all errors + const errors = []; - const oas = documentInventory.resolved; - let httpMethods = Object.keys(oas.paths[input]); + // Iterate through each path key and its corresponding path item + for (const [pathKey, pathItem] of Object.entries(paths)) { + // Skip if not a custom method + if (!isCustomMethod(pathKey)) continue; - const invalidMethodsFound = httpMethods.some((key) => !validMethods.includes(key)); - if (invalidMethodsFound) { - return ERROR_RESULT; - } + // Get HTTP methods for this path + const httpMethods = Object.keys(pathItem); + + // Check for invalid methods + if (httpMethods.some(method => !VALID_METHODS.includes(method))) { + errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE }); + continue; + } - const validMethodsFound = httpMethods.filter((key) => validMethods.includes(key)); - if (validMethodsFound.length > 1) { - return ERROR_RESULT; + // Check for multiple valid methods + const validMethodCount = httpMethods.filter(method => + VALID_METHODS.includes(method) + ).length; + + if (validMethodCount > 1) { + errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE }); + } } + + return errors; + }; From 2afb4c9129b6519257f1cebadb1ba94c658f20b4 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 18 Dec 2024 14:24:30 +0000 Subject: [PATCH 06/11] prettier fix --- .../rulesets/functions/eachCustomMethodMustBeGetOrPost.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js index ee294c8609..0ea737909f 100644 --- a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -16,15 +16,13 @@ export default (paths) => { const httpMethods = Object.keys(pathItem); // Check for invalid methods - if (httpMethods.some(method => !VALID_METHODS.includes(method))) { + if (httpMethods.some((method) => !VALID_METHODS.includes(method))) { errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE }); continue; } // Check for multiple valid methods - const validMethodCount = httpMethods.filter(method => - VALID_METHODS.includes(method) - ).length; + const validMethodCount = httpMethods.filter((method) => VALID_METHODS.includes(method)).length; if (validMethodCount > 1) { errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE }); @@ -32,5 +30,4 @@ export default (paths) => { } return errors; - }; From 5b191b2fdc74c183aaeb545099f1de34c754ebda Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 18 Dec 2024 15:40:14 +0000 Subject: [PATCH 07/11] address the comments --- tools/spectral/ipa/rulesets/IPA-109.yaml | 2 +- .../eachCustomMethodMustBeGetOrPost.js | 35 ++++++++----------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/tools/spectral/ipa/rulesets/IPA-109.yaml b/tools/spectral/ipa/rulesets/IPA-109.yaml index fc52c938b5..48c1645e07 100644 --- a/tools/spectral/ipa/rulesets/IPA-109.yaml +++ b/tools/spectral/ipa/rulesets/IPA-109.yaml @@ -9,6 +9,6 @@ rules: 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' + given: '$.paths[*]' then: function: 'eachCustomMethodMustBeGetOrPost' diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js index 0ea737909f..0a77cc725a 100644 --- a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -1,33 +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 (paths) => { - // Collect all errors - const errors = []; +export default (input, opts, { path }) => { + // Extract the path key (e.g., '/a/{exampleId}:method') from the JSONPath. + let pathKey = path[1]; - // Iterate through each path key and its corresponding path item - for (const [pathKey, pathItem] of Object.entries(paths)) { - // Skip if not a custom method - if (!isCustomMethod(pathKey)) continue; + if (!isCustomMethod(pathKey)) return; - // Get HTTP methods for this path - const httpMethods = Object.keys(pathItem); + const httpMethods = Object.keys(input); - // Check for invalid methods - if (httpMethods.some((method) => !VALID_METHODS.includes(method))) { - errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE }); - continue; - } + // 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; + // Check for multiple valid methods + const validMethodCount = httpMethods.filter((method) => VALID_METHODS.includes(method)).length; - if (validMethodCount > 1) { - errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE }); - } + if (validMethodCount > 1) { + return ERROR_RESULT; } - - return errors; }; From 439edfc6df66b8f6693228884bb5d89f74c2cd6e Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 18 Dec 2024 16:40:18 +0000 Subject: [PATCH 08/11] address the comments --- .../ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js | 8 ++++++++ tools/spectral/ipa/rulesets/IPA-109.yaml | 2 +- .../rulesets/functions/eachCustomMethodMustBeGetOrPost.js | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js b/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js index e48ec6e579..3dd4777f9d 100644 --- a/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js +++ b/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js @@ -18,6 +18,14 @@ testRule('xgen-IPA-109-custom-method-must-be-GET-or-POST', [ '/b:method': { get: {}, }, + '/c/{exampleId}:method': { + get: {}, + 'x-xgen-IPA-exception': {} + }, + '/c:method': { + get: {}, + 'x-xgen-IPA-exception': {} + }, }, }, errors: [], diff --git a/tools/spectral/ipa/rulesets/IPA-109.yaml b/tools/spectral/ipa/rulesets/IPA-109.yaml index 48c1645e07..5f8fbdb91e 100644 --- a/tools/spectral/ipa/rulesets/IPA-109.yaml +++ b/tools/spectral/ipa/rulesets/IPA-109.yaml @@ -9,6 +9,6 @@ rules: 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[*]' + given: "$.paths[*]" then: function: 'eachCustomMethodMustBeGetOrPost' diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js index 0a77cc725a..2aad969195 100644 --- a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -3,6 +3,7 @@ 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']; +const EXCEPTION_EXTENSION_KEY = 'x-xgen-IPA-exception'; export default (input, opts, { path }) => { // Extract the path key (e.g., '/a/{exampleId}:method') from the JSONPath. @@ -10,7 +11,9 @@ export default (input, opts, { path }) => { if (!isCustomMethod(pathKey)) return; - const httpMethods = Object.keys(input); + //Exclude exception extension key + let keys = Object.keys(input); + const httpMethods = keys.filter(key => key !== EXCEPTION_EXTENSION_KEY); // Check for invalid methods if (httpMethods.some((method) => !VALID_METHODS.includes(method))) { From a485904d84cf141de4289f6254ece28dde124446 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 18 Dec 2024 16:40:58 +0000 Subject: [PATCH 09/11] prettier fix --- .../ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js | 4 ++-- tools/spectral/ipa/rulesets/IPA-109.yaml | 2 +- .../ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js b/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js index 3dd4777f9d..369f61efcd 100644 --- a/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js +++ b/tools/spectral/ipa/__tests__/eachCustomMethodMustBeGetOrPost.test.js @@ -20,11 +20,11 @@ testRule('xgen-IPA-109-custom-method-must-be-GET-or-POST', [ }, '/c/{exampleId}:method': { get: {}, - 'x-xgen-IPA-exception': {} + 'x-xgen-IPA-exception': {}, }, '/c:method': { get: {}, - 'x-xgen-IPA-exception': {} + 'x-xgen-IPA-exception': {}, }, }, }, diff --git a/tools/spectral/ipa/rulesets/IPA-109.yaml b/tools/spectral/ipa/rulesets/IPA-109.yaml index 5f8fbdb91e..48c1645e07 100644 --- a/tools/spectral/ipa/rulesets/IPA-109.yaml +++ b/tools/spectral/ipa/rulesets/IPA-109.yaml @@ -9,6 +9,6 @@ rules: 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[*]" + given: '$.paths[*]' then: function: 'eachCustomMethodMustBeGetOrPost' diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js index 2aad969195..0dcf2bb799 100644 --- a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -13,7 +13,7 @@ export default (input, opts, { path }) => { //Exclude exception extension key let keys = Object.keys(input); - const httpMethods = keys.filter(key => key !== EXCEPTION_EXTENSION_KEY); + const httpMethods = keys.filter((key) => key !== EXCEPTION_EXTENSION_KEY); // Check for invalid methods if (httpMethods.some((method) => !VALID_METHODS.includes(method))) { From 74cc372608c0106f774ae67e3ce14d3fc1636301 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 18 Dec 2024 16:59:34 +0000 Subject: [PATCH 10/11] address the comments --- .../rulesets/functions/eachCustomMethodMustBeGetOrPost.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js index 0dcf2bb799..bb7be025da 100644 --- a/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js +++ b/tools/spectral/ipa/rulesets/functions/eachCustomMethodMustBeGetOrPost.js @@ -3,7 +3,7 @@ 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']; -const EXCEPTION_EXTENSION_KEY = 'x-xgen-IPA-exception'; +const HTTP_METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace']; export default (input, opts, { path }) => { // Extract the path key (e.g., '/a/{exampleId}:method') from the JSONPath. @@ -11,9 +11,9 @@ export default (input, opts, { path }) => { if (!isCustomMethod(pathKey)) return; - //Exclude exception extension key + //Extract the keys which are equivalent of the http methods let keys = Object.keys(input); - const httpMethods = keys.filter((key) => key !== EXCEPTION_EXTENSION_KEY); + const httpMethods = keys.filter((key) => HTTP_METHODS.includes(key)); // Check for invalid methods if (httpMethods.some((method) => !VALID_METHODS.includes(method))) { From df5a94adfd033d9df73387f9bc5d961539233d3a Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 18 Dec 2024 17:10:39 +0000 Subject: [PATCH 11/11] prettier fix --- tools/spectral/ipa/ipa-spectral.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/spectral/ipa/ipa-spectral.yaml b/tools/spectral/ipa/ipa-spectral.yaml index ad74bfcb42..789367fc96 100644 --- a/tools/spectral/ipa/ipa-spectral.yaml +++ b/tools/spectral/ipa/ipa-spectral.yaml @@ -3,4 +3,3 @@ extends: - ./rulesets/IPA-102.yaml - ./rulesets/IPA-104.yaml - ./rulesets/IPA-109.yaml -