Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-108-delete-response-should-be-empty', [
{
name: 'valid DELETE with void 204',
document: {
paths: {
'/resource/{id}': {
delete: {
responses: {
204: {},
},
},
},
},
},
errors: [],
},
{
name: 'valid DELETE with void 204 versioned',
document: {
paths: {
'/resource/{id}': {
delete: {
responses: {
204: {
description: 'No Content',
content: {
'application/vnd.atlas.2023-01-01+json': {
'x-xgen-version': '2023-01-01',
},
},
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid DELETE with non-void 204',
document: {
paths: {
'/resource/{id}': {
delete: {
responses: {
204: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: { type: 'object' },
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-108-delete-response-should-be-empty',
message:
'DELETE method should return an empty response. The response should not have a schema property and reference to models. 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-response-should-be-empty': 'Legacy API',
},
responses: {
204: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: { type: 'object' },
},
},
},
},
},
},
},
},
errors: [],
},
]);
1 change: 1 addition & 0 deletions tools/spectral/ipa/ipa-spectral.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extends:
- ./rulesets/IPA-113.yaml
- ./rulesets/IPA-123.yaml
- ./rulesets/IPA-106.yaml
- ./rulesets/IPA-108.yaml

overrides:
- files:
Expand Down
14 changes: 14 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-108.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# IPA-108: Delete
# http://go/ipa/108

rules:
xgen-IPA-108-delete-response-should-be-empty:
description: Delete method response should not have schema reference to object. http://go/ipa/108
message: '{{error}} http://go/ipa/108'
severity: warn
given: $.paths[*].delete
then:
function: deleteMethodResponseShouldNotHaveSchema

functions:
- deleteMethodResponseShouldNotHaveSchema
8 changes: 8 additions & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ For rule definitions, see [IPA-106.yaml](https://github.com/mongodb/openapi/blob
| ------------------------------------------------------------------ | -------------------------------------------------------------------------------- | -------- |
| xgen-IPA-106-create-method-request-body-is-request-suffixed-object | The Create method request should be a Request suffixed object. http://go/ipa/106 | warn |

### IPA-108

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 |

### IPA-109

For rule definitions, see [IPA-109.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-109.yaml).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { hasException } from './utils/exceptions.js';
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';

const RULE_NAME = 'xgen-IPA-108-delete-response-should-be-empty';
const ERROR_MESSAGE =
'DELETE method should return an empty response. The response should not have a schema property and reference to models.';

/**
* Delete method should return an empty response
* @param {object} input - The delete operation object
* @param {object} _ - Unused
* @param {object} context - The context object containing the path
*/
export default (input, _, { path }) => {
const deleteOp = input;
if (!deleteOp.responses || deleteOp.responses.length === 0) {
return;
}
if (hasException(deleteOp, RULE_NAME)) {
collectException(deleteOp, RULE_NAME, path);
return;
}

const responses = deleteOp.responses;
for (const [status, response] of Object.entries(responses)) {
if (status === '204' && response.content) {
for (const contentType of Object.keys(response.content)) {
if (response.content[contentType].schema) {
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
}
}
}
}

collectAdoption(path, RULE_NAME);
};
Loading