Skip to content

Commit

Permalink
feat: error for non-object request bodies (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
barrett-schonefeld committed May 4, 2021
1 parent 028c57d commit ba00a8d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/spectral-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@ parameters:

**Default Severity**: error

## request-body-object

Request bodies should be objects.

**Bad Example**

```yaml
requestBody:
content:
application/json:
schema:
type: string
```

**Good Example**

```yaml
requestBody:
content:
application/json:
schema:
type: object
properties:
prop1:
type: string
```

**Default Severity**: warn

## response-example-provided

Response examples are used to generate documentation. To improve the generated documentation, response examples should be provided in the schema object or "next to" the schema object.
Expand Down
11 changes: 11 additions & 0 deletions src/spectral/rulesets/.defaultsForSpectral.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ rules:
resolved: true
then:
function: error-response-schema
# ensure requestBody is an object
request-body-object:
description: "All request bodies MUST be structured as an object"
given: $.paths[*][*].requestBody.content[*].schema
severity: error
then:
field: type
function: enumeration
functionOptions:
values:
- object
# ensure major version is in path
major-version-in-path:
description: 'All paths must contain the API major version as a distinct path segment'
Expand Down
81 changes: 81 additions & 0 deletions test/spectral/tests/custom-rules/request-body-object.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const inCodeValidator = require('../../../../src/lib');

describe('spectral - test that request body schema is an object', function() {
it('should error only when request body is not an object', async () => {
const spec = {
openapi: '3.0.0',
paths: {
'/path1': {
post: {
operationId: 'addPet',
requestBody: {
content: {
'application/json': {
schema: {
// no error
type: 'object',
properties: {
prop1: {
type: 'string'
}
}
}
}
}
}
},
put: {
operationId: 'updatePet',
requestBody: {
content: {
'application/json': {
schema: {
// error 1
type: 'array',
items: {
type: 'string'
}
}
}
}
}
},
patch: {
operationId: 'patchPet',
requestBody: {
content: {
'application/json': {
schema: {
// no error, properties provided
properties: {
prop1: {
type: 'string'
}
}
}
}
}
}
}
}
}
};

const res = await inCodeValidator(spec, true);
const expectedErrors = res.errors.filter(
err =>
err.message === 'All request bodies MUST be structured as an object'
);
expect(expectedErrors.length).toBe(1);
expect(expectedErrors[0].path).toEqual([
'paths',
'/path1',
'put',
'requestBody',
'content',
'application/json',
'schema',
'type'
]);
});
});

0 comments on commit ba00a8d

Please sign in to comment.