diff --git a/tools/spectral/ipa/__tests__/deleteMethod204Response.test.js b/tools/spectral/ipa/__tests__/deleteMethod204Response.test.js new file mode 100644 index 0000000000..88d364298f --- /dev/null +++ b/tools/spectral/ipa/__tests__/deleteMethod204Response.test.js @@ -0,0 +1,104 @@ +import testRule from './__helpers__/testRule'; +import { DiagnosticSeverity } from '@stoplight/types'; + +testRule('xgen-IPA-108-delete-method-return-204-response', [ + { + name: 'valid DELETE with 204', + document: { + paths: { + '/resource/{id}': { + delete: { + responses: { + 204: { + description: 'Resource deleted', + }, + }, + }, + }, + }, + }, + errors: [], + }, + { + name: 'invalid DELETE with no responses', + document: { + paths: { + '/resource/{id}': { + delete: { + responses: {}, + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-108-delete-method-return-204-response', + message: 'DELETE method should return 204 No Content status code. http://go/ipa/108', + path: ['paths', '/resource/{id}', 'delete'], + severity: DiagnosticSeverity.Warning, + }, + ], + }, + { + name: 'invalid DELETE with no responses', + document: { + paths: { + '/resource/{id}': { + delete: {}, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-108-delete-method-return-204-response', + message: 'DELETE method should return 204 No Content status code. http://go/ipa/108', + path: ['paths', '/resource/{id}', 'delete'], + severity: DiagnosticSeverity.Warning, + }, + ], + }, + { + name: 'invalid DELETE missing 204', + document: { + paths: { + '/resource/{id}': { + delete: { + responses: { + 200: { + description: 'Resource deleted', + }, + }, + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-108-delete-method-return-204-response', + message: 'DELETE method should return 204 No Content status code. 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-method-return-204-response': 'Legacy API', + }, + responses: { + 200: { + description: 'Resource deleted', + }, + }, + }, + }, + }, + }, + errors: [], + }, +]); diff --git a/tools/spectral/ipa/rulesets/IPA-108.yaml b/tools/spectral/ipa/rulesets/IPA-108.yaml index 7300f624b5..af2306e778 100644 --- a/tools/spectral/ipa/rulesets/IPA-108.yaml +++ b/tools/spectral/ipa/rulesets/IPA-108.yaml @@ -10,5 +10,14 @@ rules: then: function: deleteMethodResponseShouldNotHaveSchema + xgen-IPA-108-delete-method-return-204-response: + description: DELETE method must return 204 No Content. http://go/ipa/108 + message: '{{error}} http://go/ipa/108' + severity: warn + given: $.paths[*].delete + then: + function: deleteMethod204Response + functions: - deleteMethodResponseShouldNotHaveSchema + - deleteMethod204Response diff --git a/tools/spectral/ipa/rulesets/README.md b/tools/spectral/ipa/rulesets/README.md index 0ef8fb00e3..1d706d5126 100644 --- a/tools/spectral/ipa/rulesets/README.md +++ b/tools/spectral/ipa/rulesets/README.md @@ -50,9 +50,10 @@ 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 | +| 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 | ### IPA-109 diff --git a/tools/spectral/ipa/rulesets/functions/deleteMethod204Response.js b/tools/spectral/ipa/rulesets/functions/deleteMethod204Response.js new file mode 100644 index 0000000000..c907589bc5 --- /dev/null +++ b/tools/spectral/ipa/rulesets/functions/deleteMethod204Response.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-method-return-204-response'; +const ERROR_MESSAGE = 'DELETE method should return 204 No Content status code.'; + +/** + * Delete method should return 204 No Content status code + * + * @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['204']) { + return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE); + } + return collectAdoption(path, RULE_NAME); +};