diff --git a/tools/spectral/ipa/__tests__/deleteMethod404Response.test.js b/tools/spectral/ipa/__tests__/deleteMethod404Response.test.js new file mode 100644 index 0000000000..49b492f4b7 --- /dev/null +++ b/tools/spectral/ipa/__tests__/deleteMethod404Response.test.js @@ -0,0 +1,101 @@ +import testRule from './__helpers__/testRule'; +import { DiagnosticSeverity } from '@stoplight/types'; + +testRule('xgen-IPA-108-delete-include-404-response', [ + { + name: 'valid DELETE with 404', + document: { + paths: { + '/resource/{id}': { + delete: { + responses: { + 204: {}, + 404: { + description: 'Resource not found', + }, + }, + }, + }, + }, + }, + errors: [], + }, + { + name: 'invalid DELETE with no responses', + document: { + paths: { + '/resource/{id}': { + delete: {}, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-108-delete-include-404-response', + message: 'DELETE method should include 404 status code for not found resources. http://go/ipa/108', + path: ['paths', '/resource/{id}', 'delete'], + severity: DiagnosticSeverity.Warning, + }, + ], + }, + { + name: 'invalid empty responses', + document: { + paths: { + '/resource/{id}': { + delete: { + responses: {}, + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-108-delete-include-404-response', + message: 'DELETE method should include 404 status code for not found resources. http://go/ipa/108', + path: ['paths', '/resource/{id}', 'delete'], + severity: DiagnosticSeverity.Warning, + }, + ], + }, + { + name: 'invalid DELETE missing 404', + document: { + paths: { + '/resource/{id}': { + delete: { + responses: { + 204: {}, + }, + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-108-delete-include-404-response', + message: 'DELETE method should include 404 status code for not found resources. http://go/ipa/108', + path: ['paths', '/resource/{id}', 'delete'], + severity: DiagnosticSeverity.Warning, + }, + ], + }, + { + name: 'valid with exception', + document: { + paths: { + '/resource/{id}': { + delete: { + 'x-xgen-IPA-exception': { + 'xgen-IPA-108-delete-include-404-response': 'Idempotent delete', + }, + responses: { + 204: {}, + }, + }, + }, + }, + }, + errors: [], + }, +]); diff --git a/tools/spectral/ipa/rulesets/IPA-108.yaml b/tools/spectral/ipa/rulesets/IPA-108.yaml index af2306e778..e5bd2cd233 100644 --- a/tools/spectral/ipa/rulesets/IPA-108.yaml +++ b/tools/spectral/ipa/rulesets/IPA-108.yaml @@ -18,6 +18,15 @@ rules: then: function: deleteMethod204Response + xgen-IPA-108-delete-include-404-response: + description: DELETE method must include 404 response and return it when resource not found. http://go/ipa/108 + message: '{{error}} http://go/ipa/108' + severity: warn + given: $.paths[*].delete + then: + function: deleteMethod404Response + functions: - deleteMethodResponseShouldNotHaveSchema - deleteMethod204Response + - deleteMethod404Response diff --git a/tools/spectral/ipa/rulesets/README.md b/tools/spectral/ipa/rulesets/README.md index 1d706d5126..a7e120957c 100644 --- a/tools/spectral/ipa/rulesets/README.md +++ b/tools/spectral/ipa/rulesets/README.md @@ -50,10 +50,11 @@ For rule definitions, see [IPA-106.yaml](https://github.com/mongodb/openapi/blob For rule definitions, see [IPA-108.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-108.yaml). -| Rule Name | Description | Severity | -| ---------------------------------------------- | ------------------------------------------------------------------------------------ | -------- | -| xgen-IPA-108-delete-response-should-be-empty | Delete method response should not have schema reference to object. http://go/ipa/108 | warn | -| xgen-IPA-108-delete-method-return-204-response | DELETE method must return 204 No Content. http://go/ipa/108 | warn | +| Rule Name | Description | Severity | +| ---------------------------------------------- | ------------------------------------------------------------------------------------------------ | -------- | +| xgen-IPA-108-delete-response-should-be-empty | Delete method response should not have schema reference to object. http://go/ipa/108 | warn | +| xgen-IPA-108-delete-method-return-204-response | DELETE method must return 204 No Content. http://go/ipa/108 | warn | +| xgen-IPA-108-delete-include-404-response | DELETE method must include 404 response and return it when resource not found. http://go/ipa/108 | warn | ### IPA-109 diff --git a/tools/spectral/ipa/rulesets/functions/deleteMethod404Response.js b/tools/spectral/ipa/rulesets/functions/deleteMethod404Response.js new file mode 100644 index 0000000000..8cbc3ca180 --- /dev/null +++ b/tools/spectral/ipa/rulesets/functions/deleteMethod404Response.js @@ -0,0 +1,25 @@ +import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js'; +import { hasException } from './utils/exceptions.js'; + +const RULE_NAME = 'xgen-IPA-108-delete-include-404-response'; +const ERROR_MESSAGE = 'DELETE method should include 404 status code for not found resources.'; + +/** + * Delete method should include 404 status code for not found resources + * + * @param {object} input - The delete operation object + * @param {object} _ - Unused + * @param {object} context - The context object containing the path + */ +export default (input, _, { path }) => { + const responses = input.responses; + if (hasException(input, RULE_NAME)) { + collectException(input, RULE_NAME, path); + return; + } + + if (!responses || !responses['404']) { + return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE); + } + return collectAdoption(path, RULE_NAME); +};