diff --git a/lib/models/asyncapi.js b/lib/models/asyncapi.js index aa5a55540..06035d7c2 100644 --- a/lib/models/asyncapi.js +++ b/lib/models/asyncapi.js @@ -135,6 +135,13 @@ class AsyncAPIDocument extends Base { return this._json.tags.map(t => new Tag(t)); } + /** + * @returns {boolean} + */ + hasMessages() { + return !!this.allMessages().size + } + /** * @returns {Map} */ diff --git a/test/models/asyncapi_test.js b/test/models/asyncapi_test.js index d71662be9..29095b92d 100644 --- a/test/models/asyncapi_test.js +++ b/test/models/asyncapi_test.js @@ -166,6 +166,24 @@ describe('AsyncAPIDocument', () => { }); }); + describe('#hasMessages()', function () { + it('should return true if there is a message in components but not in channels', () => { + const doc = { components: { messages: { test: { test: true, k: 3 } } } }; + const d = new AsyncAPIDocument(doc); + expect(d.hasMessages()).to.equal(true); + }); + it('should return true if there is a message in channels operations but not in components', () => { + const doc = { channels: { test: { publish: { message: { name: 'test', test: false, k: 1 } } } } }; + const d = new AsyncAPIDocument(doc); + expect(d.hasMessages()).to.equal(true); + }); + it('should return false if there are no messages neither in components nor in channels operations', () => { + const doc = { channels: { test: { publish: { } } }, components: { } }; + const d = new AsyncAPIDocument(doc); + expect(d.hasMessages()).to.equal(false); + }); + }); + describe('#allMessages()', function () { it('should return an array with all the messages used in the document and overwrite the message from channel', () => { const doc = { channels: { test: { publish: { message: { name: 'test', test: false, k: 1 } } } }, components: { messages: { test: { test: true, k: 3 } } } };