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
Empty file.
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', 'responses', '204'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'valid with exception',
document: {
paths: {
'/resource/{id}': {
delete: {
responses: {
204: {
'application/vnd.atlas.2023-01-01+json': {
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️

'x-xgen-IPA-exception': {
'xgen-IPA-108-delete-response-should-be-empty': 'Legacy API',
},
content: {
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.responses[204]
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,58 @@
import { hasException } from './utils/exceptions.js';
import {
collectAdoption,
collectAndReturnViolation,
collectException,
handleInternalError,
} 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. Handle exception on OpenAPI schema
if (hasException(input, RULE_NAME)) {
collectException(input, RULE_NAME, path);
return;
}

// 2. Validation
const errors = checkViolations(input, path);
if (errors.length > 0) {
return collectAndReturnViolation(path, RULE_NAME, errors);
}

collectAdoption(path, RULE_NAME);
};

/**
* Check if the operation has validation issues
* @param {object} input - The object to vefify
* @param {object} jsonPathArray - The jsonPathArray covering location in the OpenAPI schema
* @return {Array<string>} - errors array ()
*/
function checkViolations(input, jsonPathArray) {
const errors = [];
try {
const successResponse = input;
if (successResponse.content) {
for (const contentType of Object.keys(successResponse.content)) {
if (successResponse.content[contentType] && successResponse.content[contentType].schema) {
errors.push({
path: jsonPathArray,
message: `Error found for ${contentType}: ${ERROR_MESSAGE}`,
});
}
}
}
} catch (e) {
handleInternalError(RULE_NAME, jsonPathArray, e);
}
return errors;
}
14 changes: 14 additions & 0 deletions tools/spectral/ipa/rulesets/functions/utils/collectionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ export function collectException(object, ruleName, path) {
collector.add(EntryType.EXCEPTION, path, ruleName, exceptionReason);
}
}

/**
* Creates internal rule error entry for the collector in order to not fail validation process.
* @param {Array<string>} jsonPathArray - The JSON path for the object where the rule exception occurred.
* @param {string} ruleName - The name of the rule that was adopted.
*/
export function handleInternalError(ruleName, jsonPathArray, error) {
return [
{
path: jsonPathArray,
message: `${ruleName} Internal Rule Error: ${error} Please report issue in https://github.com/mongodb/openapi/issues`,
},
];
}
Loading