Skip to content

Commit b981826

Browse files
authored
CLOUDP-306347: isSingleResourceIdentifier for delete operations (#553)
1 parent 981b3c8 commit b981826

10 files changed

+221
-10
lines changed

tools/spectral/ipa/__tests__/IPA108DeleteMethod204Response.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,45 @@ testRule('xgen-IPA-108-delete-method-return-204-response', [
101101
},
102102
errors: [],
103103
},
104+
{
105+
name: 'skipped for collection endpoint (no path parameter)',
106+
document: {
107+
paths: {
108+
'/resources': {
109+
delete: {
110+
responses: {
111+
200: {
112+
description: 'Resource deleted',
113+
},
114+
},
115+
},
116+
},
117+
},
118+
},
119+
errors: [],
120+
},
121+
{
122+
name: 'applied for single resource endpoint (with path parameter)',
123+
document: {
124+
paths: {
125+
'/resources/{resourceId}': {
126+
delete: {
127+
responses: {
128+
200: {
129+
description: 'Resource deleted',
130+
},
131+
},
132+
},
133+
},
134+
},
135+
},
136+
errors: [
137+
{
138+
code: 'xgen-IPA-108-delete-method-return-204-response',
139+
message: 'DELETE method should return 204 No Content status code.',
140+
path: ['paths', '/resources/{resourceId}', 'delete'],
141+
severity: DiagnosticSeverity.Warning,
142+
},
143+
],
144+
},
104145
]);

tools/spectral/ipa/__tests__/IPA108DeleteMethod404Response.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,41 @@ testRule('xgen-IPA-108-delete-include-404-response', [
9898
},
9999
errors: [],
100100
},
101+
{
102+
name: 'skipped for collection endpoint (no path parameter)',
103+
document: {
104+
paths: {
105+
'/resources': {
106+
delete: {
107+
responses: {
108+
204: {},
109+
},
110+
},
111+
},
112+
},
113+
},
114+
errors: [],
115+
},
116+
{
117+
name: 'applied for single resource endpoint (with path parameter)',
118+
document: {
119+
paths: {
120+
'/resources/{resourceId}': {
121+
delete: {
122+
responses: {
123+
204: {},
124+
},
125+
},
126+
},
127+
},
128+
},
129+
errors: [
130+
{
131+
code: 'xgen-IPA-108-delete-include-404-response',
132+
message: 'DELETE method should include 404 status code for not found resources.',
133+
path: ['paths', '/resources/{resourceId}', 'delete'],
134+
severity: DiagnosticSeverity.Warning,
135+
},
136+
],
137+
},
101138
]);

tools/spectral/ipa/__tests__/IPA108DeleteMethodNoRequestBody.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,49 @@ testRule('xgen-IPA-108-delete-request-no-body', [
6868
},
6969
errors: [],
7070
},
71+
{
72+
name: 'skipped for collection endpoint (no path parameter)',
73+
document: {
74+
paths: {
75+
'/resources': {
76+
delete: {
77+
requestBody: {
78+
content: {
79+
'application/json': {
80+
schema: { type: 'object' },
81+
},
82+
},
83+
},
84+
},
85+
},
86+
},
87+
},
88+
errors: [],
89+
},
90+
{
91+
name: 'applied for single resource endpoint (with path parameter)',
92+
document: {
93+
paths: {
94+
'/resources/{resourceId}': {
95+
delete: {
96+
requestBody: {
97+
content: {
98+
'application/json': {
99+
schema: { type: 'object' },
100+
},
101+
},
102+
},
103+
},
104+
},
105+
},
106+
},
107+
errors: [
108+
{
109+
code: 'xgen-IPA-108-delete-request-no-body',
110+
message: 'DELETE method should not have a request body.',
111+
path: ['paths', '/resources/{resourceId}', 'delete'],
112+
severity: DiagnosticSeverity.Warning,
113+
},
114+
],
115+
},
71116
]);

