Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions tools/spectral/ipa/__tests__/deleteMethod404Response.test.js
Original file line number Diff line number Diff line change
@@ -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: [],
},
]);
9 changes: 9 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-108.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
message: '{{error}} http://go/ipa/108'
severity: warn
given: $.paths[*].delete
then:
function: deleteMethod404Response

functions:
- deleteMethodResponseShouldNotHaveSchema
- deleteMethod204Response
- deleteMethod404Response
1 change: 1 addition & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ For rule definitions, see [IPA-108.yaml](https://github.com/mongodb/openapi/blob
| ---------------------------------------------- | ------------------------------------------------------------------------------------ | -------- |
| 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 | warn |

### IPA-109

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
};
Loading