-
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
Open
julius-jogela
wants to merge
7
commits into
main
Choose a base branch
from
feat-ipa-132-operations-resource-rules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 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
215 changes: 215 additions & 0 deletions
215
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,215 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| const READ_ONLY_SCHEMA_ERROR_MESSAGE = | ||
| 'The Operation resource must be read-only. All properties of the GET response schema must be marked as readOnly: true.'; | ||
|
|
||
| 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 methods other than get', | ||
| document: { | ||
| paths: { | ||
| '/api/atlas/v2/resourceName/operations': { | ||
| get: readOnlyGet, | ||
| post: {}, | ||
| }, | ||
| '/api/atlas/v2/resourceName/operations/{operationId}': { | ||
| get: readOnlyGet, | ||
| put: {}, | ||
| patch: {}, | ||
| delete: {}, | ||
| head: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| 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, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: 'Operations endpoints are read-only and do not allow the head method.', | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/{operationId}', 'head'], | ||
| 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 resources with properties that are not readOnly, checked per path item', | ||
| 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: READ_ONLY_SCHEMA_ERROR_MESSAGE, | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-132-operation-must-be-a-read-only-resource', | ||
| message: READ_ONLY_SCHEMA_ERROR_MESSAGE, | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/{operationId}', 'get'], | ||
| 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, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
| 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}': {}, | ||
| // Nesting below the first operations segment is a violation even when the path ends in | ||
| // another well-formed operations suffix | ||
| '/api/atlas/v2/resourceName/operations/{operationId}/operations': {}, | ||
| }, | ||
| }, | ||
| 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, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-132-operations-endpoint-must-be-a-leaf-resource', | ||
| message: ERROR_MESSAGE, | ||
| path: ['paths', '/api/atlas/v2/resourceName/operations/{operationId}/operations'], | ||
| 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.