-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-313321: Add a new Spectral lint rule that validates that at any time, an endpoint can only have one API version marked as “upcoming” #697
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 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2b1d005
Implemented spectral lint rule that validates that at any time, an en…
jeroenvervaeke 3b684bf
ran prettier
jeroenvervaeke f7e2a1a
Added unit tests for new check + added .tool-versions for asdf
jeroenvervaeke bcb61b8
ran prettier
jeroenvervaeke 0dc6831
fix eslint
jeroenvervaeke 257aad3
second attempt to fix linting
jeroenvervaeke b810f8f
third attempt at fixing eslint
jeroenvervaeke 160ec80
undo linting changes, renamed test to end with .test.js
jeroenvervaeke 96b5eac
undo rename
jeroenvervaeke 91b1195
Merge branch 'main' into CLOUDP-313321
jeroenvervaeke 99a260a
files are provided using cli, we don't need this config that override…
jeroenvervaeke c5421c0
fix ignores
jeroenvervaeke 2f62801
fixed package.json
jeroenvervaeke 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
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
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 @@ | ||
| nodejs 24.0.2 |
99 changes: 99 additions & 0 deletions
99
tools/spectral/__tests__/acceptHeaderUpcomingVersionLimit.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,99 @@ | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
| import acceptHeaderUpcomingVersionLimit from '../functions/acceptHeaderUpcomingVersionLimit'; | ||
|
|
||
| describe('accept-header-upcoming-version-limit', () => { | ||
| it('valid: no upcoming Accept headers', () => { | ||
| const operation = { | ||
| operationId: 'getTest', | ||
| responses: { | ||
| 200: { | ||
| content: { | ||
| 'application/json': {}, | ||
| }, | ||
| }, | ||
| }, | ||
| requestBody: { | ||
| content: { | ||
| 'application/json': {}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = acceptHeaderUpcomingVersionLimit(operation); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('valid: one upcoming Accept header in response', () => { | ||
| const operation = { | ||
| operationId: 'getTest', | ||
| responses: { | ||
| 200: { | ||
| content: { | ||
| 'application/vnd.atlas.2024-06-01.upcoming+json': {}, | ||
| 'application/json': {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = acceptHeaderUpcomingVersionLimit(operation); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('invalid: two upcoming Accept headers in response', () => { | ||
| const operation = { | ||
| operationId: 'getTest', | ||
| responses: { | ||
| 200: { | ||
| content: { | ||
| 'application/vnd.atlas.2024-06-01.upcoming+json': {}, | ||
| 'application/vnd.atlas.2024-07-01.upcoming+json': {}, | ||
| 'application/json': {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = acceptHeaderUpcomingVersionLimit(operation); | ||
| expect(result).toEqual([ | ||
| { | ||
| message: expect.stringMatching(/Found 2 upcoming API Accept headers/), | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('invalid: two upcoming Accept headers in request', () => { | ||
| const operation = { | ||
| operationId: 'postTest', | ||
| requestBody: { | ||
| content: { | ||
| 'application/vnd.atlas.2024-06-01.upcoming+json': {}, | ||
| 'application/vnd.atlas.2024-07-01.upcoming+json': {}, | ||
| 'application/json': {}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = acceptHeaderUpcomingVersionLimit(operation); | ||
| expect(result).toEqual([ | ||
| { | ||
| message: expect.stringMatching(/Found 2 upcoming API Accept headers/), | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('invalid: missing operationId', () => { | ||
| const operation = { | ||
| responses: { | ||
| 200: { | ||
| content: { | ||
| 'application/vnd.atlas.2024-06-01.upcoming+json': {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = acceptHeaderUpcomingVersionLimit(operation); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
| }); |
41 changes: 41 additions & 0 deletions
41
tools/spectral/functions/acceptHeaderUpcomingVersionLimit.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,41 @@ | ||
| module.exports = function (input) { | ||
| // Get operationId from context, if this fails return an error | ||
| const operationId = input.operationId; | ||
| if (!operationId) { | ||
| return; | ||
| } | ||
|
|
||
| // List of errors | ||
| const errors = []; | ||
|
|
||
| // Validate versions in 200 responses | ||
| const responseErr = validateContent(operationId, 'response', input?.responses?.[200]?.content); | ||
| if (responseErr != null) errors.push(responseErr); | ||
|
|
||
| // Validate versions in requests | ||
| const requestErr = validateContent(operationId, 'request', input?.requestBody?.content); | ||
| if (requestErr != null) errors.push(requestErr); | ||
|
|
||
| return errors.length > 0 ? errors : undefined; | ||
| }; | ||
|
|
||
| // Check for upcoming API Accept headers | ||
| const upcomingRegex = /^application\/vnd\.atlas\.\d{4}-\d{2}-\d{2}\.upcoming\+.+$/; | ||
|
|
||
| function validateContent(operationId, section, content) { | ||
| if (content == null) { | ||
| return null; | ||
| } | ||
|
|
||
| const contentTypes = Object.keys(content); | ||
| const upcomingContentTypes = contentTypes.filter((k) => upcomingRegex.test(k)); | ||
| // If there's less than or equal to one upcoming header then the operation is valid | ||
| if (upcomingContentTypes.length <= 1) { | ||
| return null; | ||
| } | ||
|
|
||
| // Return an error message | ||
| return { | ||
| message: `OperationId: ${operationId} - Found ${upcomingContentTypes.length} upcoming API Accept headers (section: ${section}): ${upcomingContentTypes.join(', ')}`, | ||
| }; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have unit tests like we have for ipa rule sets?
https://github.com/mongodb/openapi/tree/main/tools/spectral/ipa/__tests__
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed those, didn't know it was possible! Thanks