Skip to content

Commit

Permalink
feat: create new rule to enforce array parameters format (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorKarpiuk authored Nov 24, 2023
1 parent cf35f76 commit efb6a45
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/silver-singers-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": minor
"@redocly/cli": minor
---

Added new rule `array-parameter-serialization` to require that serialization parameters `style` and `explode` are present on array parameters.
109 changes: 109 additions & 0 deletions docs/rules/array-parameter-serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
slug: /docs/cli/rules/array-parameter-serialization
---

# array-parameter-serialization

Enforces the inclusion of `style` and `explode` fields for parameters with array type or parameters with a schema that includes `items` or `prefixItems`.

| OAS | Compatibility |
| --- | ------------- |
| 2.0 ||
| 3.0 ||
| 3.1 ||

```mermaid
flowchart TD
root ==> Paths --> PathItem --> Operation --> Parameter --enforces style and explode fields for array types--> Schema
PathItem --> Parameter
NamedParameter --> Parameter
root ==> components
subgraph components
NamedParameter
end
style Parameter fill:#codaf9,stroke:#0044d4,stroke-width:5px
style Schema fill:#codaf9,stroke:#0044d4,stroke-width:5px
```

## API design principles

Specifying serialization details consistently helps developers understand how to interact with the API effectively.

## Configuration

| Option | Type | Description |
| -------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- |
| severity | string | Possible values: `off`, `warn`, `error`. Default `off`. |
| in | [string] | List of valid parameter locations where the rule should be enforced. By default the rule applies to parameters in all locations. |

An example configuration:

```yaml
rules:
array-parameter-serialization:
severity: error
in:
- query
- header
```
## Examples
Given this configuration:
```yaml
rules:
array-parameter-serialization:
severity: error
in:
- query
```
Example of **incorrect** parameter:
```yaml
paths:
/example:
get:
parameters:
- name: exampleArray
in: query
schema:
type: array
items:
type: string
```
Example of **correct** parameter:
```yaml
paths:
/example:
get:
parameters:
- name: exampleArray
in: query
style: form
explode: true
schema:
type: array
items:
type: string
```
## Related rules
- [configurable rules](./configurable-rules.md)
- [boolean-parameter-prefixes](./boolean-parameter-prefixes.md)
- [no-invalid-parameter-examples](./no-invalid-parameter-examples.md)
- [parameter-description](./parameter-description.md)
- [operation-parameters-unique](./operation-parameters-unique.md)
## Resources
- [Rule source for OAS 3.0 and 3.1](https://github.com/Redocly/redocly-cli/blob/main/packages/core/src/rules/oas3/array-parameter-serialization.ts)
- [OpenAPI Parameter](https://redocly.com/docs/openapi-visual-reference/parameter/) docs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports[`resolveConfig should ignore minimal from the root and read local file 1
"oas3_0Decorators": {},
"oas3_0Preprocessors": {},
"oas3_0Rules": {
"array-parameter-serialization": "off",
"boolean-parameter-prefixes": "error",
"component-name-unique": "off",
"no-empty-servers": "error",
Expand All @@ -40,6 +41,7 @@ exports[`resolveConfig should ignore minimal from the root and read local file 1
"oas3_1Decorators": {},
"oas3_1Preprocessors": {},
"oas3_1Rules": {
"array-parameter-serialization": "off",
"boolean-parameter-prefixes": "error",
"component-name-unique": "off",
"no-empty-servers": "error",
Expand Down Expand Up @@ -125,6 +127,7 @@ exports[`resolveStyleguideConfig should resolve extends with local file config w
"oas3_0Decorators": {},
"oas3_0Preprocessors": {},
"oas3_0Rules": {
"array-parameter-serialization": "off",
"boolean-parameter-prefixes": "error",
"component-name-unique": "off",
"no-empty-servers": "error",
Expand All @@ -144,6 +147,7 @@ exports[`resolveStyleguideConfig should resolve extends with local file config w
"oas3_1Decorators": {},
"oas3_1Preprocessors": {},
"oas3_1Rules": {
"array-parameter-serialization": "off",
"boolean-parameter-prefixes": "error",
"component-name-unique": "off",
"no-empty-servers": "error",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/config/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const all: PluginStyleguideConfig<'built-in'> = {
'component-name-unique': 'error',
'response-contains-property': 'error',
'spec-components-invalid-map-name': 'error',
'array-parameter-serialization': 'error',
},
oas3_1Rules: {
'no-invalid-media-type-examples': 'error',
Expand All @@ -101,6 +102,7 @@ const all: PluginStyleguideConfig<'built-in'> = {
'component-name-unique': 'error',
'response-contains-property': 'error',
'spec-components-invalid-map-name': 'error',
'array-parameter-serialization': 'error',
},
async2Rules: {
'channels-kebab-case': 'error',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/config/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const minimal: PluginStyleguideConfig<'built-in'> = {
'request-mime-type': 'off',
'response-contains-property': 'off',
'response-mime-type': 'off',
'array-parameter-serialization': 'off',
},
oas3_1Rules: {
'no-invalid-media-type-examples': 'warn',
Expand All @@ -83,6 +84,7 @@ const minimal: PluginStyleguideConfig<'built-in'> = {
'request-mime-type': 'off',
'response-contains-property': 'off',
'response-mime-type': 'off',
'array-parameter-serialization': 'off',
},
async2Rules: {
'channels-kebab-case': 'off',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/config/recommended-strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const recommendedStrict: PluginStyleguideConfig<'built-in'> = {
'request-mime-type': 'off',
'response-contains-property': 'off',
'response-mime-type': 'off',
'array-parameter-serialization': 'off',
},
oas3_1Rules: {
'no-invalid-media-type-examples': 'error',
Expand All @@ -83,6 +84,7 @@ const recommendedStrict: PluginStyleguideConfig<'built-in'> = {
'request-mime-type': 'off',
'response-contains-property': 'off',
'response-mime-type': 'off',
'array-parameter-serialization': 'off',
},
async2Rules: {
'channels-kebab-case': 'off',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/config/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const recommended: PluginStyleguideConfig<'built-in'> = {
'request-mime-type': 'off',
'response-contains-property': 'off',
'response-mime-type': 'off',
'array-parameter-serialization': 'off',
},
oas3_1Rules: {
'no-invalid-media-type-examples': 'warn',
Expand All @@ -83,6 +84,7 @@ const recommended: PluginStyleguideConfig<'built-in'> = {
'request-mime-type': 'off',
'response-contains-property': 'off',
'response-mime-type': 'off',
'array-parameter-serialization': 'off',
},
async2Rules: {
'channels-kebab-case': 'off',
Expand Down
Loading

1 comment on commit efb6a45

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 76.17% 4068/5341
🟡 Branches 66.07% 2167/3280
🟡 Functions 68.39% 660/965
🟡 Lines 76.36% 3815/4996

Test suite run success

652 tests passing in 94 suites.

Report generated by 🧪jest coverage report action from efb6a45

Please sign in to comment.