Skip to content

Commit

Permalink
fix: case when message is oneOf for one element (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev authored May 24, 2022
1 parent 429c8e7 commit 48d8708
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/models/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Operation extends OperationTraitable {
*/
message(index) {
if (!this._json.message) return null;
if (this._json.message.oneOf && this._json.message.oneOf.length === 1) return new Message(this._json.message.oneOf[0]);
if (!this._json.message.oneOf) return new Message(this._json.message);
if (typeof index !== 'number') return null;
if (index > this._json.message.oneOf.length - 1) return null;
Expand Down
6 changes: 6 additions & 0 deletions test/models/operation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ describe('Operation', function() {
expect(d.message(0).json()).to.be.deep.equal(doc.message.oneOf[0]);
expect(d.message(1).json()).to.be.deep.equal(doc.message.oneOf[1]);
});

it('should return a Message object if no index is provided and message is oneOf from one element', function() {
const doc = { message: { oneOf: [{ test: true }] } };
const d = new Operation(doc);
expect(d.message().json()).to.be.deep.equal(doc.message.oneOf[0]);
});

it('should return null when index is out of bounds', function() {
const doc = { message: { oneOf: [{ test: true }, { test: false }] } };
Expand Down

0 comments on commit 48d8708

Please sign in to comment.