-
Notifications
You must be signed in to change notification settings - Fork 49
Add linter rule to suggest the scope parameter #765
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
bdefoy
wants to merge
13
commits into
main
Choose a base branch
from
bdefoy/suggest-scope-parameter
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eba3cae
Add linter rule to suggest the scope parameter
bdefoy 5dd16da
Merge branch 'main' into bdefoy/suggest-scope-parameter
bdefoy 9b4104e
fix build
bdefoy f8c9338
Merge branch 'bdefoy/suggest-scope-parameter' of https://github.com/A…
bdefoy e801dd4
fix lint
bdefoy 9a7c8bc
broken
bdefoy ec01852
fixed rule/test. need to clean up
bdefoy d6b4483
fix
bdefoy 60bf4d8
WIP
bdefoy 80219e2
Fix and add tests
bdefoy 1c40ee9
Remove TODO
bdefoy 4b0b2cd
Clean up test code
bdefoy 418e1b0
Edit change log
bdefoy 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# SuggestScopeParameter | ||
|
||
## Category | ||
|
||
ARM Warning | ||
|
||
## Applies to | ||
|
||
ARM OpenAPI(swagger) specs | ||
|
||
## Related ARM Guideline Code | ||
|
||
- N/a | ||
|
||
## Description | ||
|
||
OpenAPI authors can use the `scope` path parameter to indicate that an API is applicable to various scopes (Tenant, | ||
Management Group, Subscription, Resource Group, etc.) rather than explicitly defining each scope in the spec. | ||
|
||
## How to fix | ||
|
||
Remove all explicitly-scoped paths that only vary in scope and create a path with the `scope` parameter. | ||
|
||
Example of explicitly-scoped paths that only vary in scope: | ||
|
||
2. Explicitly-scoped path (by subscription) | ||
**`/subscriptions/{subscriptionId}`**`/providers/Microsoft.Bakery/breads` | ||
|
||
3. Explicitly-scoped path (by resource group): | ||
**`/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}`**`/providers/Microsoft.Bakery/breads` | ||
|
||
These two paths can be replaced with a single path that uses the `scope` parameter: | ||
|
||
1. Path with scope parameter: | ||
**`/{scope}`**`/providers/Microsoft.Bakery/breads` |
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
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
40 changes: 40 additions & 0 deletions
40
packages/rulesets/src/spectral/functions/suggest-scope-parameter.ts
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,40 @@ | ||
/** | ||
* Suggests combining paths that differ only in scope by defining a scope parameter. | ||
* | ||
* This function checks if the given path can be combined with other paths in the Swagger document | ||
* by introducing a scope parameter. It returns suggestions for paths that differ only in scope. | ||
*/ | ||
|
||
const suggestScopeParameter = (path: any, _opts: any, ctx: any) => { | ||
const swagger = ctx?.documentInventory?.resolved | ||
|
||
let lowerCasePath: string | ||
|
||
if ( | ||
path === null || | ||
typeof path !== "string" || | ||
path.length === 0 || | ||
swagger === null || | ||
(lowerCasePath = path.toLocaleLowerCase()).includes("{scope}") | ||
) { | ||
return [] | ||
} | ||
|
||
const suffix = path.substring(path.lastIndexOf("/providers")).toLocaleLowerCase() | ||
|
||
// Find all paths that differ only in scope | ||
const matchingPaths = Object.keys(swagger.paths).filter((p: string) => { | ||
const lower = p.toLocaleLowerCase() | ||
return !lower.includes("{scope}") && lower !== lowerCasePath && lower.endsWith(suffix) | ||
}) | ||
|
||
return matchingPaths.map((match: string) => { | ||
return { | ||
// note: only matched path is included in the message so that Spectral can deduplicate errors | ||
message: `Path with suffix "${suffix}" differs from another path only in scope. These paths share a suffix and can be combined by defining a scope parameter.`, | ||
path: ctx.path, | ||
} | ||
}) | ||
} | ||
|
||
export default suggestScopeParameter |
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.
This may not be a good idea from a customer viewpoint as it makes it less obvious as to what scopes are truly supported. Before we decide one way or the other, lets first check how TypeSpec generates the different API paths for this scenario and align the linter rules to work with that.
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 don't see support in TypeSpec for the scope parameter, but I'm also not seeing how you can generate a spec with a resource at multiple scopes aside from extension resources. I might look into this a bit more and follow up
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'm not sure that there is any use of a scope parameter outside of extension resources. I have not been able to find one in the private API specs repo yet, and I'm not sure how it would fit with the RPC.
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.
The scenario described in #750 is a rare case where an RP is attempting to basically duplicate endpoints. I will change the rule to look for duplicate endpoints like this.