-
Notifications
You must be signed in to change notification settings - Fork 18
feat(ipa): implement IPA-132 ruleset and base Operations resource rules #1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0adb887
feat(ipa): implement IPA-132 ruleset and base Operations resource rules
julius-jogela 9eb5a08
fix(ipa): address IPA-132 review feedback
julius-jogela 27fa639
fix(ipa): keep custom method paths out of IPA-132 scope
julius-jogela 07d5ad0
fix(ipa): apply IPA-132 review suggestions for util reuse
julius-jogela 1a4a51d
fix(ipa): allow only get on Operations endpoints and harden the IPA-1…
julius-jogela 67b8faf
fix(ipa): check the readOnly Operation schema on the single Operation…
julius-jogela 5880330
fix(ipa): validate Operations endpoint methods from the defined path …
julius-jogela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
200 changes: 200 additions & 0 deletions
200
tools/spectral/ipa/__tests__/IPA132OperationMustBeAReadOnlyResource.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| const readOnlyGet = { | ||
| responses: { | ||
| 200: { | ||
| content: { | ||
| 'application/vnd.atlas.2024-08-05+json': { | ||
| schema: { | ||
| properties: { | ||
| id: { readOnly: true }, | ||
| status: { readOnly: true }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const nonReadOnlyGet = { | ||
| responses: { | ||
| 200: { | ||
| content: { | ||
| 'application/vnd.atlas.2024-08-05+json': { | ||
| schema: { | ||
| properties: { | ||
| id: { readOnly: true }, | ||
| status: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| testRule('xgen-IPA-132-operation-must-be-a-read-only-resource', [ | ||
| { | ||
| name: 'valid read-only Operations endpoints', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations': { | ||
| get: readOnlyGet, | ||
| }, | ||
| '/api/atlas/v2/resourceName/operations/{operationId}': { | ||
| get: readOnlyGet, | ||
| }, | ||
| '/api/atlas/v2/resourceName/{pathParam}/operations': { | ||
| get: readOnlyGet, | ||
| }, | ||
| '/api/atlas/v2/resourceName/{pathParam}/operations/{operationId}': { | ||
| get: readOnlyGet, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'paths that are not Operations endpoints are ignored', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName': { | ||
| post: {}, | ||
| }, | ||
| '/api/atlas/v2/resourceName/{pathParam}': { | ||
| put: {}, | ||
| patch: {}, | ||
| delete: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid Operations endpoints with mutating methods', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations': { | ||
| get: readOnlyGet, | ||
| post: {}, | ||
| }, | ||
| '/api/atlas/v2/resourceName/operations/{operationId}': { | ||
| get: readOnlyGet, | ||
| put: {}, | ||
| patch: {}, | ||
| delete: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: 'Operations endpoints are read-only and do not allow the post method.', | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations', 'post'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: 'Operations endpoints are read-only and do not allow the put method.', | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/{operationId}', 'put'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: 'Operations endpoints are read-only and do not allow the patch method.', | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/{operationId}', 'patch'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: 'Operations endpoints are read-only and do not allow the delete method.', | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/{operationId}', 'delete'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid Operations endpoint with a declared but empty method', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations': { | ||
| get: readOnlyGet, | ||
| post: null, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: 'Operations endpoints are read-only and do not allow the post method.', | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations', 'post'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid Operation resource with properties that are not readOnly', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations': { | ||
| get: nonReadOnlyGet, | ||
| }, | ||
| '/api/atlas/v2/resourceName/operations/{operationId}': { | ||
| get: nonReadOnlyGet, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: | ||
| 'The Operation resource must be read-only. All properties of the GET response schema must be marked as readOnly: true.', | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid Operations endpoints with exceptions', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations': { | ||
| get: readOnlyGet, | ||
| post: {}, | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-132-operation-must-be-a-read-only-resource': 'reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'read-only Operations endpoints do not need an exception', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations': { | ||
| get: readOnlyGet, | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-132-operation-must-be-a-read-only-resource': 'reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: 'This component adopts the rule and does not need an exception. Please remove the exception.', | ||
| path: [ | ||
| 'paths', | ||
| '/api/atlas/v2/resourceName/operations', | ||
| 'x-xgen-IPA-exception', | ||
| 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| ], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
102 changes: 102 additions & 0 deletions
102
tools/spectral/ipa/__tests__/IPA132OperationsEndpointMustBeALeafResource.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| const ERROR_MESSAGE = | ||
| 'Operations endpoints must be leaf resources. An `operations` segment may only be followed by a single operation identifier path parameter.'; | ||
|
|
||
| testRule('xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource', [ | ||
| { | ||
| name: 'valid leaf Operations endpoints', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations': {}, | ||
| '/api/atlas/v2/resourceName/operations/{operationId}': {}, | ||
| '/api/atlas/v2/resourceName/{pathParam}/operations': {}, | ||
| '/api/atlas/v2/resourceName/{pathParam}/operations/{operationId}': {}, | ||
| // Root-level Operations endpoints are leaves, their nesting is covered by | ||
| // xgen-IPA-132-operations-endpoint-must-not-be-global | ||
| '/api/atlas/v2/operations': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'paths without an operations segment are ignored', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName': {}, | ||
| '/api/atlas/v2/resourceName/{pathParam}': {}, | ||
| '/api/atlas/v2/resourceName/{pathParam}/childResource': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid paths nested below Operations endpoints', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations/subresource': {}, | ||
| '/api/atlas/v2/resourceName/operations/{operationId}/subresource': {}, | ||
| '/api/atlas/v2/resourceName/operations/{operationId}/{anotherId}': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource', | ||
| message: ERROR_MESSAGE, | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/subresource'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource', | ||
| message: ERROR_MESSAGE, | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/{operationId}/subresource'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource', | ||
| message: ERROR_MESSAGE, | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/{operationId}/{anotherId}'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid paths with exceptions', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations/{operationId}/subresource': { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource': 'reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'leaf Operations endpoints do not need an exception', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations/{operationId}': { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource': 'reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource', | ||
| message: 'This component adopts the rule and does not need an exception. Please remove the exception.', | ||
| path: [ | ||
| 'paths', | ||
| '/api/atlas/v2/resourceName/operations/{operationId}', | ||
| 'x-xgen-IPA-exception', | ||
| 'xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource', | ||
| ], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.