Skip to content

Commit f25843d

Browse files
committed
fixed broken serialization of duplicated entries in sequences
For example, with the following code ``` var obj = { test: 'canary' }; var array = [ 0, 1 ]; var arrayWithRefs = [ obj, obj, array, array ]; ``` `arrayWithRefs` was serialized as ``` - &ref_0test: canary - *ref0 - &ref_1- 0 - 1 - *ref1 ``` which is incorrect.
1 parent 0215138 commit f25843d

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

lib/js-yaml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports.dump = dumper.dump;
2727
module.exports.safeDump = dumper.safeDump;
2828
module.exports.YAMLException = require('./js-yaml/exception');
2929

30-
// Deprecared schema names from JS-YAML 2.0.x
30+
// Deprecated schema names from JS-YAML 2.0.x
3131
module.exports.MINIMAL_SCHEMA = require('./js-yaml/schema/failsafe');
3232
module.exports.SAFE_SCHEMA = require('./js-yaml/schema/default_safe');
3333
module.exports.DEFAULT_SCHEMA = require('./js-yaml/schema/default_full');

lib/js-yaml/dumper.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -715,10 +715,6 @@ function writeNode(state, level, object, block, compact, iskey) {
715715
block = (0 > state.flowLevel || state.flowLevel > level);
716716
}
717717

718-
if ((null !== state.tag && '?' !== state.tag) || (2 !== state.indent && level > 0)) {
719-
compact = false;
720-
}
721-
722718
var objectOrArray = '[object Object]' === type || '[object Array]' === type,
723719
duplicateIndex,
724720
duplicate;
@@ -728,6 +724,10 @@ function writeNode(state, level, object, block, compact, iskey) {
728724
duplicate = duplicateIndex !== -1;
729725
}
730726

727+
if ((null !== state.tag && '?' !== state.tag) || duplicate || (2 !== state.indent && level > 0)) {
728+
compact = false;
729+
}
730+
731731
if (duplicate && state.usedDuplicates[duplicateIndex]) {
732732
state.dump = '*ref_' + duplicateIndex;
733733
} else {
@@ -738,7 +738,7 @@ function writeNode(state, level, object, block, compact, iskey) {
738738
if (block && (0 !== Object.keys(state.dump).length)) {
739739
writeBlockMapping(state, level, state.dump, compact);
740740
if (duplicate) {
741-
state.dump = '&ref_' + duplicateIndex + (0 === level ? '\n' : '') + state.dump;
741+
state.dump = '&ref_' + duplicateIndex + state.dump;
742742
}
743743
} else {
744744
writeFlowMapping(state, level, state.dump);
@@ -750,7 +750,7 @@ function writeNode(state, level, object, block, compact, iskey) {
750750
if (block && (0 !== state.dump.length)) {
751751
writeBlockSequence(state, level, state.dump, compact);
752752
if (duplicate) {
753-
state.dump = '&ref_' + duplicateIndex + (0 === level ? '\n' : '') + state.dump;
753+
state.dump = '&ref_' + duplicateIndex + state.dump;
754754
}
755755
} else {
756756
writeFlowSequence(state, level, state.dump);

test/issues/0205.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
4+
var assert = require('assert');
5+
var yaml = require('../../');
6+
7+
8+
test('Duplicated objects within array', function () {
9+
var obj = { test: 'canary' };
10+
var arrayWithRefs = [ obj, obj ];
11+
12+
var obtained = yaml.load(yaml.dump(arrayWithRefs));
13+
14+
assert.equal(obtained[0].test, 'canary');
15+
assert.equal(obtained[0], obtained[1]);
16+
});
17+
18+
test('Duplicated arrays within array', function () {
19+
var array = [ 0, 1 ];
20+
var arrayWithRefs = [ array, array ];
21+
22+
var obtained = yaml.load(yaml.dump(arrayWithRefs));
23+
24+
assert.equal(obtained[0][0], 0);
25+
assert.equal(obtained[0][1], 1);
26+
assert.equal(obtained[0], obtained[1]);
27+
});

0 commit comments

Comments
 (0)