Skip to content
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

Added new rule LroAzureAsyncOperationHeader #749

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions docs/lro-azure-async-operation-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# LroAzureAsyncOperationHeader

## Category

ARM Error

## Applies to

ARM OpenAPI(swagger) specs

## Related ARM Guideline Code

- RPC-Async-V1-06

## Output Message

A 202 response should include an Azure-AsyncOperation response header.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A 202 response should include an Azure-AsyncOperation response header.
A 202 response must include an Azure-AsyncOperation response header.


## Description

Azure-AsyncOperation header must be supported for all async operations that return 202.

## CreatedAt

Oct 11, 2024

## How to fix the violation

Adding the Azure-AsyncOperation header schema to the 202 response header schema.

## Good Example

```json
"/api/configServers": {
put: {
operationId: "ConfigServers_Update",
responses: {
202: {
description: 'Accepted',
headers: {
'Azure-AsyncOperation': {
type: 'string',
},
},
},
},
},
},
```

## Bad Example

```json
"/api/configServers": {
put: {
operationId: "ConfigServers_Update",
responses: {
"202": {
description: "Success",
headers: {
//No Azure-AsyncOperation header
},
},
},
},
},
```
6 changes: 6 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@ For Data plane spec, the allowed response status codes for a long DELETE operati

Please refer to [long-running-response-status-code-data-plane.md](./long-running-response-status-code-data-plane.md) for details.

### LroAzureAsyncOperationHeader

Azure-AsyncOperation header must be supported for all async operations that return 202.

Please refer to [lro-azure-async-operation-header.md](./lro-azure-async-operation-header.md) for details.

### LroErrorContent

Error response content of long running operations must follow the error schema provided in the common types v2 and above.
Expand Down
14 changes: 14 additions & 0 deletions packages/rulesets/generated/spectral/az-arm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3301,6 +3301,20 @@ const ruleset = {
function: provisioningState,
},
},
LroAzureAsyncOperationHeader: {
rpcGuidelineCode: "RPC-Async-V1-06",
description: "Azure-AsyncOperation header must be supported for all async operations that return 202.",
message: "{{description}}",
severity: "error",
formats: [oas2],
given: "$.paths[*][*].responses[?(@property == '202')]",
then: {
function: hasHeader,
functionOptions: {
name: "Azure-AsyncOperation",
},
},
},
LroLocationHeader: {
rpcGuidelineCode: "RPC-Async-V1-07",
description: "Location header must be supported for all async operations that return 202.",
Expand Down
16 changes: 16 additions & 0 deletions packages/rulesets/src/spectral/az-arm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ const ruleset: any = {
},
},

// RPC Code: RPC-Async-V1-06
LroAzureAsyncOperationHeader: {
rpcGuidelineCode: "RPC-Async-V1-06",
description: "Azure-AsyncOperation header must be supported for all async operations that return 202.",
message: "{{description}}",
severity: "error",
formats: [oas2],
given: "$.paths[*][*].responses[?(@property == '202')]",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s[?(@Property == '202')

This rule is actually for all long running operations. Not just for the 202 status code. Sorry I should have clarified this earlier.

then: {
function: hasheader,
functionOptions: {
name: "Azure-AsyncOperation",
},
},
},

// RPC Code: RPC-Async-V1-07
LroLocationHeader: {
rpcGuidelineCode: "RPC-Async-V1-07",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { Spectral } from "@stoplight/spectral-core"
import linterForRule from "./utils"

let linter: Spectral

beforeAll(async () => {
linter = await linterForRule("LroAzureAsyncOperationHeader")
return linter
})

const ERROR_MESSAGE = "Azure-AsyncOperation header must be supported for all async operations that return 202."

test("LroAzureAsyncOperationHeader should find no errors", () => {
const myOpenApiDocument = {
swagger: "2.0",
paths: {
"/foo1/operations": {
get: {
operationId: "foo_get",
responses: {
200: {
description: "Success",
},
202: {
description: "Accepted",
headers: {
"Azure-AsyncOperation": {
description: "The URL where the status of the asynchronous operation can be checked.",
type: "string",
},
},
},
default: {
description: "Error",
},
},
},
post: {
operationId: "foo_post",
responses: {
200: {
description: "Success",
},
202: {
description: "Accepted",
headers: {
"Azure-asyncoperation": {
description: "The URL where the status of the asynchronous operation can be checked.",
type: "string",
},
},
},
default: {
description: "Error",
},
},
},
put: {
operationId: "foo_put",
responses: {
200: {
description: "Success",
},
202: {
description: "Accepted",
headers: {
"azure-asyncOperation": {
description: "The URL where the status of the asynchronous operation can be checked.",
type: "string",
},
},
},
default: {
description: "Error",
},
},
},
},
},
}
return linter.run(myOpenApiDocument).then((results) => {
expect(results.length).toBe(0)
})
})

test("LroAzureAsyncOperationHeader should find errors with no Azure-AsyncOperation header", () => {
const myOpenApiDocument = {
swagger: "2.0",
paths: {
"/foo1/operations": {
get: {
operationId: "foo_get",
responses: {
200: {
description: "Success",
},
202: {
description: "Accepted",
headers: {
Location: {
description: "The URL where the status of the asynchronous operation can be checked.",
type: "string",
},
},
},
default: {
description: "Error",
},
},
},
post: {
operationId: "foo_post",
responses: {
200: {
description: "Success",
},
202: {
description: "Accepted",
headers: {},
},
default: {
description: "Error",
},
},
},
put: {
operationId: "foo_put",
responses: {
200: {
description: "Success",
},
202: {
description: "Accepted",
headers: {
azureasyncOperation: {
description: "The URL where the status of the asynchronous operation can be checked.",
type: "string",
},
},
},
default: {
description: "Error",
},
},
},
},
},
}
return linter.run(myOpenApiDocument).then((results) => {
expect(results.length).toBe(3)
expect(results[0].path.join(".")).toBe("paths./foo1/operations.get.responses.202.headers")
expect(results[0].message).toEqual(ERROR_MESSAGE)
expect(results[1].path.join(".")).toBe("paths./foo1/operations.post.responses.202.headers")
expect(results[1].message).toEqual(ERROR_MESSAGE)
expect(results[2].path.join(".")).toBe("paths./foo1/operations.put.responses.202.headers")
expect(results[2].message).toEqual(ERROR_MESSAGE)
})
})