Skip to content

Commit

Permalink
Merge pull request #225 from refractproject/kylef/remove-undefined
Browse files Browse the repository at this point in the history
Fix handling of array/object enum samples in JSON 0.6 serialisation
  • Loading branch information
pksunkara committed Mar 15, 2019
2 parents 1e823bc + 9ca769f commit 76949b5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Minim Changelog

## 0.23.2 (2019-03-15)

### Bug Fixes

- Fixes serialisation of array and object sample values in enumerations in
Refract JSON 0.6 serialisation.

## 0.23.1 (2019-02-25)

### Bug Fixes
Expand Down
13 changes: 11 additions & 2 deletions lib/serialisers/JSON06Serialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ module.exports = class JSON06Serialiser extends JSONSerialiser {
attributes.set('default', new this.namespace.elements.Array([defaultValue.content]));
}

// Strip typeAttributes from samples, 0.6 doesn't usually contain them in samples
samples.forEach((sample) => {
sample.content.attributes.remove('typeAttributes');
if (sample.content && sample.content.element) {
sample.content.attributes.remove('typeAttributes');
}
});

// Content -> Samples
Expand All @@ -132,7 +135,13 @@ module.exports = class JSON06Serialiser extends JSONSerialiser {
samples.unshift(element.content);
}

samples = samples.map(sample => new this.namespace.elements.Array([sample.content]));
samples = samples.map((sample) => {
if (sample instanceof this.namespace.elements.Array) {
return [sample];
}

return new this.namespace.elements.Array([sample.content]);
});

if (samples.length) {
attributes.set('samples', samples);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minim",
"version": "0.23.1",
"version": "0.23.2",
"description": "A library for interacting with JSON through Refract elements",
"main": "lib/minim.js",
"scripts": {
Expand Down
46 changes: 46 additions & 0 deletions test/serialisers/JSON06Serialiser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,52 @@ describe('JSON 0.6 Serialiser', () => {
// }
});

it('serialises enum with object samples', () => {
const object = new minim.elements.Object();
const array = new minim.elements.Array();

const sample = new minim.elements.Object({ message: 'Hello World' });

const enumeration = new minim.Element();
enumeration.element = 'enum';
enumeration.attributes.set('enumerations', [object, array]);
enumeration.attributes.set('samples', [sample]);

const result = serialiser.serialise(enumeration);

expect(result).to.deep.equal({
element: 'enum',
attributes: {
samples: [
[
{
element: 'object',
content: [
{
element: 'member',
content: {
key: {
element: 'string',
content: 'message',
},
value: {
element: 'string',
content: 'Hello World',
},
},
},
],
},
],
],
},
content: [
{ element: 'object' },
{ element: 'array' },
],
});
});

it('serialises enum with fixed values', () => {
const defaultElement = new minim.Element(new minim.elements.String('North'));
defaultElement.element = 'enum';
Expand Down

0 comments on commit 76949b5

Please sign in to comment.