Skip to content

fix: case when message is oneOf for one element #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
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