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

fix: Do not auto-convert string values to boolean COMPASS-4466 #221

Merged
merged 1 commit into from
Nov 12, 2020
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
18 changes: 10 additions & 8 deletions src/utils/bson-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
* 3. etc.
*/
import bson from 'bson';

import { createLogger } from './logger';


const debug = createLogger('bson-csv');

const BOOLEAN_TRUE = ['1', 'true', 'TRUE', true];
Expand Down Expand Up @@ -249,14 +249,16 @@ export const serialize = function(doc) {
return;
}

if (BOOLEAN_TRUE.includes(value)) {
output[newKey] = 'true';
return;
}
if (type === 'Boolean') {
if (BOOLEAN_TRUE.includes(value)) {
output[newKey] = 'true';
return;
}

if (BOOLEAN_FALSE.includes(value)) {
output[newKey] = 'false';
return;
if (BOOLEAN_FALSE.includes(value)) {
output[newKey] = 'false';
return;
}
}

// Embedded documents
Expand Down
105 changes: 62 additions & 43 deletions src/utils/bson-csv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,49 @@ describe('bson-csv', () => {
expect(bsonCSV.Boolean.fromString('true')).to.equal(true);
expect(bsonCSV.Boolean.fromString('TRUE')).to.equal(true);
});
it('should serialize as a string', () => {
expect(serialize({ value: false })).to.deep.equal({
value: 'false'
});

expect(serialize({ value: true })).to.deep.equal({
value: 'true'
});
});
it('should not auto-convert other values', () => {
expect(serialize({ value: 'false' })).to.deep.equal({
value: 'false'
});
expect(serialize({ value: 'FALSE' })).to.deep.equal({
value: 'FALSE'
});
expect(serialize({ value: 'true' })).to.deep.equal({
value: 'true'
});
expect(serialize({ value: 'TRUE' })).to.deep.equal({
value: 'TRUE'
});
expect(serialize({ value: '0' })).to.deep.equal({
value: '0'
});
expect(serialize({ value: '1' })).to.deep.equal({
value: '1'
});
});
});
describe('Number', () => {
it('should work', () => {
it('should serialize numbers as strings', () => {
expect(serialize({ value: 0 })).to.deep.equal({
value: '0'
});
expect(serialize({ value: 1 })).to.deep.equal({
value: '1'
});
expect(serialize({ value: -1.35 })).to.deep.equal({
value: '-1.35'
});
});
it('should deserialize numbers from strings', () => {
expect(bsonCSV.Number.fromString('1')).to.equal(1);
});
});
Expand All @@ -47,29 +87,6 @@ describe('bson-csv', () => {
'Date of Transfer': '2017-01-13T00:00:00Z'
});
});
});
describe('Undefined', () => {
it('should serialize as a string', () => {
expect(serialize({ value: undefined })).to.deep.equal({
value: 'undefined'
});
});
});
describe('Null', () => {
it('should serialize as a string', () => {
expect(serialize({ value: null })).to.deep.equal({
value: 'null'
});
});
});
describe('RegExp', () => {
it('should serialize as a string', () => {
expect(serialize({ value: /^mongodb/ })).to.deep.equal({
value: '/^mongodb/'
});
});
});
describe('Date', () => {
it('should detect value:<Date> as Date', () => {
expect(
detectType(new Date('2020-03-19T20:02:48.406Z'))
Expand All @@ -92,6 +109,27 @@ describe('bson-csv', () => {
});
});
});
describe('Undefined', () => {
it('should serialize as a string', () => {
expect(serialize({ value: undefined })).to.deep.equal({
value: 'undefined'
});
});
});
describe('Null', () => {
it('should serialize as a string', () => {
expect(serialize({ value: null })).to.deep.equal({
value: 'null'
});
});
});
describe('RegExp', () => {
it('should serialize as a string', () => {
expect(serialize({ value: /^mongodb/ })).to.deep.equal({
value: '/^mongodb/'
});
});
});
describe('Array', () => {
it('should serialize as a string of extended JSON', () => {
expect(
Expand Down Expand Up @@ -127,25 +165,6 @@ describe('bson-csv', () => {
});
});
});
describe('Boolean', () => {
it('should serialize as a string', () => {
expect(serialize({ value: false })).to.deep.equal({
value: 'false'
});

expect(serialize({ value: true })).to.deep.equal({
value: 'true'
});
});
it('should serialize as normalized string', () => {
expect(serialize({ value: 'FALSE' })).to.deep.equal({
value: 'false'
});
expect(serialize({ value: 'TRUE' })).to.deep.equal({
value: 'true'
});
});
});
});
describe('bson', () => {
describe('ObjectID', () => {
Expand Down
4 changes: 0 additions & 4 deletions src/utils/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
/* eslint-disable callback-return */
/* eslint-disable complexity */

/**
* TODO: lucas: rename `export-formatters`
*/

import csv from 'fast-csv';
import { EJSON } from 'bson';
import { serialize as flatten } from './bson-csv';
Expand Down