diff --git a/tools/spectral/ipa/__tests__/IPA114ErrorResponsesReferToApiError.test.js b/tools/spectral/ipa/__tests__/IPA114ErrorResponsesReferToApiError.test.js index 91a83a313f..3e994073f7 100644 --- a/tools/spectral/ipa/__tests__/IPA114ErrorResponsesReferToApiError.test.js +++ b/tools/spectral/ipa/__tests__/IPA114ErrorResponsesReferToApiError.test.js @@ -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', }, diff --git a/tools/spectral/ipa/rulesets/IPA-114.yaml b/tools/spectral/ipa/rulesets/IPA-114.yaml index e6fc26815f..4ad796dc76 100644 --- a/tools/spectral/ipa/rulesets/IPA-114.yaml +++ b/tools/spectral/ipa/rulesets/IPA-114.yaml @@ -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 given: '$.paths[*][*].responses[?(@property.match(/^[45]\d\d$/))]' then: function: 'IPA114ErrorResponsesReferToApiError' diff --git a/tools/spectral/ipa/rulesets/functions/IPA114ErrorResponsesReferToApiError.js b/tools/spectral/ipa/rulesets/functions/IPA114ErrorResponsesReferToApiError.js index 3231c8221f..02d0ede00f 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA114ErrorResponsesReferToApiError.js +++ b/tools/spectral/ipa/rulesets/functions/IPA114ErrorResponsesReferToApiError.js @@ -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.` }]; @@ -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({