Skip to content

Commit

Permalink
fix(rulesets): operation-success-response rule does not respect 2XX a…
Browse files Browse the repository at this point in the history
…nd 3XX (#1793)
  • Loading branch information
P0lip committed Aug 31, 2021
1 parent 0aa8863 commit b5c6a8f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,81 @@ testRule('operation-success-response', [
},
errors: [],
},

{
name: 'oas2 document has 2XX and 3XX responses set',
document: {
swagger: '2.0',
paths: {
'/path': {
get: {
responses: {
'3XX': {},
},
},
post: {
responses: {
'2XX': {},
},
},
},
},
},
errors: [
{
message: 'Operation must have at least one "2xx" or "3xx" response.',
path: ['paths', '/path', 'get', 'responses'],
severity: DiagnosticSeverity.Warning,
},
{
message: 'Operation must have at least one "2xx" or "3xx" response.',
path: ['paths', '/path', 'post', 'responses'],
severity: DiagnosticSeverity.Warning,
},
],
},

{
name: 'oas 3.0.x document has 2XX and 3XX responses set',
document: {
openapi: '3.0.3',
paths: {
'/path': {
get: {
responses: {
'3XX': {},
},
},
post: {
responses: {
'2XX': {},
},
},
},
},
},
errors: [],
},

{
name: 'oas 3.1.x document has 2XX and 3XX responses set',
document: {
openapi: '3.1.0',
paths: {
'/path': {
get: {
responses: {
'3XX': {},
},
},
post: {
responses: {
'2XX': {},
},
},
},
},
},
errors: [],
},
]);
42 changes: 26 additions & 16 deletions packages/rulesets/src/oas/functions/oasOpSuccessResponse.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import type { IFunction } from '@stoplight/spectral-core';
import { isObject } from './utils/isObject';
import { createRulesetFunction } from '@stoplight/spectral-core';
import { oas3 } from '@stoplight/spectral-formats';

export const oasOpSuccessResponse: IFunction = targetVal => {
if (!isObject(targetVal)) {
return;
}
export const oasOpSuccessResponse = createRulesetFunction<Record<string, unknown>, null>(
{
input: {
type: 'object',
},
options: null,
},
(input, opts, context) => {
const isOAS3X = context.document.formats?.has(oas3) === true;

for (const response of Object.keys(input)) {
if (isOAS3X && (response === '2XX' || response === '3XX')) {
return;
}

for (const response of Object.keys(targetVal)) {
if (Number(response) >= 200 && Number(response) < 400) {
return;
if (Number(response) >= 200 && Number(response) < 400) {
return;
}
}
}

return [
{
message: 'Operation must define at least a single 2xx or 3xx response',
},
];
};
return [
{
message: 'Operation must define at least a single 2xx or 3xx response',
},
];
},
);

export default oasOpSuccessResponse;

0 comments on commit b5c6a8f

Please sign in to comment.