Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
104 changes: 104 additions & 0 deletions tools/spectral/ipa/__tests__/deleteMethod204Response.test.js
Original file line number Diff line number Diff line change
@@ -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: [],
},
]);
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 @@ -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
7 changes: 4 additions & 3 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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-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);
};
Loading