Skip to content

Commit

Permalink
Fix: additional items and properties are never discovered and tests f…
Browse files Browse the repository at this point in the history
…ailing on windows (#72)

* fixes #71
and fixes a problem with "additionalItems" are never checked

* Fixed a problem with the parse_test on windows where path is required even if not on linux.
Fixed a problem with the parse_test where offsets are not caluclated.

* Removed forgotten console log.

* Update lib/models/asyncapi.js

Co-Authored-By: Fran Méndez <[email protected]>

* Update lib/models/asyncapi.js

Co-Authored-By: Fran Méndez <[email protected]>

Co-authored-by: Fran Méndez <[email protected]>
  • Loading branch information
jonaslagoni and fmvilas authored Apr 27, 2020
1 parent fec793f commit e2973e1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 13 deletions.
31 changes: 22 additions & 9 deletions lib/models/asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,31 @@ function recursiveSchema(schema, callback) {
if (schema.type() !== undefined) {
switch (schema.type()) {
case 'object':
const props = schema.properties();
for (const [, propertySchema] of Object.entries(props)) {
recursiveSchema(propertySchema, callback);
if(schema.additionalProperties() !== undefined && typeof schema.additionalProperties() !== "boolean"){
const additionalSchema = schema.additionalProperties()
recursiveSchema(additionalSchema, callback);
}
if(schema.properties() !== null){
const props = schema.properties();
for (const [, propertySchema] of Object.entries(props)) {
recursiveSchema(propertySchema, callback);
}
}
break;
case 'array':
if (Array.isArray(schema.items())) {
schema.items().forEach(arraySchema => {
recursiveSchema(arraySchema, callback);
});
} else {
recursiveSchema(schema.items(), callback);
if(schema.additionalItems() !== undefined){
const additionalArrayItems = schema.additionalItems()
recursiveSchema(additionalArrayItems, callback);
}

if(schema.items() !== null){
if (Array.isArray(schema.items())) {
schema.items().forEach(arraySchema => {
recursiveSchema(arraySchema, callback);
});
} else {
recursiveSchema(schema.items(), callback);
}
}
break;
}
Expand Down
73 changes: 73 additions & 0 deletions test/models/asyncapi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,79 @@ describe('AsyncAPIDocument', () => {
});

describe('#allSchemas()', function () {

it('should return additional items schemas when no items specified', () => {
const doc = {
"channels": {
"some_channel": {
"subscribe": {
"message": {
"name": "some_map",
"payload": {
"type": "array",
"$id": "payloadSchema",
"test": true,
"additionalItems": {
"type": "string",
"$id": "additionalItemSchema",
"test": true
}
}
}
}
}
}
};
const d = new AsyncAPIDocument(doc);
const schemas = d.allSchemas();
expect(schemas.size).to.be.equal(2);
//Ensure the actual keys are as expected
const schemaKeys = Array.from(schemas.keys());
expect(schemaKeys).to.deep.equal([
"payloadSchema",
"additionalItemSchema"
])
for (const t of schemas.values()) {
expect(t.constructor.name).to.be.equal('Schema');
expect(t.json().test).to.be.equal(true);
}
});
it('should return additional property schemas when no properties are specified', () => {
const doc = {
"channels": {
"some_channel": {
"subscribe": {
"message": {
"name": "some_map",
"payload": {
"type": "object",
"$id": "payloadSchema",
"test": true,
"additionalProperties": {
"type": "string",
"$id": "additionalPropSchema",
"test": true
}
}
}
}
}
}
};
const d = new AsyncAPIDocument(doc);
const schemas = d.allSchemas();
expect(schemas.size).to.be.equal(2);
//Ensure the actual keys are as expected
const schemaKeys = Array.from(schemas.keys());
expect(schemaKeys).to.deep.equal([
"payloadSchema",
"additionalPropSchema"
])
for (const t of schemas.values()) {
expect(t.constructor.name).to.be.equal('Schema');
expect(t.json().test).to.be.equal(true);
}
});
it('should return a map with all the schemas used in the document', () => {
const doc = { channels: { test: { parameters: { testParam1: { schema: { $id: 'testParamSchema', test: true, k: 0 } } }, publish: { message: { headers: { test: true, k: 1 }, payload: { test: true, k: 2 } } } }, test2: { subscribe: { message: { payload: { $id: 'testPayload', test: true, k: 2 } } } } }, components: { schemas: { testSchema: { test: true, k: 3 } } } };
const d = new AsyncAPIDocument(doc);
Expand Down
8 changes: 4 additions & 4 deletions test/parse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('parse()', function () {

it('should offer information about YAML line and column where $ref errors are located', async function () {
try {
await parser.parse(inputYAML)
await parser.parse(inputYAML, { path: __filename })
} catch (e) {
expect(e.refs).to.deep.equal([{
jsonPointer: '/components/schemas/testSchema/properties/test/$ref',
Expand All @@ -222,7 +222,7 @@ describe('parse()', function () {

it('should offer information about JSON line and column where $ref errors are located', async function () {
try {
await parser.parse(inputJSON)
await parser.parse(inputJSON, { path: __filename })
} catch (e) {
expect(e.refs).to.deep.equal([{
jsonPointer: '/components/schemas/testSchema/properties/test/$ref',
Expand All @@ -238,7 +238,7 @@ describe('parse()', function () {

it('should not offer information about JS line and column where $ref errors are located if format is JS', async function () {
try {
await parser.parse(JSON.parse(inputJSON))
await parser.parse(JSON.parse(inputJSON), { path: __filename })
} catch (e) {
expect(e.refs).to.deep.equal([{
jsonPointer: '/components/schemas/testSchema/properties/test/$ref',
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('parse()', function () {
expect(e.type).to.equal('https://github.com/asyncapi/parser-js/invalid-yaml');
expect(e.title).to.equal('The provided YAML is not valid.');
expect(e.detail).to.equal(`bad indentation of a mapping entry at line 19, column 11:\n $ref: "#/components/schemas/sentAt"\n ^`);
expect(e.location).to.deep.equal({ startOffset: 460, startLine: 19, startColumn: 11 });
expect(e.location).to.deep.equal({ startOffset: offset(460, 19), startLine: 19, startColumn: 11 });
}
});

Expand Down

0 comments on commit e2973e1

Please sign in to comment.