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

feat(rulesets): add oas3_1-servers-in-webhook and oas3_1-callbacks-in… #2581

Merged
merged 8 commits into from May 3, 2024
30 changes: 30 additions & 0 deletions docs/reference/openapi-rules.md
Expand Up @@ -922,3 +922,33 @@ servers:
```

In this example, both **`{region}`** and **`{version}`** variables are properly defined and used in the server URL. Also, the default value for **`region`** is within the allowed values.

### oas3_1-servers-in-webhook

Servers should not be defined in a webhook.

**Recommended:** Yes

**Bad Example**

```yaml
webhooks:
servers:
- url: https://example.com/
- url: https://example.com/api/
```

### oas3_1-callbacks-in-webhook

Callbacks should not be defined in a webhook.

**Recommended:** Yes

**Bad Example**

```yaml
webhooks:
newPet:
post:
callbacks: ...
```
@@ -0,0 +1,25 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from '../../__tests__/__helpers__/tester';

testRule('oas3_1-callbacks-in-webhook', [
{
name: 'callbacks defined in webhook',
document: {
openapi: '3.1.0',
webhooks: {
newPet: {
post: {
callbacks: {},
},
},
},
},
errors: [
{
message: 'Callbacks should not be defined in a webhook.',
path: ['webhooks', 'newPet', 'post', 'callbacks'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);
@@ -0,0 +1,21 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from '../../__tests__/__helpers__/tester';

testRule('oas3_1-servers-in-webhook', [
{
name: 'servers defined in webhook',
document: {
openapi: '3.1.0',
webhooks: {
servers: [],
},
},
errors: [
{
message: 'Servers should not be defined in a webhook.',
path: ['webhooks', 'servers'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);
18 changes: 18 additions & 0 deletions packages/rulesets/src/oas/index.ts
Expand Up @@ -707,5 +707,23 @@ const ruleset = {
},
},
},
'oas3_1-servers-in-webhook': {
message: 'Servers should not be defined in a webhook.',
formats: [oas3_1],
recommended: true,
given: ['$.webhooks.servers'],
Copy link
Member

Choose a reason for hiding this comment

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

i think we want this inside of the webhook operation, not at the webhooks object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohhh okay servers can be defined on both. Should the warning only be at the webhook operation level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm reading through the oas docs and it seems like we should be showing a warning at both levels (the servers array at the path item object level would apply to all operations and servers at the operation level is for that operation only)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok added that here fb14f54

then: {
function: undefined,
},
},
'oas3_1-callbacks-in-webhook': {
message: 'Callbacks should not be defined in a webhook.',
formats: [oas3_1],
recommended: true,
given: ['$.webhooks[*][*].callbacks'],
then: {
function: undefined,
Copy link
Member

Choose a reason for hiding this comment

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

are these required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you referring to the function: undefined part? That function is checking if callbacks is undefined and if it's not then it'll show the warning.

},
},
},
};