Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,19 @@ testRule('xgen-IPA-114-error-responses-refer-to-api-error', [
get: {
responses: {
400: {
'x-xgen-IPA-exception': {
'xgen-IPA-114-error-responses-refer-to-api-error': 'Reason',
},
content: {
'application/json': {
schema: {
type: 'object',
},
'x-xgen-IPA-exception': {
'xgen-IPA-114-error-responses-refer-to-api-error': 'Reason',
},
},
},
},
500: {
description: 'Internal Service Error',
'x-xgen-IPA-exception': {
'xgen-IPA-114-error-responses-refer-to-api-error': 'Reason',
},
Expand Down
1 change: 1 addition & 0 deletions tools/spectral/ipa/rulesets/IPA-114.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rules:
This rule checks that all 4xx and 5xx error responses reference the ApiError schema.
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-114-error-responses-refer-to-api-error'
severity: warn
resolved: false
Copy link
Collaborator

Choose a reason for hiding this comment

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

FYI this is probably why the external spec references don't work, since we're passing the unresolved spec

One option to consider could be to run the validation on the resolved spec, but when evaluating the object, use the passed unresolvedDocument to see the schema ref name

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Discussed offline. For now, we will proceed with the current approach, and for the API Registry violations, I will note down the limitation in the exception reason, and refer to the investigation ticket

given: '$.paths[*][*].responses[?(@property.match(/^[45]\d\d$/))]'
then:
function: 'IPA114ErrorResponsesReferToApiError'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode)
} else if (apiResponseObject.$ref) {
const schemaName = getSchemaNameFromRef(apiResponseObject.$ref);
const responseSchema = resolveObject(oas, ['components', 'responses', schemaName]);
if (!responseSchema) {
return [{ path, message: `${errorCode} response must define content with a valid reference.` }];
}
content = responseSchema.content;
} else {
return [{ path, message: `${errorCode} response must define content with ApiError schema reference.` }];
Expand All @@ -56,13 +59,7 @@ function checkViolationsAndReturnErrors(apiResponseObject, oas, path, errorCode)
continue;
}

if (hasException(mediaTypeObj, RULE_NAME)) {
collectException(mediaTypeObj, RULE_NAME, [...path, 'content', mediaType]);
continue;
}

const contentPath = [...path, 'content', mediaType];

// Check if schema exists
if (!mediaTypeObj.schema) {
errors.push({
Expand Down
Loading