Skip to content

Commit

Permalink
fix(json-0.6): serialise undefined content as empty array
Browse files Browse the repository at this point in the history
The bug was discovered when doing conversions from a deserialised API
Elements tree when using the JSON serialiser as the content for the
element internally become undefined which means the serialiser wouldn't
work.
  • Loading branch information
kylef committed Sep 11, 2019
1 parent 750e154 commit 5fd404b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Minim Changelog

## Master

### Bug Fixes

- Fixes a JSON 0.6 serialisation bug where httpRequest and similar array-based
elements with undefined content would be serialised with undefined content
instead of an empty array as content.

## 0.23.5 (2019-07-02)

This release brings some performance improvements, namely to serialising with
Expand Down
10 changes: 6 additions & 4 deletions lib/serialisers/JSON06Serialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,24 @@ module.exports = class JSON06Serialiser extends JSONSerialiser {
if (this.shouldSerialiseContent(element, content)) {
payload.content = content;
}
} else if (this.shouldSerialiseContent(element, element.content) && element instanceof this.namespace.elements.Array) {
payload.content = [];
}

return payload;
}

shouldSerialiseContent(element, content) {
if (content === undefined) {
return false;
}

if (element.element === 'parseResult' || element.element === 'httpRequest'
|| element.element === 'httpResponse' || element.element === 'category'
|| element.element === 'link') {
return true;
}

if (content === undefined) {
return false;
}

if (Array.isArray(content) && content.length === 0) {
return false;
}
Expand Down
12 changes: 12 additions & 0 deletions test/serialisers/JSON06Serialiser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,18 @@ describe('JSON 0.6 Serialiser', () => {
});
});

it('serialises undefined httpRequest content', () => {
const element = new minim.elements.Array();
element.element = 'httpRequest';
element.content = undefined;
const serialised = serialiser.serialise(element);

expect(serialised).to.deep.equal({
element: 'httpRequest',
content: [],
});
});

it('serialises empty httpResponse content', () => {
const element = new minim.elements.Element([]);
element.element = 'httpResponse';
Expand Down

0 comments on commit 5fd404b

Please sign in to comment.