Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 9b54b9c

Browse files
authored
fix: Do not auto-convert string values to boolean COMPASS-4466 (#221)
1 parent 4b377fe commit 9b54b9c

File tree

3 files changed

+72
-55
lines changed

3 files changed

+72
-55
lines changed

src/utils/bson-csv.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
* 3. etc.
2121
*/
2222
import bson from 'bson';
23-
2423
import { createLogger } from './logger';
2524

25+
2626
const debug = createLogger('bson-csv');
2727

2828
const BOOLEAN_TRUE = ['1', 'true', 'TRUE', true];
@@ -249,14 +249,16 @@ export const serialize = function(doc) {
249249
return;
250250
}
251251

252-
if (BOOLEAN_TRUE.includes(value)) {
253-
output[newKey] = 'true';
254-
return;
255-
}
252+
if (type === 'Boolean') {
253+
if (BOOLEAN_TRUE.includes(value)) {
254+
output[newKey] = 'true';
255+
return;
256+
}
256257

257-
if (BOOLEAN_FALSE.includes(value)) {
258-
output[newKey] = 'false';
259-
return;
258+
if (BOOLEAN_FALSE.includes(value)) {
259+
output[newKey] = 'false';
260+
return;
261+
}
260262
}
261263

262264
// Embedded documents

src/utils/bson-csv.spec.js

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,49 @@ describe('bson-csv', () => {
2525
expect(bsonCSV.Boolean.fromString('true')).to.equal(true);
2626
expect(bsonCSV.Boolean.fromString('TRUE')).to.equal(true);
2727
});
28+
it('should serialize as a string', () => {
29+
expect(serialize({ value: false })).to.deep.equal({
30+
value: 'false'
31+
});
32+
33+
expect(serialize({ value: true })).to.deep.equal({
34+
value: 'true'
35+
});
36+
});
37+
it('should not auto-convert other values', () => {
38+
expect(serialize({ value: 'false' })).to.deep.equal({
39+
value: 'false'
40+
});
41+
expect(serialize({ value: 'FALSE' })).to.deep.equal({
42+
value: 'FALSE'
43+
});
44+
expect(serialize({ value: 'true' })).to.deep.equal({
45+
value: 'true'
46+
});
47+
expect(serialize({ value: 'TRUE' })).to.deep.equal({
48+
value: 'TRUE'
49+
});
50+
expect(serialize({ value: '0' })).to.deep.equal({
51+
value: '0'
52+
});
53+
expect(serialize({ value: '1' })).to.deep.equal({
54+
value: '1'
55+
});
56+
});
2857
});
2958
describe('Number', () => {
30-
it('should work', () => {
59+
it('should serialize numbers as strings', () => {
60+
expect(serialize({ value: 0 })).to.deep.equal({
61+
value: '0'
62+
});
63+
expect(serialize({ value: 1 })).to.deep.equal({
64+
value: '1'
65+
});
66+
expect(serialize({ value: -1.35 })).to.deep.equal({
67+
value: '-1.35'
68+
});
69+
});
70+
it('should deserialize numbers from strings', () => {
3171
expect(bsonCSV.Number.fromString('1')).to.equal(1);
3272
});
3373
});
@@ -47,29 +87,6 @@ describe('bson-csv', () => {
4787
'Date of Transfer': '2017-01-13T00:00:00Z'
4888
});
4989
});
50-
});
51-
describe('Undefined', () => {
52-
it('should serialize as a string', () => {
53-
expect(serialize({ value: undefined })).to.deep.equal({
54-
value: 'undefined'
55-
});
56-
});
57-
});
58-
describe('Null', () => {
59-
it('should serialize as a string', () => {
60-
expect(serialize({ value: null })).to.deep.equal({
61-
value: 'null'
62-
});
63-
});
64-
});
65-
describe('RegExp', () => {
66-
it('should serialize as a string', () => {
67-
expect(serialize({ value: /^mongodb/ })).to.deep.equal({
68-
value: '/^mongodb/'
69-
});
70-
});
71-
});
72-
describe('Date', () => {
7390
it('should detect value:<Date> as Date', () => {
7491
expect(
7592
detectType(new Date('2020-03-19T20:02:48.406Z'))
@@ -92,6 +109,27 @@ describe('bson-csv', () => {
92109
});
93110
});
94111
});
112+
describe('Undefined', () => {
113+
it('should serialize as a string', () => {
114+
expect(serialize({ value: undefined })).to.deep.equal({
115+
value: 'undefined'
116+
});
117+
});
118+
});
119+
describe('Null', () => {
120+
it('should serialize as a string', () => {
121+
expect(serialize({ value: null })).to.deep.equal({
122+
value: 'null'
123+
});
124+
});
125+
});
126+
describe('RegExp', () => {
127+
it('should serialize as a string', () => {
128+
expect(serialize({ value: /^mongodb/ })).to.deep.equal({
129+
value: '/^mongodb/'
130+
});
131+
});
132+
});
95133
describe('Array', () => {
96134
it('should serialize as a string of extended JSON', () => {
97135
expect(
@@ -127,25 +165,6 @@ describe('bson-csv', () => {
127165
});
128166
});
129167
});
130-
describe('Boolean', () => {
131-
it('should serialize as a string', () => {
132-
expect(serialize({ value: false })).to.deep.equal({
133-
value: 'false'
134-
});
135-
136-
expect(serialize({ value: true })).to.deep.equal({
137-
value: 'true'
138-
});
139-
});
140-
it('should serialize as normalized string', () => {
141-
expect(serialize({ value: 'FALSE' })).to.deep.equal({
142-
value: 'false'
143-
});
144-
expect(serialize({ value: 'TRUE' })).to.deep.equal({
145-
value: 'true'
146-
});
147-
});
148-
});
149168
});
150169
describe('bson', () => {
151170
describe('ObjectID', () => {

src/utils/formatters.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
/* eslint-disable callback-return */
33
/* eslint-disable complexity */
44

5-
/**
6-
* TODO: lucas: rename `export-formatters`
7-
*/
8-
95
import csv from 'fast-csv';
106
import { EJSON } from 'bson';
117
import { serialize as flatten } from './bson-csv';

0 commit comments

Comments
 (0)