Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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,98 @@
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',
},
'application/vnd.atlas.2023-03-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:
'Error found for application/vnd.atlas.2023-01-01+json: DELETE method should return an empty response. The response should not have a schema property. 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,61 @@
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.';

/**
* 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 }) => {
// 1. Filter out not relevant use cases that should not lead to adoption.
const deleteOp = input;
if (!deleteOp.responses || deleteOp.responses.length === 0) {
return;
}

// 2. Handle exception on OpenAPI schema
if (hasException(deleteOp, RULE_NAME)) {
collectException(deleteOp, RULE_NAME, path);
return;
}

// 3. Validation
const errors = checkViolations(deleteOp.responses);
if (errors) {
return collectAndReturnViolation(path, RULE_NAME, errors);
}

collectAdoption(path, RULE_NAME);
};

/**
* Check if the operation has validation issues
* @param {object} input - The object to vefify
* @return {Array<string>|undefined} - The content types that have a schema
*/
function checkViolations(input) {
try {
if (input && input['204']) {
const successResponse = input['204'];
if (successResponse.content) {
const errors = [];
for (const contentType of Object.keys(successResponse.content)) {
if (successResponse.content[contentType] && successResponse.content[contentType].schema) {
errors.push({
message: `Error found for ${contentType}: ${ERROR_MESSAGE}`,
});
}
}
return errors.length > 0 ? errors : undefined;
}
}
} catch (e) {
return ['Internal Rule Error without reporting violation' + e];
Copy link
Member Author

@wtrocki wtrocki Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can get different error when rule missbehaves. That gives us:

  • Ability to understand the issue at runtime and tests instead of getting generic AggregateError: Error running Nimma
  • Protect production failure to break entire validation. Users will be able to add exception until we fix problem in single rule but other rules will still work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to helper for reuse.

}
// No errors returning undefined
return undefined;
}
Loading