-
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 QueryParametersInCollectionGet linter rule
- Loading branch information
akhilailla
committed
Sep 12, 2024
1 parent
a19b89c
commit 87c755c
Showing
6 changed files
with
585 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# QueryParametersInCollectionGet | ||
|
||
## Category | ||
|
||
ARM Error | ||
|
||
## Applies to | ||
|
||
ARM OpenAPI(swagger) specs | ||
|
||
## Related ARM Guideline Code | ||
|
||
- RPC-Get-V1-15 | ||
|
||
## Description | ||
|
||
Collection Get's/List operations MUST not have query parameters other than api-version & OData filter. | ||
|
||
## How to fix the violation | ||
|
||
Ensure that collection GET/List operations do not include any query parameters other than api-version and the OData $filter. | ||
|
||
## Good Examples | ||
|
||
```json | ||
"Microsoft.Music/Songs": { | ||
get: { | ||
operationId: "Foo_Update", | ||
description: "Test Description", | ||
parameters: [ | ||
{ | ||
name: "api-version", | ||
in: "query" | ||
}, | ||
], | ||
}, | ||
}, | ||
``` | ||
|
||
```json | ||
"Microsoft.Music/Songs": { | ||
get: { | ||
operationId: "Foo_Update", | ||
description: "Test Description", | ||
parameters: [ | ||
{ | ||
name: "api-version", | ||
in: "query" | ||
}, | ||
{ | ||
name: "$filter", | ||
in: "query", | ||
required: false, | ||
type: "string", | ||
}, | ||
], | ||
}, | ||
}, | ||
``` | ||
|
||
## Bad Examples | ||
|
||
```json | ||
"Microsoft.Music/Songs": { | ||
get: { | ||
operationId: "Foo_Update", | ||
description: "Test Description", | ||
parameters: [ | ||
{ | ||
name: "name", | ||
in: "query", | ||
required: false, | ||
type: "string", | ||
}, | ||
], | ||
}, | ||
}, | ||
``` |
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
38 changes: 38 additions & 0 deletions
38
packages/rulesets/src/spectral/functions/query-parameters-in-collection-get.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,38 @@ | ||
import { getResourcesPathHierarchyBasedOnResourceType } from "./utils" | ||
|
||
export const queryParametersInCollectionGet = (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 GET = "get" | ||
const errors: any[] = [] | ||
|
||
for (const uri of uris) { | ||
// skip, if GET op isn't defined | ||
if (!pathItem[uri][GET]) { | ||
continue | ||
} | ||
// check if the GET op is a collection get/list call | ||
const hierarchy = getResourcesPathHierarchyBasedOnResourceType(uri) | ||
if (hierarchy.length == 0 && pathItem[uri][GET]) { | ||
const params = pathItem[uri][GET]["parameters"] | ||
const queryParams = params?.filter( | ||
(param: { in: string; name: string }) => param.in === "query" && param.name !== "api-version" && param.name !== "$filter", | ||
) | ||
queryParams?.forEach((param: { name: any }) => { | ||
errors.push({ | ||
message: `Query parameter ${param.name} should be removed. Collection Get's/List operation MUST not have query parameters other than api version & OData filter.`, | ||
path: [path, uri, GET, "parameters"], | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
return errors | ||
} |
Oops, something went wrong.