-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new rule ValidQueryParametersForPointOperations (#746)
Co-authored-by: akhilailla <akhilailla@microsoft.com>
- Loading branch information
1 parent
a954384
commit c26c69a
Showing
9 changed files
with
615 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# trackedExtensionResourcesAreNotAllowed | ||
# TrackedExtensionResourcesAreNotAllowed | ||
|
||
## Category | ||
|
||
|
This file contains 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,107 @@ | ||
# ValidQueryParametersForPointOperations | ||
|
||
## Category | ||
|
||
ARM Error | ||
|
||
## Applies to | ||
|
||
ARM OpenAPI(swagger) specs | ||
|
||
## Related ARM Guideline Code | ||
|
||
- RPC-Uri-V1-13 | ||
|
||
## Description | ||
|
||
Point operations (GET, PUT, PATCH, DELETE) must not include any query parameters other than api-version. | ||
|
||
## How to fix the violation | ||
|
||
Remove all query params other than api-version for point operations (GET, PUT, PATCH, DELETE). | ||
|
||
## Good Examples | ||
|
||
```json | ||
"Microsoft.Music/Songs": { | ||
"get": { | ||
"operationId": "Foo_Get", | ||
"description": "Test Description", | ||
"parameters": [ | ||
{ | ||
"name": "api-version", | ||
"in": "query" | ||
}, | ||
], | ||
}, | ||
"put": { | ||
"operationId": "Foo_Update", | ||
"description": "Test Description", | ||
"parameters": [ | ||
{ | ||
"name": "api-version", | ||
"in": "query" | ||
}, | ||
], | ||
}, | ||
"patch": { | ||
"operationId": "Foo_Update", | ||
"description": "Test Description", | ||
"parameters": [ | ||
{ | ||
"name": "api-version", | ||
"in": "query" | ||
}, | ||
], | ||
}, | ||
"delete": { | ||
"operationId": "Foo_Update", | ||
"description": "Test Description", | ||
"parameters": [ | ||
{ | ||
"name": "api-version", | ||
"in": "query" | ||
}, | ||
], | ||
}, | ||
}, | ||
``` | ||
|
||
## Bad Examples | ||
|
||
```json | ||
"Microsoft.Music/Songs": { | ||
"get": { | ||
"operationId": "Foo_get", | ||
"description": "Test Description", | ||
"parameters": [ | ||
{ | ||
"name": "name", | ||
"in": "query", | ||
"required": false, | ||
"type": "string", | ||
}, | ||
], | ||
}, | ||
"put": { | ||
"operationId": "Foo_Create", | ||
"description": "Test Description", | ||
"parameters": [ | ||
{ | ||
"name": "$filter", | ||
"in": "query" | ||
}, | ||
], | ||
}, | ||
"patch": { | ||
"operationId": "Foo_Update", | ||
"description": "Test Description", | ||
"parameters": [ | ||
{ | ||
"name": "name", | ||
"in": "query" | ||
}, | ||
], | ||
}, | ||
}, | ||
``` |
This file contains 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 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 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
37 changes: 37 additions & 0 deletions
37
packages/rulesets/src/spectral/functions/valid-query-parameters-for-point-operations.ts
This file contains 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,37 @@ | ||
import { isPointOperation } from "./utils" | ||
|
||
export const validQueryParametersForPointOperations = (pathItem: any, _opts: any, ctx: any) => { | ||
if (pathItem === null || typeof pathItem !== "object") { | ||
return [] | ||
} | ||
|
||
const path = ctx.path || [] | ||
const uris = Object.keys(pathItem) | ||
if (uris.length < 1) { | ||
return [] | ||
} | ||
const pointOperations = new Set(["get", "put", "patch", "delete"]) | ||
const errors: any[] = [] | ||
|
||
for (const uri of uris) { | ||
//check if the path is a point operation | ||
if (isPointOperation(uri)) { | ||
const verbs = Object.keys(pathItem[uri]) | ||
for (const verb of verbs) { | ||
//check query params only for point operations/verbs | ||
if (pointOperations.has(verb)) { | ||
const params = pathItem[uri][verb]["parameters"] | ||
const queryParams = params?.filter((param: { in: string; name: string }) => param.in === "query" && param.name !== "api-version") | ||
queryParams?.map((param: { name: any }) => { | ||
errors.push({ | ||
message: `Query parameter ${param.name} should be removed. Point operation '${verb}' MUST not have query parameters other than api-version.`, | ||
path: [path, uri, verb, "parameters"], | ||
}) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return errors | ||
} |
This file contains 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
Oops, something went wrong.