Skip to content

Commit be645e7

Browse files
additional checks
1 parent 2d43dbc commit be645e7

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

tools/spectral/ipa/__tests__/IPA114ErrorResponsesReferToApiError.test.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,34 @@ testRule('xgen-IPA-114-error-responses-refer-to-api-error', [
9191
errors: [
9292
{
9393
code: 'xgen-IPA-114-error-responses-refer-to-api-error',
94-
message: '400 response must define a schema referencing ApiError',
94+
message: '400 response must define a schema referencing ApiError.',
9595
path: ['paths', '/resources', 'get', 'responses', '400', 'content', 'application/json'],
9696
severity: DiagnosticSeverity.Warning,
9797
},
9898
],
9999
},
100+
{
101+
name: 'invalid error responses missing content',
102+
document: {
103+
paths: {
104+
'/resources': {
105+
get: {
106+
responses: {
107+
400: {},
108+
},
109+
},
110+
},
111+
},
112+
},
113+
errors: [
114+
{
115+
code: 'xgen-IPA-114-error-responses-refer-to-api-error',
116+
message: '400 response must define content with ApiError schema reference.',
117+
path: ['paths', '/resources', 'get', 'responses', '400'],
118+
severity: DiagnosticSeverity.Warning,
119+
},
120+
],
121+
},
100122
{
101123
name: 'invalid error responses referencing wrong schema',
102124
document: {
@@ -133,7 +155,7 @@ testRule('xgen-IPA-114-error-responses-refer-to-api-error', [
133155
errors: [
134156
{
135157
code: 'xgen-IPA-114-error-responses-refer-to-api-error',
136-
message: '500 response must reference ApiError schema',
158+
message: '500 response must reference ApiError schema.',
137159
path: ['paths', '/resources', 'get', 'responses', '500', 'content', 'application/json'],
138160
severity: DiagnosticSeverity.Warning,
139161
},
@@ -168,7 +190,7 @@ testRule('xgen-IPA-114-error-responses-refer-to-api-error', [
168190
errors: [
169191
{
170192
code: 'xgen-IPA-114-error-responses-refer-to-api-error',
171-
message: '404 response must reference ApiError schema',
193+
message: '404 response must reference ApiError schema.',
172194
path: ['paths', '/resources', 'get', 'responses', '404', 'content', 'application/json'],
173195
severity: DiagnosticSeverity.Warning,
174196
},
@@ -193,6 +215,11 @@ testRule('xgen-IPA-114-error-responses-refer-to-api-error', [
193215
},
194216
},
195217
},
218+
500: {
219+
'x-xgen-IPA-exception': {
220+
'xgen-IPA-114-error-responses-refer-to-api-error': 'Reason',
221+
},
222+
},
196223
},
197224
},
198225
},

tools/spectral/ipa/rulesets/functions/IPA114ErrorResponsesReferToApiError.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,27 @@ const RULE_NAME = 'xgen-IPA-114-error-responses-refer-to-api-error';
1818
* @param {object} context - The context object containing path and document information
1919
*/
2020
export default (input, _, { path, documentInventory }) => {
21-
const oas = documentInventory.unresolved;
22-
const apiResponseObject = resolveObject(oas, path);
23-
const errorCode = path[path.length - 1]; // e.g., "400", "404", "500"
21+
try {
22+
const oas = documentInventory.unresolved;
23+
const apiResponseObject = resolveObject(oas, path);
24+
const errorCode = path[path.length - 1];
25+
26+
// Check for exception at response level
27+
if (hasException(apiResponseObject, RULE_NAME)) {
28+
collectException(apiResponseObject, RULE_NAME, path);
29+
return;
30+
}
2431

25-
const errors = checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode);
32+
const errors = checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode);
33+
if (errors.length !== 0) {
34+
return collectAndReturnViolation(path, RULE_NAME, errors);
35+
}
2636

27-
if (errors.length !== 0) {
28-
return collectAndReturnViolation(path, RULE_NAME, errors);
37+
collectAdoption(path, RULE_NAME);
38+
} catch(e) {
39+
handleInternalError(RULE_NAME, path, e);
2940
}
3041

31-
collectAdoption(path, RULE_NAME);
3242
};
3343

3444
function checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode) {
@@ -42,6 +52,8 @@ function checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode)
4252
const schemaName = getSchemaNameFromRef(apiResponseObject.$ref);
4353
const responseSchema = resolveObject(oas, ['components', 'responses', schemaName]);
4454
content = responseSchema.content;
55+
} else {
56+
return [{ path, message: `${errorCode} response must define content with ApiError schema reference.` }];
4557
}
4658

4759
for (const [mediaType, mediaTypeObj] of Object.entries(content)) {
@@ -57,20 +69,21 @@ function checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode)
5769
const contentPath = [...path, 'content', mediaType];
5870

5971
// Check if schema exists
60-
if (!mediaTypeObj || !mediaTypeObj.schema) {
72+
if (!mediaTypeObj.schema) {
6173
errors.push({
6274
path: contentPath,
63-
message: `${errorCode} response must define a schema referencing ApiError`,
75+
message: `${errorCode} response must define a schema referencing ApiError.`,
6476
});
6577
continue;
6678
}
6779

6880
// Check if schema references ApiError
6981
const schema = mediaTypeObj.schema;
82+
7083
if (!schema.$ref || !schema.$ref.endsWith('/ApiError')) {
7184
errors.push({
7285
path: contentPath,
73-
message: `${errorCode} response must reference ApiError schema`,
86+
message: `${errorCode} response must reference ApiError schema.`,
7487
});
7588
}
7689
}

0 commit comments

Comments
 (0)