-
Notifications
You must be signed in to change notification settings - Fork 9
Request: Adding new configurable rule: operation-operationId-exclude-OPTIONS
#52
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,154 @@ | ||||||
| # Variant of the `operation-operationId` rule that excludes `OPTIONS` operations | ||||||
|
|
||||||
| Author: | ||||||
| - [@nickcorby](https://github.com/nickcorby) | ||||||
|
|
||||||
| ## What this does and why | ||||||
|
|
||||||
| This rule extends the built-in rule [operation-operationId](https://redocly.com/docs/cli/rules/oas/operation-operationId) by filtering out set operations e.g. `OPTIONS`. | ||||||
|
|
||||||
| This rule can be tweaked to exclude any combination of operations, or inversely to **include** set operations. | ||||||
|
|
||||||
| ## Code | ||||||
|
|
||||||
| > If you are using a ruleset that includes the `operation-operationId` rule (like `recommended`), you will need to disable this rule in order for the configurable rule to apply. | ||||||
| > | ||||||
| > e.g. if your `redocly.yaml` file contains: | ||||||
| > | ||||||
| > ```yaml | ||||||
| > extends: | ||||||
| > - recommended | ||||||
| > ``` | ||||||
| > You will need to include the following snippet in the `rules` property: | ||||||
| > | ||||||
| > ```yaml | ||||||
| > rules: | ||||||
| > operation-operationId: off | ||||||
| > ``` | ||||||
|
|
||||||
| This rule is added to your `redocly.yaml` file: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ```yaml | ||||||
| rule/operation-operationId-exclude-OPTIONS: | ||||||
| subject: | ||||||
| type: Operation | ||||||
| filterOutParentKeys: | ||||||
| - options | ||||||
| assertions: | ||||||
| required: | ||||||
| - operationId | ||||||
| message: Operation is missing 'operationId' property. | ||||||
| severity: error | ||||||
| ``` | ||||||
|
|
||||||
| The `filterOutParentKeys` property allows you to define which operations you would like to exclude from the rule: | ||||||
|
|
||||||
| ```yaml | ||||||
| type: Operation | ||||||
| filterOutParentKeys: | ||||||
| - options | ||||||
| ``` | ||||||
|
|
||||||
| This can be extended to multiple operations: | ||||||
|
|
||||||
| ```yaml | ||||||
| type: Operation | ||||||
| filterOutParentKeys: | ||||||
| - options | ||||||
| - delete | ||||||
| ``` | ||||||
|
|
||||||
| Alternatively, you can use the `filterInParentKeys` property to define which operations you would like to include in the rule: | ||||||
|
|
||||||
| ```yaml | ||||||
| type: Operation | ||||||
| filterInParentKeys: | ||||||
| - get | ||||||
| - post | ||||||
| ``` | ||||||
|
|
||||||
| ## Examples | ||||||
|
|
||||||
| With the rule configured with `severity: error`, the following snippet of a `GET` operation will trigger this rule & display an error: | ||||||
|
|
||||||
| ```yaml | ||||||
| get: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you make your examples smaller just to contain the needed data to showcase the rule, and start from the root? openapi: 3.2.0
paths:
/example:
get: ... |
||||||
| tags: | ||||||
| - Endpoint | ||||||
| summary: "Example endpoint." | ||||||
| parameters: | ||||||
| - name: "Authorization" | ||||||
| in: "header" | ||||||
| description: "Auth Token" | ||||||
| required: true | ||||||
| schema: | ||||||
| type: "string" | ||||||
| responses: | ||||||
| "200": | ||||||
| description: "200 response" | ||||||
| headers: | ||||||
| Access-Control-Allow-Origin: | ||||||
| schema: | ||||||
| type: "string" | ||||||
| content: | ||||||
| application/json: | ||||||
| schema: | ||||||
| $ref: ../models/200Response.yaml | ||||||
| "400": | ||||||
| description: "400 response" | ||||||
| headers: | ||||||
| Access-Control-Allow-Origin: | ||||||
| schema: | ||||||
| type: "string" | ||||||
| content: | ||||||
| application/json: | ||||||
| schema: | ||||||
| $ref: ../models/ErrorResponse.yaml | ||||||
| security: | ||||||
| - Authorizer: [] | ||||||
| - api_key: [] | ||||||
| ``` | ||||||
|
|
||||||
| However, the following snippet of an `OPTIONS` operation **won't** trigger the rule: | ||||||
|
|
||||||
| ```yaml | ||||||
| options: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps you can combine the examples into one endpoint, but with different methods, and leave hints what's correct and what's wrong in summaries or descriptions. |
||||||
| tags: | ||||||
| - Endpoint | ||||||
| summary: "Example endpoint." | ||||||
| parameters: | ||||||
| - name: "Authorization" | ||||||
| in: "header" | ||||||
| description: "Auth Token" | ||||||
| required: true | ||||||
| schema: | ||||||
| type: "string" | ||||||
| responses: | ||||||
| "200": | ||||||
| description: "200 response" | ||||||
| headers: | ||||||
| Access-Control-Allow-Origin: | ||||||
| schema: | ||||||
| type: "string" | ||||||
| content: | ||||||
| application/json: | ||||||
| schema: | ||||||
| $ref: ../models/200Response.yaml | ||||||
| "400": | ||||||
| description: "400 response" | ||||||
| headers: | ||||||
| Access-Control-Allow-Origin: | ||||||
| schema: | ||||||
| type: "string" | ||||||
| content: | ||||||
| application/json: | ||||||
| schema: | ||||||
| $ref: ../models/ErrorResponse.yaml | ||||||
| security: | ||||||
| - Authorizer: [] | ||||||
| - api_key: [] | ||||||
| ``` | ||||||
|
|
||||||
| ## References | ||||||
|
|
||||||
| Assisted by [AlbinaBlazhko17](https://github.com/AlbinaBlazhko17) | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| rule/operation-operationId-exclude-OPTIONS: | ||
| subject: | ||
| type: Operation | ||
| filterOutParentKeys: | ||
| - options | ||
| assertions: | ||
| required: | ||
| - operationId | ||
| message: Operation is missing 'operationId' property. | ||
| severity: error |
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.
Technically, the configurable rule will be applied anyway, but it just doesn't make any sense to use both built-in and the configurable rule 🙂.