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

testRule('xgen-IPA-104-get-method-response-code-is-200', [
{
name: 'valid methods',
document: {
paths: {
'/resource': {
get: {
responses: {
400: {},
500: {},
},
},
},
'/resource/{id}': {
get: {
responses: {
200: {},
400: {},
500: {},
},
},
},
'/resource/{id}:customMethod': {
get: {
responses: {
400: {},
500: {},
},
},
},
'/singleton': {
get: {
responses: {
400: {},
500: {},
},
},
},
},
},
errors: [],
},
{
name: 'invalid methods',
document: {
paths: {
'/resource1': { get: { responses: {} } },
'/resource1/{id}': {
get: {
responses: {
201: {},
400: {},
500: {},
},
},
},
'/resource2': { get: { responses: {} } },
'/resource2/{id}': {
get: {
responses: {
400: {},
500: {},
},
},
},
'/resource3': { get: { responses: {} } },
'/resource3/{id}': {
get: {
responses: {
200: {},
201: {},
400: {},
500: {},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-104-get-method-response-code-is-200',
message:
'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/104',
path: ['paths', '/resource1/{id}', 'get'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-104-get-method-response-code-is-200',
message:
'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/104',
path: ['paths', '/resource2/{id}', 'get'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-104-get-method-response-code-is-200',
message:
'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/104',
path: ['paths', '/resource3/{id}', 'get'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid method with exception',
document: {
paths: {
'/resource1': { get: { responses: {} } },
'/resource1/{id}': {
get: {
responses: {
201: {},
400: {},
500: {},
},
'x-xgen-IPA-exception': {
'xgen-IPA-104-get-method-response-code-is-200': 'reason',
},
},
},
'/resource2': { get: { responses: {} } },
'/resource2/{id}': {
get: {
responses: {
400: {},
500: {},
},
'x-xgen-IPA-exception': {
'xgen-IPA-104-get-method-response-code-is-200': 'reason',
},
},
},
},
},
errors: [],
},
]);
9 changes: 9 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-104.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
functions:
- eachResourceHasGetMethod
- getMethodReturnsSingleResource
- getResponseCodeShouldBe200OK

rules:
xgen-IPA-104-resource-has-GET:
Expand All @@ -21,3 +22,11 @@ rules:
given: '$.paths[*].get'
then:
function: 'getMethodReturnsSingleResource'

xgen-IPA-104-get-method-response-code-is-200:
description: 'The Get method must return a 200 OK response. http://go/ipa/104'
message: '{{error}} http://go/ipa/104'
severity: warn
given: '$.paths[*].get'
then:
function: 'getResponseCodeShouldBe200OK'
1 change: 1 addition & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ For rule definitions, see [IPA-104.yaml](https://github.com/mongodb/openapi/blob
| ----------------------------------------------- | ----------------------------------------------------------------------------------------- | -------- |
| xgen-IPA-104-resource-has-GET | APIs must provide a get method for resources. http://go/ipa/104 | warn |
| 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 |

### IPA-109

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

const RULE_NAME = 'xgen-IPA-104-get-method-response-code-is-200';
const ERROR_MESSAGE =
'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code.';

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

if (isCustomMethod(resourcePath) || !isChild(resourcePath)) {
return;
}

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

if (input['responses']) {
Copy link
Collaborator

@lovisaberggren lovisaberggren Feb 27, 2025

Choose a reason for hiding this comment

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

Something to consider here is that this test will cover IPA 105 List as well, since it's also a HTTP GET method, which may skew the metrics, and will cause duplicate validation if we are planning to implement the same check for IPA 105.

Also, this covers custom methods too, though IIRC we don't define that custom method GET should return 200. Maybe we should introduce a guideline for this on custom methods, or not do the validation if we think there is a reason for custom gets to return a 202 for example (though IMO it should be a custom post in that case)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I modified the code as "If it is a custom method or collection resource (not a child with current glossary), the rule will be bypassed". Let me know what you think

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks!

const responses = input['responses'];

// If there is no 200 response, return a violation
if (!responses['200']) {
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
}

// If there are other 2xx responses that are not 200, return a violation
if (Object.keys(responses).some((key) => key.startsWith('2') && key !== '200')) {
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
}
}

collectAdoption(path, RULE_NAME);
};
Loading