tools/spectral/ipa/__tests__/IPA108DeleteMethodResponseShouldNotHaveSchema.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,54 @@ testRule('xgen-IPA-108-delete-response-should-be-empty', [
9595
},
9696
errors: [],
9797
},
98+
{
99+
name: 'skipped for collection endpoint (no path parameter)',
100+
document: {
101+
paths: {
102+
'/resources': {
103+
delete: {
104+
responses: {
105+
204: {
106+
content: {
107+
'application/vnd.atlas.2023-01-01+json': {
108+
schema: { type: 'object' },
109+
},
110+
},
111+
},
112+
},
113+
},
114+
},
115+
},
116+
},
117+
errors: [],
118+
},
119+
{
120+
name: 'applied for single resource endpoint (with path parameter)',
121+
document: {
122+
paths: {
123+
'/resources/{resourceId}': {
124+
delete: {
125+
responses: {
126+
204: {
127+
content: {
128+
'application/vnd.atlas.2023-01-01+json': {
129+
schema: { type: 'object' },
130+
},
131+
},
132+
},
133+
},
134+
},
135+
},
136+
},
137+
},
138+
errors: [
139+
{
140+
code: 'xgen-IPA-108-delete-response-should-be-empty',
141+
message:
142+
'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.',
143+
path: ['paths', '/resources/{resourceId}', 'delete', 'responses', '204'],
144+
severity: DiagnosticSeverity.Warning,
145+
},
146+
],
147+
},
98148
]);

tools/spectral/ipa/rulesets/IPA-108.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ rules:
88
99
##### Implementation details
1010
Rule checks for the following conditions:
11-
- Applies to 204 responses in DELETE methods
11+
- Applies to 204 responses in DELETE methods for single resource endpoints (with path parameters)
1212
- Verifies that the response does not contain a schema property
1313
- Fails if any content type in the response has a defined schema as reference
14+
- Skips validation for collection endpoints (without path parameters)
1415
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-108-delete-response-should-be-empty'
1516
severity: warn
1617
given: $.paths[*].delete.responses[204]
@@ -23,11 +24,12 @@ rules:
2324
2425
##### Implementation details
2526
Rule checks for the following conditions:
26-
- Applies to all DELETE methods
27-
- Verifies the 204 No Content response code is present
27+
- Applies to all DELETE methods for single resource endpoints (with path parameters)
28+
- Verifies the 204 No Content response code is present
2829
- Fails if the method lacks a 204 No Content response or defines a different 2xx status code
2930
- Ensures no other 2xx response codes are defined
3031
- Fails if the 204 status code is missing or if other 2xx responses exist
32+
- Skips validation for collection endpoints (without path parameters)
3133
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-108-delete-method-return-204-response'
3234
severity: warn
3335
given: $.paths[*].delete
@@ -40,9 +42,10 @@ rules:
4042
4143
##### Implementation details
4244
Rule checks for the following conditions:
43-
- Applies to all DELETE methods
45+
- Applies to all DELETE methods for single resource endpoints (with path parameters)
4446
- Verifies that the method includes a 404 response code
4547
- Fails if the 404 status code is missing from the responses
48+
- Skips validation for collection endpoints (without path parameters)
4649
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-108-delete-include-404-response'
4750
severity: warn
4851
given: $.paths[*].delete
@@ -55,9 +58,10 @@ rules:
5558
5659
##### Implementation details
5760
Rule checks for the following conditions:
58-
- Applies to all DELETE methods
61+
- Applies to all DELETE methods for single resource endpoints (with path parameters)
5962
- Verifies that the operation object does not contain a requestBody property
6063
- Fails if any requestBody is defined for the DELETE method
64+
- Skips validation for collection endpoints (without path parameters)
6165
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-108-delete-request-no-body'
6266
severity: warn
6367
given: $.paths[*].delete

tools/spectral/ipa/rulesets/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,10 @@ Delete method response should not have schema reference to object.
336336

337337
##### Implementation details
338338
Rule checks for the following conditions:
339-
- Applies to 204 responses in DELETE methods
339+
- Applies to 204 responses in DELETE methods for single resource endpoints (with path parameters)
340340
- Verifies that the response does not contain a schema property
341341
- Fails if any content type in the response has a defined schema as reference
342+
- Skips validation for collection endpoints (without path parameters)
342343

343344
#### xgen-IPA-108-delete-method-return-204-response
344345

@@ -347,11 +348,12 @@ DELETE method must return 204 No Content.
347348

348349
##### Implementation details
349350
Rule checks for the following conditions:
350-
- Applies to all DELETE methods
351-
- Verifies the 204 No Content response code is present
351+
- Applies to all DELETE methods for single resource endpoints (with path parameters)
352+
- Verifies the 204 No Content response code is present
352353
- Fails if the method lacks a 204 No Content response or defines a different 2xx status code
353354
- Ensures no other 2xx response codes are defined
354355
- Fails if the 204 status code is missing or if other 2xx responses exist
356+
- Skips validation for collection endpoints (without path parameters)
355357

356358
#### xgen-IPA-108-delete-include-404-response
357359

@@ -360,9 +362,10 @@ DELETE method must include 404 response and return it when resource not found.
360362

