diff --git a/tools/spectral/ipa/__tests__/IPA106ReadOnlyResourceShouldNotHaveCreateMethod.test.js b/tools/spectral/ipa/__tests__/IPA106ReadOnlyResourceShouldNotHaveCreateMethod.test.js index f3bde92bd90..3a20deabec8 100644 --- a/tools/spectral/ipa/__tests__/IPA106ReadOnlyResourceShouldNotHaveCreateMethod.test.js +++ b/tools/spectral/ipa/__tests__/IPA106ReadOnlyResourceShouldNotHaveCreateMethod.test.js @@ -287,4 +287,169 @@ testRule('xgen-IPA-106-readonly-resource-should-not-have-create-method', [ }, errors: [], }, + { + name: 'valid: resource with create method and request body parity exception', + document: { + openapi: '3.0.0', + paths: { + '/resource': { + get: { + responses: { + 200: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + post: { + requestBody: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + 'x-xgen-IPA-exception': { + 'xgen-IPA-106-create-method-request-body-is-get-method-response': 'Request body takes firstName', + }, + schema: { + type: 'object', + properties: { + firstName: { type: 'integer' }, + }, + }, + }, + }, + }, + responses: { + 201: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + }, + '/resource/{id}': { + get: { + responses: { + 200: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + errors: [], + }, + { + name: 'invalid: read-only resource with create method and an unrelated exception on the request body content', + document: { + openapi: '3.0.0', + paths: { + '/resource': { + get: { + responses: { + 200: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + post: { + requestBody: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + 'x-xgen-IPA-exception': { + 'xgen-IPA-106-create-method-request-has-no-readonly-fields': 'Unrelated exception', + }, + schema: { + type: 'object', + properties: { + firstName: { type: 'integer' }, + }, + }, + }, + }, + }, + responses: { + 201: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + }, + '/resource/{id}': { + get: { + responses: { + 200: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-106-readonly-resource-should-not-have-create-method', + message: 'Read-only resources must not define the Create method.', + path: ['paths', '/resource', 'post'], + severity: DiagnosticSeverity.Error, + }, + ], + }, ]); diff --git a/tools/spectral/ipa/__tests__/IPA108ReadOnlyResourceShouldNotHaveDeleteMethod.test.js b/tools/spectral/ipa/__tests__/IPA108ReadOnlyResourceShouldNotHaveDeleteMethod.test.js index 645493e8718..f1ae25007b7 100644 --- a/tools/spectral/ipa/__tests__/IPA108ReadOnlyResourceShouldNotHaveDeleteMethod.test.js +++ b/tools/spectral/ipa/__tests__/IPA108ReadOnlyResourceShouldNotHaveDeleteMethod.test.js @@ -240,4 +240,169 @@ testRule('xgen-IPA-108-readonly-resource-should-not-have-delete-method', [ }, errors: [], }, + { + name: 'valid: resource with create is allowed a delete method', + document: { + openapi: '3.0.0', + paths: { + '/resource': { + get: { + responses: { + 200: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + post: { + requestBody: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { type: 'object', properties: { name: { type: 'string' } } }, + }, + }, + }, + responses: { + 201: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + }, + '/resource/{id}': { + get: { + responses: { + 200: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + delete: { + responses: { + 204: { + description: 'No Content', + }, + }, + }, + }, + }, + }, + errors: [], + }, + { + name: 'invalid: read-only resource with only a custom method should not have a delete method', + document: { + openapi: '3.0.0', + paths: { + '/readOnlyResource': { + get: { + responses: { + 200: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + }, + '/readOnlyResource/{id}': { + get: { + responses: { + 200: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + delete: { + responses: { + 204: { + description: 'No Content', + }, + }, + }, + }, + '/readOnlyResource/{id}:reset': { + post: { + requestBody: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { type: 'object', properties: { name: { type: 'string' } } }, + }, + }, + }, + responses: { + 201: { + content: { + 'application/vnd.atlas.2023-01-01+json': { + schema: { + type: 'object', + properties: { + id: { type: 'string', readOnly: true }, + name: { type: 'string', readOnly: true }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-108-readonly-resource-should-not-have-delete-method', + message: 'Read-only resources must not define the Delete method.', + path: ['paths', '/readOnlyResource/{id}', 'delete'], + severity: DiagnosticSeverity.Error, + }, + ], + }, ]); diff --git a/tools/spectral/ipa/__tests__/utils/resourceEvaluation.test.js b/tools/spectral/ipa/__tests__/utils/resourceEvaluation.test.js index f92a809fb79..075c48b6f01 100644 --- a/tools/spectral/ipa/__tests__/utils/resourceEvaluation.test.js +++ b/tools/spectral/ipa/__tests__/utils/resourceEvaluation.test.js @@ -2,6 +2,7 @@ import { describe, expect, it } from '@jest/globals'; import { allPropertiesAreReadOnly, getResourcePathItems, + hasStandardCreateMethod, isReadOnlyResource, isResourceCollectionIdentifier, isRootLevelResource, @@ -643,4 +644,47 @@ describe('tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js', () expect(resourceBelongsToSingleParent('/a/{b}/{c}')).toBe(false); }); }); + + describe('hasStandardCreateMethod', () => { + const paths = { + '/resource': { get: {}, post: {} }, + '/resource/{id}': { get: {}, delete: {} }, + '/resource/{id}:reset': { post: {} }, + '/resourceWithoutCreate': { get: {} }, + '/resourceWithoutCreate/{id}': { get: {}, delete: {} }, + '/resourceWithoutCreate/{id}:reset': { post: {} }, + '/resourceWithoutCreate:search': { post: {} }, + '/groups/{groupId}/singleton': { get: {}, patch: {} }, + '/groups/{groupId}/nested': { get: {}, post: {} }, + '/groups/{groupId}/nested/{nestedId}': { get: {}, delete: {} }, + }; + + it('returns true for a resource collection path with a POST method', () => { + expect(hasStandardCreateMethod('/resource', paths)).toBe(true); + }); + + it('returns true for a single resource path when its collection path has a POST method', () => { + expect(hasStandardCreateMethod('/resource/{id}', paths)).toBe(true); + expect(hasStandardCreateMethod('/groups/{groupId}/nested/{nestedId}', paths)).toBe(true); + }); + + it('returns false when the collection path has no POST method', () => { + expect(hasStandardCreateMethod('/resourceWithoutCreate', paths)).toBe(false); + expect(hasStandardCreateMethod('/resourceWithoutCreate/{id}', paths)).toBe(false); + }); + + it('does not treat custom methods as a Create method', () => { + expect(hasStandardCreateMethod('/resource/{id}:reset', paths)).toBe(false); + expect(hasStandardCreateMethod('/resourceWithoutCreate/{id}:reset', paths)).toBe(false); + expect(hasStandardCreateMethod('/resourceWithoutCreate:search', paths)).toBe(false); + }); + + it('returns false for a singleton path without a POST method', () => { + expect(hasStandardCreateMethod('/groups/{groupId}/singleton', paths)).toBe(false); + }); + + it('returns false when the collection path does not exist', () => { + expect(hasStandardCreateMethod('/missing/{id}', paths)).toBe(false); + }); + }); }); diff --git a/tools/spectral/ipa/rulesets/IPA-106.yaml b/tools/spectral/ipa/rulesets/IPA-106.yaml index f3defb6fc2f..875081162ec 100644 --- a/tools/spectral/ipa/rulesets/IPA-106.yaml +++ b/tools/spectral/ipa/rulesets/IPA-106.yaml @@ -125,6 +125,7 @@ rules: - Checks if the resource is a read-only resource (all properties in GET response have readOnly:true) - If a resource does not have a standard GET method, it is not considered read-only (cannot determine the resource schema) - Fails if a Create method is defined on a read-only resource + - A Create method whose request body content has an `x-xgen-IPA-exception` for `xgen-IPA-106-create-method-request-body-is-get-method-response` is excluded, since the resource accepts input that is not in the GET response and is managed through Create - Operation objects with `x-xgen-IPA-exception` for this rule are excluded from validation message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-106-readonly-resource-should-not-have-create-method' severity: error diff --git a/tools/spectral/ipa/rulesets/IPA-108.yaml b/tools/spectral/ipa/rulesets/IPA-108.yaml index 66c677e6e63..03e1e9e0024 100644 --- a/tools/spectral/ipa/rulesets/IPA-108.yaml +++ b/tools/spectral/ipa/rulesets/IPA-108.yaml @@ -64,6 +64,7 @@ rules: - Applies to DELETE methods on single resource paths and singleton resources - Checks if the resource is a read-only resource (all properties in GET response have readOnly:true) - If a resource does not have a standard GET method, it is not considered read-only (cannot determine the resource schema) + - Resources with a standard Create method (POST on the resource collection path) are excluded, since a resource that can be created can be deleted. The Create method is evaluated by xgen-IPA-106-readonly-resource-should-not-have-create-method - Fails if a Delete method is defined on a read-only resource - Operation objects with `x-xgen-IPA-exception` for this rule are excluded from validation message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-108-readonly-resource-should-not-have-delete-method' diff --git a/tools/spectral/ipa/rulesets/README.md b/tools/spectral/ipa/rulesets/README.md index 64d07a86a76..2741c506d02 100644 --- a/tools/spectral/ipa/rulesets/README.md +++ b/tools/spectral/ipa/rulesets/README.md @@ -368,6 +368,7 @@ Rule checks for the following conditions: - Checks if the resource is a read-only resource (all properties in GET response have readOnly:true) - If a resource does not have a standard GET method, it is not considered read-only (cannot determine the resource schema) - Fails if a Create method is defined on a read-only resource + - A Create method whose request body content has an `x-xgen-IPA-exception` for `xgen-IPA-106-create-method-request-body-is-get-method-response` is excluded, since the resource accepts input that is not in the GET response and is managed through Create - Operation objects with `x-xgen-IPA-exception` for this rule are excluded from validation #### xgen-IPA-106-valid-operation-id @@ -579,6 +580,7 @@ Rule checks for the following conditions: - Applies to DELETE methods on single resource paths and singleton resources - Checks if the resource is a read-only resource (all properties in GET response have readOnly:true) - If a resource does not have a standard GET method, it is not considered read-only (cannot determine the resource schema) + - Resources with a standard Create method (POST on the resource collection path) are excluded, since a resource that can be created can be deleted. The Create method is evaluated by xgen-IPA-106-readonly-resource-should-not-have-create-method - Fails if a Delete method is defined on a read-only resource - Operation objects with `x-xgen-IPA-exception` for this rule are excluded from validation diff --git a/tools/spectral/ipa/rulesets/functions/IPA106ReadOnlyResourceShouldNotHaveCreateMethod.js b/tools/spectral/ipa/rulesets/functions/IPA106ReadOnlyResourceShouldNotHaveCreateMethod.js index ab9344f013c..2cf413d3d65 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA106ReadOnlyResourceShouldNotHaveCreateMethod.js +++ b/tools/spectral/ipa/rulesets/functions/IPA106ReadOnlyResourceShouldNotHaveCreateMethod.js @@ -5,9 +5,11 @@ import { isResourceCollectionIdentifier, isSingletonResource, } from './utils/resourceEvaluation.js'; +import { hasException } from './utils/exceptions.js'; import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/collectionUtils.js'; const ERROR_MESSAGE = 'Read-only resources must not define the Create method.'; +const REQUEST_BODY_PARITY_RULE = 'xgen-IPA-106-create-method-request-body-is-get-method-response'; export default (input, opts, { path, documentInventory, rule }) => { const ruleName = rule.name; @@ -24,10 +26,24 @@ export default (input, opts, { path, documentInventory, rule }) => { return; } + // An exception for request body parity means the Create method takes input that is not in the GET response, + // so the resource is managed through Create and is not a true read-only resource + if (hasRequestBodyParityException(resourcePaths[resourcePath].post)) { + return; + } + const errors = checkViolationsAndReturnErrors(path, ruleName); return evaluateAndCollectAdoptionStatus(errors, ruleName, input, path); }; +// The exception is placed on the request body content version, which is what the parity rule evaluates +function hasRequestBodyParityException(postOperation) { + const content = postOperation?.requestBody?.content; + return Object.values(content ?? {}).some( + (mediaTypeObject) => mediaTypeObject && hasException(mediaTypeObject, REQUEST_BODY_PARITY_RULE) + ); +} + function checkViolationsAndReturnErrors(path, ruleName) { try { return [{ path, message: ERROR_MESSAGE }]; diff --git a/tools/spectral/ipa/rulesets/functions/IPA108ReadOnlyResourceShouldNotHaveDeleteMethod.js b/tools/spectral/ipa/rulesets/functions/IPA108ReadOnlyResourceShouldNotHaveDeleteMethod.js index 1593cb35eb4..d0097a8ce71 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA108ReadOnlyResourceShouldNotHaveDeleteMethod.js +++ b/tools/spectral/ipa/rulesets/functions/IPA108ReadOnlyResourceShouldNotHaveDeleteMethod.js @@ -1,5 +1,6 @@ import { getResourcePathItems, + hasStandardCreateMethod, isReadOnlyResource, isResourceCollectionIdentifier, isSingleResourceIdentifier, @@ -26,6 +27,11 @@ export default (input, opts, { path, documentInventory, rule }) => { return; } + // A resource with a Create method is managed and not a true read-only resource, the Create method is evaluated separately by IPA-106 + if (hasStandardCreateMethod(resourcePath, oas.paths)) { + return; + } + const errors = checkViolationsAndReturnErrors(path, ruleName); return evaluateAndCollectAdoptionStatus(errors, ruleName, input, path); }; diff --git a/tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js b/tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js index 607c76bf91a..59280986577 100644 --- a/tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js +++ b/tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js @@ -148,6 +148,25 @@ export function hasPostMethod(pathObject) { return Object.keys(pathObject).includes('post'); } +/** + * Checks if a resource has a standard Create method, a POST on the resource collection path. + * Custom methods are not Create methods. + * + * @param {string} resourcePath a single resource path, a resource collection path or a singleton path + * @param {Object} allPathItems all path items + * @returns {boolean} + */ +export function hasStandardCreateMethod(resourcePath, allPathItems) { + if (isCustomMethodIdentifier(resourcePath)) { + return false; + } + const collectionPath = isSingleResourceIdentifier(resourcePath) + ? resourcePath.substring(0, resourcePath.lastIndexOf('/')) + : resourcePath; + const collectionPathItem = allPathItems[collectionPath]; + return collectionPathItem !== undefined && hasPostMethod(collectionPathItem); +} + /** * Checks if a path object has a DELETE method *