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
2 changes: 1 addition & 1 deletion src/impl/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const cloneDeep = require('lodash.clonedeep'),

// CONSTANTS

const PATH__EXAMPLES = '$..examples[?(@property.match(/[\/+]json/))]',
const PATH__EXAMPLES = '$..examples[?(@property && typeof @property === "string" && @property.match(/[\/+]json/))]',
PROP__SCHEMA = 'schema',
PROP__EXAMPLES = 'examples';

Expand Down
5 changes: 3 additions & 2 deletions src/impl/v3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const cloneDeep = require('lodash.clonedeep'),

// CONSTANTS

const RESPONSES = '$..responses..content[?(@property.match(/[\/+]json/))]';
const REQUEST = '$..requestBody.content[?(@property.match(/[\/+]json/))]';
// eslint-disable-next-line max-len
const RESPONSES = '$..responses..content[?(@property && typeof @property === "string" && @property.match(/[\/+]json/))]';
const REQUEST = '$..requestBody.content[?(@property && typeof @property === "string" && @property.match(/[\/+]json/))]';
const SINGLE_EXAMPLE = '.example';
const MANY_EXAMPLES = '.examples.*.value';

Expand Down
32 changes: 32 additions & 0 deletions test/data/v2/response-valid-content-array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
swagger: '2.0'
info:
title: API
version: 1.0.0
paths:
/metrics:
get:
summary: Get info
operationId: get_info
description: |
This API hook returns server metrics.
produces:
- application/json
responses:
200:
description: OK
schema:
type: object
properties:
examples:
type: array
description: Array of valid entries
items:
type: object
properties:
type:
type: integer
description: RR type of entry
examples:
application/json:
examples:
- type: 0
32 changes: 32 additions & 0 deletions test/data/v3/response-valid-content-array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
openapi: 3.0.2
components:
paths:
metrics:
get:
summary: Get info
operationId: "get_info"
description: |
This API hook returns server metrics.
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
content:
type: array
description: Array of valid entries
items:
type: object
properties:
type:
type: integer
description: RR type of entry
examples:
metrics:
summary: Server metrics
value:
content:
- type: 0
18 changes: 17 additions & 1 deletion test/specs/impl/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path'),
{ validateExample, 'default': validateExamples, validateExamplesByMap } = require('../../../../src/index'),
{ ApplicationError, ErrorType } = require('../../../../src/application-error');
const { prepare } = require('../../../../src/impl/v2');
const { validateFile } = require('../../../../src');

const PATH__SCHEMA_EXTERNAL_EXAMPLE = '$.paths./.get.responses.200.schema',
PATH__SCHEMA_EXTERNAL_EXAMPLE_INVALID = '$.hmm.what.am.i.gonna.get.for.lunch',
Expand All @@ -27,7 +28,9 @@ const PATH__SCHEMA_EXTERNAL_EXAMPLE = '$.paths./.get.responses.200.schema',
FILE_PATH__NOT_EXISTS = 'there is no spoon',
FILE_PATH__EXTERNAL_EXAMPLE_INVALID_TYPE = path.join('test', 'data', 'v2', 'external-examples-invalid-type.json'),
FILE_PATH__EXTERNAL_EXAMPLE_INVALID_MISSING_LINK = path.join('test', 'data', 'v2',
'external-examples-invalid-missing-link.json');
'external-examples-invalid-missing-link.json'),
FILE_PATH__VALID__RESPONSE__EXAMPLES_CONTENT_ARRAY
= path.join(__dirname, '../../../data/v2/response-valid-content-array.yaml');

describe('Main-module, for v2 should', () => {
describe('recognize', () => {
Expand Down Expand Up @@ -421,6 +424,19 @@ describe('Main-module, for v2 should', () => {
preparedOpenapi.should.deep.equal(loadTestData('v2/valid-with-all-properties-required'));
});
});
describe('handle elements named "content" which are Arrays', function() {
it('should validate successfully', async function() {
const { valid, statistics } = structuredClone(
await validateFile(FILE_PATH__VALID__RESPONSE__EXAMPLES_CONTENT_ARRAY)
);
valid.should.equal(true);
statistics.should.deep.equal({
schemasWithExamples: 1,
examplesWithoutSchema: 0,
examplesTotal: 1
});
});
});
});

function removeMapFilePath(errors) {
Expand Down
17 changes: 16 additions & 1 deletion test/specs/impl/v3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ const JSON_PATH__CONTEXT_MUTUALLY_EXCLUSIVE = '/paths/~1pets/get/responses/200/c
= path.join(__dirname, '../../../data/v3/simple-api-with-examples-exclusive-minimum-invalid.json'),
FILE_PATH__EXAMPLE_NAMES_TO_BE_ESCAPED
= path.join(__dirname, '../../../data/v3/simple-api-with-example-names-to-be-escaped.json'),
FILE_PATH__UNKNOWN_FORMATS = path.join(__dirname, '../../../data/v3/unknown-formats.json');
FILE_PATH__UNKNOWN_FORMATS = path.join(__dirname, '../../../data/v3/unknown-formats.json'),
FILE_PATH__VALID__RESPONSE__EXAMPLES_CONTENT_ARRAY
= path.join(__dirname, '../../../data/v3/response-valid-content-array.yaml');

describe('Main-module, for v3 should', function() {
describe('recognize', function() {
Expand Down Expand Up @@ -514,6 +516,19 @@ unknown format "country-code-2" ignored in schema at path "#/properties/country"
})).valid.should.equal(true);
});
});
describe('handle elements named "content" which are Arrays', function() {
it('should validate successfully', async function() {
const { valid, statistics } = structuredClone(
await validateFile(FILE_PATH__VALID__RESPONSE__EXAMPLES_CONTENT_ARRAY)
);
valid.should.equal(true);
statistics.should.deep.equal({
schemasWithExamples: 1,
examplesWithoutSchema: 0,
examplesTotal: 1
});
});
});
});

function _expandFilePathOfErrors(errors, propertyName) {
Expand Down