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
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This will install Husky, which manages Git hooks for the project. The hooks ensu
This project uses the following Git hooks:

- **pre-commit**: Automatically formats your code using Prettier and runs tests for staged JavaScript files to ensure code quality before each commit.
- If you get the message `hint: The '.husky/pre-commit' hook was ignored because it's not set as executable.` during the commit, you can run `chmod ug+x .husky/*` to make it executable.

### Available Scripts

Expand Down
117 changes: 117 additions & 0 deletions tools/spectral/ipa/__tests__/listMethodHasNoRequestBody.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-105-list-method-no-request-body', [
{
name: 'valid list without body',
document: {
paths: {
'/resource': {
get: {
responses: {
200: {},
},
},
},
},
},
errors: [],
},
{
name: 'rule ignores singleton and Get',
document: {
paths: {
'/resource': {
get: {
responses: {
200: {},
},
},
},
'/resource/{id}': {
get: {
responses: {
200: {},
},
requestBody: {
content: {
'application/vnd.atlas.2024-08-05+json': {
schema: { type: 'object' },
},
},
},
},
},
'/resource/{id}/singleton': {
get: {
responses: {
200: {},
},
requestBody: {
content: {
'application/vnd.atlas.2024-08-05+json': {
schema: { type: 'object' },
},
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid list with body',
document: {
paths: {
'/resource': {
get: {
responses: {
200: {},
},
requestBody: {
content: {
'application/vnd.atlas.2024-08-05+json': {
schema: { type: 'object' },
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-105-list-method-no-request-body',
message: 'The List method must not include a request body. http://go/ipa/105',
path: ['paths', '/resource', 'get'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid with exception',
document: {
paths: {
'/resource': {
get: {
'x-xgen-IPA-exception': {
'xgen-IPA-105-list-method-no-request-body': 'reason',
},
responses: {
200: {},
},
requestBody: {
content: {
'application/vnd.atlas.2024-08-05+json': {
schema: { type: 'object' },
},
},
},
},
},
},
},
errors: [],
},
]);
8 changes: 8 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-105.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

functions:
- listResponseCodeShouldBe200OK
- listMethodHasNoRequestBody

rules:
xgen-IPA-105-list-method-response-code-is-200:
Expand All @@ -12,3 +13,10 @@ rules:
given: '$.paths[*].get'
then:
function: 'listResponseCodeShouldBe200OK'
xgen-IPA-105-list-method-no-request-body:
description: 'The List method request must not include a body. http://go/ipa/105'
message: '{{error}} http://go/ipa/105'
severity: warn
given: '$.paths[*].get'
then:
function: 'listMethodHasNoRequestBody'
7 changes: 4 additions & 3 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ For rule definitions, see [IPA-104.yaml](https://github.com/mongodb/openapi/blob

For rule definitions, see [IPA-105.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-105.yaml).

| Rule Name | Description | Severity |
| --------------------------------------------- | ---------------------------------------------------------------- | -------- |
| xgen-IPA-105-list-method-response-code-is-200 | The List method must return a 200 OK response. http://go/ipa/105 | warn |
| Rule Name | Description | Severity |
| --------------------------------------------- | ------------------------------------------------------------------ | -------- |
| xgen-IPA-105-list-method-response-code-is-200 | The List method must return a 200 OK response. http://go/ipa/105 | warn |
| xgen-IPA-105-list-method-no-request-body | The List method request must not include a body. http://go/ipa/105 | warn |

### IPA-106

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

const RULE_NAME = 'xgen-IPA-105-list-method-no-request-body';
const ERROR_MESSAGE = 'The List method must not include a request body.';

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

if (
!isResourceCollectionIdentifier(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