diff --git a/packages/ruleset/src/functions/collection-array-property.js b/packages/ruleset/src/functions/collection-array-property.js index fb1a44d2..08a312e2 100644 --- a/packages/ruleset/src/functions/collection-array-property.js +++ b/packages/ruleset/src/functions/collection-array-property.js @@ -57,7 +57,10 @@ function collectionArrayProperty(schema, path, apidef) { // If this is a collection "list"-type operation, then check to make sure // that "schema" defines an array property named after the last path segment. - if (isListOperation(operation, pathString, apidef)) { + if ( + isListOperation(operation, pathString, apidef) && + !isBinarySchema(schema) + ) { logger.debug('Detected list-type operation'); const pathSegments = pathString.split('/'); const propertyName = pathSegments[pathSegments.length - 1]; @@ -112,6 +115,17 @@ function isListOperation(operation, path, apidef) { return !!siblingPath; } +/** + * Returns true iff "schema" represents binary file content. + * @param {*} schema the schema to check + * @returns boolean true if the schema is a binary string schema + */ +function isBinarySchema(schema) { + return ( + isObject(schema) && schema.type === 'string' && schema.format === 'binary' + ); +} + /** * Returns true iff "schema" defines an array property named "name". * The property could be defined directly in "schema" or as part of diff --git a/packages/ruleset/test/rules/collection-array-property.test.js b/packages/ruleset/test/rules/collection-array-property.test.js index aff906a6..62cd278c 100644 --- a/packages/ruleset/test/rules/collection-array-property.test.js +++ b/packages/ruleset/test/rules/collection-array-property.test.js @@ -24,6 +24,34 @@ describe(`Spectral rule: ${ruleId}`, () => { expect(results).toHaveLength(0); }); + it('does not require an array property for binary file responses', async () => { + const testDocument = makeCopy(rootDocument); + + testDocument.paths['/v1/gateway_letters'] = { + get: { + operationId: 'list_gateway_letter_of_authorization', + responses: { + 200: { + description: 'Letter of Authorization retrieved successfully.', + content: { + 'application/pdf': { + schema: { + description: 'Letter of Authorization', + 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);