-
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.
parameters schema as type object (#731)
* initial draft * added rule to check if schema in parameters is of type object or not * added rules.md file * Highlighted type: object docs/parameters-schema-as-type-object.md Co-authored-by: Brett DeFoy <[email protected]> * Update docs/parameters-schema-as-type-object.md Co-authored-by: Brett DeFoy <[email protected]> * Update docs/parameters-schema-as-type-object.md Co-authored-by: Brett DeFoy <[email protected]> * Update docs/parameters-schema-as-type-object.md Co-authored-by: Brett DeFoy <[email protected]> * Update docs/parameters-schema-as-type-object.md Co-authored-by: Brett DeFoy <[email protected]> * updated the given condition to check body params from path * updated doc --------- Co-authored-by: Brett DeFoy <[email protected]>
- Loading branch information
1 parent
35f6dea
commit f514622
Showing
5 changed files
with
318 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,145 @@ | ||
# ParametersSchemaAsTypeObject | ||
|
||
## Category | ||
|
||
ARM Error | ||
|
||
## Applies to | ||
|
||
ARM OpenAPI(swagger) specs | ||
|
||
## Related ARM Guideline Code | ||
|
||
- RPC-Arg-V1-01 | ||
|
||
## Output Message | ||
|
||
The schema for body parameters must specify type:object and include a definition for its reference model. | ||
|
||
## Description | ||
|
||
The schema for body parameters should be defined strictly with the type set as 'object', as ARM does not support other types of definitions. This restriction is in place to ensure a better customer experience. | ||
|
||
## How to fix the violation | ||
|
||
Please set schema `"type": "object"` in the parameters and define the reference model of the object. | ||
|
||
## Good Example 1 | ||
|
||
```json | ||
... | ||
{ | ||
"parameters": { | ||
"ApiVersionParameter": { | ||
"name": "api-version", | ||
// not a body parameter | ||
"in": "query", | ||
"required": true, | ||
// no schema | ||
"type": "string", | ||
"description": "The HDInsight client API Version." | ||
} | ||
} | ||
} | ||
... | ||
``` | ||
|
||
## Good Example 2 | ||
|
||
```json | ||
... | ||
{ | ||
"parameters": { | ||
"ResourceGroupName": { | ||
"name": "resourceGroupName", | ||
// body parameter | ||
"in": "body", | ||
"required": true, | ||
"schema": { | ||
// refer schema from the definitons | ||
"$ref": "#/definitions/ResourceGroup" | ||
}, | ||
"description": "The name of the resource group.", | ||
"x-ms-parameter-location": "method" | ||
} | ||
} | ||
} | ||
... | ||
``` | ||
|
||
## Good Example 3 | ||
|
||
```json | ||
... | ||
{ | ||
"parameters": { | ||
"ResourceGroupName": { | ||
"name": "resourceGroupName", | ||
// body parameter | ||
"in": "body", | ||
"required": true, | ||
"schema": { | ||
// define schema with type:object | ||
"type": "object", | ||
"Resource": { | ||
"description": "The resource", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "The name of the Resource" | ||
} | ||
} | ||
} | ||
}, | ||
"description": "The name of the resource group.", | ||
"x-ms-parameter-location": "method" | ||
} | ||
} | ||
} | ||
... | ||
``` | ||
|
||
## Bad Example 1 | ||
|
||
```json | ||
... | ||
{ | ||
"parameters": { | ||
"ResourceGroupName": { | ||
"name": "resourceGroupName", | ||
"in": "body", | ||
"required": true, | ||
"schema": { | ||
// object not defined | ||
"type": "object", | ||
}, | ||
"description": "The name of the resource group.", | ||
"x-ms-parameter-location": "method" | ||
} | ||
} | ||
} | ||
... | ||
``` | ||
|
||
## Bad Example 2 | ||
|
||
```json | ||
... | ||
{ | ||
"parameters": { | ||
"ResourceGroupName": { | ||
"name": "resourceGroupName", | ||
"in": "body", | ||
"required": true, | ||
"schema": { | ||
// type:string is not allowed for body parameters | ||
"type": "string", | ||
}, | ||
"description": "The name of the resource group.", | ||
"x-ms-parameter-location": "method" | ||
} | ||
} | ||
} | ||
... | ||
``` |
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
137 changes: 137 additions & 0 deletions
137
packages/rulesets/src/spectral/test/parameters-schema-as-type-object.test.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,137 @@ | ||
import { Spectral } from "@stoplight/spectral-core" | ||
import linterForRule from "./utils" | ||
|
||
let linter: Spectral | ||
|
||
const ERROR_MESSAGE = "The schema for body parameters must specify type:object and include a definition for its reference model." | ||
|
||
beforeAll(async () => { | ||
linter = await linterForRule("ParametersSchemaAsTypeObject") | ||
return linter | ||
}) | ||
|
||
test("ParametersSchemaAsTypeObject should find errors", () => { | ||
const myOpenApiDocument = { | ||
swagger: "2.0", | ||
paths: { | ||
"/providers/Microsoft.ConnectedVMwarevSphere/virtualMachine/default": { | ||
get: { | ||
parameters: [ | ||
{ | ||
$ref: "#/parameters/LoadTestNameParameter", | ||
}, | ||
{ | ||
$ref: "#/parameters/QuotaBucketNameParameter", | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
parameters: { | ||
LoadTestNameParameter: { | ||
in: "body", | ||
name: "loadTestName", | ||
description: "Load Test name.", | ||
required: true, | ||
"x-ms-parameter-location": "method", | ||
schema: { | ||
type: "object", | ||
}, | ||
}, | ||
QuotaBucketNameParameter: { | ||
in: "body", | ||
name: "quotaBucketName", | ||
description: "Quota Bucket name.", | ||
required: true, | ||
"x-ms-parameter-location": "method", | ||
schema: { | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
} | ||
return linter.run(myOpenApiDocument).then((results) => { | ||
expect(results.length).toBe(1) | ||
expect(results[0].path.join(".")).toBe( | ||
"paths./providers/Microsoft.ConnectedVMwarevSphere/virtualMachine/default.get.parameters.1.schema.type", | ||
) | ||
expect(results[0].message).toContain(ERROR_MESSAGE) | ||
}) | ||
}) | ||
|
||
test("ParametersSchemaAsTypeObject should find no errors", () => { | ||
const myOpenApiDocument = { | ||
swagger: "2.0", | ||
"/providers/Microsoft.ConnectedVMwarevSphere/virtualMachine/default": { | ||
get: { | ||
parameters: [ | ||
{ | ||
$ref: "#/parameters/LoadTestNameParameter", | ||
}, | ||
{ | ||
$ref: "#/parameters/ResourceGroupName", | ||
}, | ||
{ | ||
$ref: "#/parameters/QuotaBucketNameParameter", | ||
}, | ||
], | ||
}, | ||
}, | ||
definitions: { | ||
ResourceGroup: { | ||
description: "The ResourceGroup model definition.", | ||
properties: { | ||
id: { | ||
readOnly: true, | ||
type: "string", | ||
description: "ResourceGroup Id", | ||
}, | ||
}, | ||
}, | ||
}, | ||
parameters: { | ||
LoadTestNameParameter: { | ||
in: "body", | ||
name: "loadTestName", | ||
description: "Load Test name.", | ||
required: true, | ||
"x-ms-parameter-location": "method", | ||
schema: { | ||
$ref: "#/definitions/ResourceGroup", | ||
}, | ||
}, | ||
QuotaBucketNameParameter: { | ||
in: "body", | ||
name: "quotaBucketName", | ||
description: "Quota Bucket name.", | ||
required: true, | ||
"x-ms-parameter-location": "method", | ||
type: "string", | ||
}, | ||
ResourceGroupName: { | ||
name: "resourceGroupName", | ||
in: "body", | ||
required: true, | ||
schema: { | ||
// define schema with type:object | ||
type: "object", | ||
Resource: { | ||
description: "The resource", | ||
type: "object", | ||
properties: { | ||
name: { | ||
type: "string", | ||
description: "The name of the Resource", | ||
}, | ||
}, | ||
}, | ||
}, | ||
description: "The name of the resource group.", | ||
"x-ms-parameter-location": "method", | ||
}, | ||
}, | ||
} | ||
return linter.run(myOpenApiDocument).then((results) => { | ||
expect(results.length).toBe(0) | ||
}) | ||
}) |