361363
##### Implementation details
362364
Rule checks for the following conditions:
363-
- Applies to all DELETE methods
365+
- Applies to all DELETE methods for single resource endpoints (with path parameters)
364366
- Verifies that the method includes a 404 response code
365367
- Fails if the 404 status code is missing from the responses
368+
- Skips validation for collection endpoints (without path parameters)
366369

367370
#### xgen-IPA-108-delete-request-no-body
368371

@@ -371,9 +374,10 @@ DELETE method must not have request body.
371374

372375
##### Implementation details
373376
Rule checks for the following conditions:
374-
- Applies to all DELETE methods
377+
- Applies to all DELETE methods for single resource endpoints (with path parameters)
375378
- Verifies that the operation object does not contain a requestBody property
376379
- Fails if any requestBody is defined for the DELETE method
380+
- Skips validation for collection endpoints (without path parameters)
377381

378382

379383

tools/spectral/ipa/rulesets/functions/IPA108DeleteMethod204Response.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
collectException,
55
handleInternalError,
66
} from './utils/collectionUtils.js';
7+
import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
78
import { hasException } from './utils/exceptions.js';
89

910
const RULE_NAME = 'xgen-IPA-108-delete-method-return-204-response';
@@ -17,6 +18,13 @@ const ERROR_MESSAGE = 'DELETE method should return 204 No Content status code.';
1718
* @param {object} context - The context object containing the path
1819
*/
1920
export default (input, _, { path }) => {
21+
// Check if the path is for a single resource (e.g., has path parameter)
22+
// Extract the path from context.path which is an array
23+
const pathString = path[1]; // Assuming path is ['paths', '/resource/{id}', 'delete']
24+
if (!isSingleResourceIdentifier(pathString)) {
25+
return;
26+
}
27+
2028
if (hasException(input, RULE_NAME)) {
2129
collectException(input, RULE_NAME, path);
2230
return;

tools/spectral/ipa/rulesets/functions/IPA108DeleteMethod404Response.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
2+
import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
23
import { hasException } from './utils/exceptions.js';
34

45
const RULE_NAME = 'xgen-IPA-108-delete-include-404-response';
@@ -12,6 +13,13 @@ const ERROR_MESSAGE = 'DELETE method should include 404 status code for not foun
1213
* @param {object} context - The context object containing the path
1314
*/
1415
export default (input, _, { path }) => {
16+
// Check if the path is for a single resource (e.g., has path parameter)
17+
// Extract the path from context.path which is an array
18+
const pathString = path[1]; // Assuming path is ['paths', '/resource/{id}', 'delete']
19+
if (!isSingleResourceIdentifier(pathString)) {
20+
return;
21+
}
22+
1523
if (hasException(input, RULE_NAME)) {
1624
collectException(input, RULE_NAME, path);
1725
return;

tools/spectral/ipa/rulesets/functions/IPA108DeleteMethodNoRequestBody.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
2+
import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
23
import { hasException } from './utils/exceptions.js';
34

45
const RULE_NAME = 'xgen-IPA-108-delete-request-no-body';
@@ -12,6 +13,13 @@ const ERROR_MESSAGE = 'DELETE method should not have a request body.';
1213
* @param {object} context - The context object containing the path
1314
*/
1415
export default (input, _, { path }) => {
16+
// Check if the path is for a single resource (e.g., has path parameter)
17+
// Extract the path from context.path which is an array
18+
const pathString = path[1]; // Assuming path is ['paths', '/resource/{id}', 'delete']
19+
if (!isSingleResourceIdentifier(pathString)) {
20+
return;
21+
}
22+
1523
if (hasException(input, RULE_NAME)) {
1624
collectException(input, RULE_NAME, path);
1725
return;

tools/spectral/ipa/rulesets/functions/IPA108DeleteMethodResponseShouldNotHaveSchema.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
collectException,
66
handleInternalError,
77
} from './utils/collectionUtils.js';
8+
import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
89

910
const RULE_NAME = 'xgen-IPA-108-delete-response-should-be-empty';
1011
const ERROR_MESSAGE = 'DELETE method should return an empty response. The response should not have a schema property.';
@@ -16,6 +17,11 @@ const ERROR_MESSAGE = 'DELETE method should return an empty response. The respon
1617
* @param {object} context - The context object containing the path
1718
*/
1819
export default (input, _, { path }) => {
20+
const pathString = path[1]; // Extract the resource path
21+
if (!isSingleResourceIdentifier(pathString)) {
22+
return;
23+
}
24+
1925
// 1. Handle exception on OpenAPI schema
2026
if (hasException(input, RULE_NAME)) {
2127
collectException(input, RULE_NAME, path);

0 commit comments

Comments
 (0)