diff --git a/packages/ruleset/src/functions/collection-array-property.js b/packages/ruleset/src/functions/collection-array-property.js index fb1a44d2..b1976eb8 100644 --- a/packages/ruleset/src/functions/collection-array-property.js +++ b/packages/ruleset/src/functions/collection-array-property.js @@ -6,6 +6,7 @@ const { schemaHasConstraint, isArraySchema, + isBinarySchema, isObject, getUnresolvedSpec, } = require('@ibm-cloud/openapi-ruleset-utilities'); @@ -40,6 +41,14 @@ module.exports = function (schema, _opts, context) { function collectionArrayProperty(schema, path, apidef) { logger.debug(`${ruleId}: checking schema at location: ${path.join('.')}`); + // A binary response body (e.g. a file download such as a PDF served as + // application/pdf) is never a collection, so it should not be expected to + // define a collection array property. + if (isBinarySchema(schema)) { + logger.debug(`${ruleId}: skipping binary (file download) response schema`); + return []; + } + const pathString = path[1]; if (!apidef.paths[pathString]) { // Spectral has weird behavior where if it finds unexpected characters, like diff --git a/packages/ruleset/test/rules/collection-array-property.test.js b/packages/ruleset/test/rules/collection-array-property.test.js index aff906a6..e7283041 100644 --- a/packages/ruleset/test/rules/collection-array-property.test.js +++ b/packages/ruleset/test/rules/collection-array-property.test.js @@ -24,6 +24,26 @@ describe(`Spectral rule: ${ruleId}`, () => { expect(results).toHaveLength(0); }); + it('list operation response is a binary file download (not a collection)', async () => { + const testDocument = makeCopy(rootDocument); + + // Replace the movies list response body with a binary file download (such + // as a PDF). A binary response is not a collection, so the rule must not + // expect a "movies" array property. Regression test for the false positive + // reported when an operation returns binary file content. + testDocument.paths['/v1/movies'].get.responses['200'].content = { + 'application/pdf': { + schema: { + type: 'string', + format: 'binary', + }, + }, + }; + + const results = await testRule(ruleId, rule, testDocument); + expect(results).toHaveLength(0); + }); + it('would normally warn but path contains quote character, which throws off spectral', async () => { const testDocument = makeCopy(rootDocument);