Skip to content
Merged
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
98 changes: 98 additions & 0 deletions tools/spectral/ipa/__tests__/getMethodHasNoRequestBody.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-104-get-method-no-request-body', [
{
name: 'valid GET without body',
document: {
paths: {
'/resource/{id}': {
get: {
responses: {
200: {},
},
},
},
'/resource/{id}/singleton': {
get: {
responses: {
200: {},
},
},
},
},
},
errors: [],
},
{
name: 'invalid GET with body',
document: {
paths: {
'/resource/{id}': {
get: {
responses: {
200: {},
},
requestBody: {
content: {
'application/vnd.atlas.2024-08-05+json': {
schema: { type: 'object' },
},
},
},
},
},
'/resource/{id}/singleton': {
get: {
requestBody: {
content: {
'application/vnd.atlas.2024-08-05+json': {
schema: { type: 'object' },
},
},
},
responses: {
200: {},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-104-get-method-no-request-body',
message: 'The Get method must not include a request body. http://go/ipa/104',
path: ['paths', '/resource/{id}', 'get'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-104-get-method-no-request-body',
message: 'The Get method must not include a request body. http://go/ipa/104',
path: ['paths', '/resource/{id}/singleton', 'get'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid with exception',
document: {
paths: {
'/resource/{id}': {
get: {
'x-xgen-IPA-exception': {
Copy link
Member

Choose a reason for hiding this comment

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

should exception be on request body? Not problem I genuinely asking what do you think we should do here as I have exact use case and cannot decide.

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMHO This exception should be on the operation level because it is validating the absence of requestBody

'xgen-IPA-104-get-method-no-request-body': 'reason',
},
requestBody: {
content: {
'application/json': {
schema: { type: 'object' },
},
},
},
},
},
},
},
errors: [],
},
]);
8 changes: 8 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-104.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ functions:
- getMethodReturnsSingleResource
- getMethodReturnsResponseSuffixedObject
- getResponseCodeShouldBe200OK
- getMethodHasNoRequestBody

rules:
xgen-IPA-104-resource-has-GET:
Expand Down Expand Up @@ -38,3 +39,10 @@ rules:
then:
field: '@key'
function: 'getMethodReturnsResponseSuffixedObject'
xgen-IPA-104-get-method-no-request-body:
description: 'The Get method request must not include a body. http://go/ipa/104'
message: '{{error}} http://go/ipa/104'
severity: warn
given: '$.paths[*].get'
then:
function: 'getMethodHasNoRequestBody'
1 change: 1 addition & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ For rule definitions, see [IPA-104.yaml](https://github.com/mongodb/openapi/blob
| xgen-IPA-104-get-method-returns-single-resource | The purpose of the Get method is to return data from a single resource. http://go/ipa/104 | warn |
| xgen-IPA-104-get-method-response-code-is-200 | The Get method must return a 200 OK response. http://go/ipa/104 | warn |
| xgen-IPA-104-get-method-returns-response-suffixed-object | The Get method of a resource should return a "Response" suffixed object. http://go/ipa/104 | warn |
| xgen-IPA-104-get-method-no-request-body | The Get method request must not include a body. http://go/ipa/104 | warn |

### IPA-106

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { hasException } from './utils/exceptions.js';
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
import {
getResourcePathItems,
isResourceCollectionIdentifier,
isSingleResourceIdentifier,
isSingletonResource,
} from './utils/resourceEvaluation.js';

const RULE_NAME = 'xgen-IPA-104-get-method-no-request-body';
const ERROR_MESSAGE = 'The Get method must not include a request body.';

export default (input, _, { path, documentInventory }) => {
const resourcePath = path[1];
const oas = documentInventory.resolved;

if (
!isSingleResourceIdentifier(resourcePath) &&
!(
isResourceCollectionIdentifier(resourcePath) && isSingletonResource(getResourcePathItems(resourcePath, oas.paths))
)
) {
return;
}

if (hasException(input, RULE_NAME)) {
collectException(input, RULE_NAME, path);
return;
}

const errors = checkViolationsAndReturnErrors(input, path);

if (errors.length !== 0) {
return collectAndReturnViolation(path, RULE_NAME, errors);
}
collectAdoption(path, RULE_NAME);
};

function checkViolationsAndReturnErrors(getOperationObject, path) {
if (getOperationObject.requestBody) {
return [{ path, message: ERROR_MESSAGE }];
}
return [];
}
Loading