Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions packages/ruleset/src/functions/collection-array-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const {
schemaHasConstraint,
isArraySchema,
isBinarySchema,
isObject,
getUnresolvedSpec,
} = require('@ibm-cloud/openapi-ruleset-utilities');
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions packages/ruleset/test/rules/collection-array-property.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading