Skip to content

Commit

Permalink
fix: let values pass without needless sanitization (microsoft#3805)
Browse files Browse the repository at this point in the history
* remove sanitize

* remove check for empty arrays

* fix unit tests

* Update useShell.ts

Co-authored-by: Chris Whitten <[email protected]>
  • Loading branch information
beyackle and cwhitten authored Aug 7, 2020
1 parent 3b0c2c2 commit f4e5f30
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 101 deletions.
20 changes: 0 additions & 20 deletions Composer/packages/client/__tests__/utils/dialogUtil.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
getActivityTypes,
getFriendlyName,
getbreadcrumbLabel,
sanitizeDialogData,
getSelected,
} from '../../src/utils/dialogUtil';

Expand Down Expand Up @@ -240,25 +239,6 @@ describe('getbreadcrumbLabel', () => {
});
});

describe('sanitizeDialogData', () => {
it('return undefined', () => {
const res = sanitizeDialogData();
expect(res).toEqual(undefined);
});

it('return cleaned data', () => {
const res = sanitizeDialogData(dialogs[2].content);
expect(res).toEqual({
$kind: 'kind3',
action: {
action: {
property: true,
},
},
});
});
});

describe('getSelected', () => {
it('return selected path', () => {
let res = getSelected();
Expand Down
17 changes: 1 addition & 16 deletions Composer/packages/client/src/shell/useShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

import { useMemo, useRef } from 'react';
import { ShellApi, ShellData, Shell } from '@bfc/shared';
import isEqual from 'lodash/isEqual';
import { useRecoilValue } from 'recoil';
import formatMessage from 'format-message';

import { updateRegExIntent, renameRegExIntent, updateIntentTrigger } from '../utils/dialogUtil';
import { getDialogData, setDialogData, sanitizeDialogData } from '../utils/dialogUtil';
import { getDialogData, setDialogData } from '../utils/dialogUtil';
import { getFocusPath } from '../utils/navigation';
import { isAbsHosted } from '../utils/envUtil';
import { undoFunctionState } from '../recoilModel/undo/history';
Expand Down Expand Up @@ -102,29 +101,15 @@ export function useShell(source: EventSource): Shell {
updateDialog({ id, content: newDialog.content });
}

function cleanData() {
const cleanedData = sanitizeDialogData(dialogsMap[dialogId]);
if (!isEqual(dialogsMap[dialogId], cleanedData)) {
const payload = {
id: dialogId,
content: cleanedData,
};
updateDialog(payload);
}
}

function navigationTo(path) {
cleanData();
navTo(path, breadcrumb);
}

function focusEvent(subPath) {
cleanData();
selectTo(subPath);
}

function focusSteps(subPaths: string[] = [], fragment?: string) {
cleanData();
let dataPath: string = subPaths[0];

if (source === FORM_EDITOR) {
Expand Down
48 changes: 0 additions & 48 deletions Composer/packages/client/src/utils/dialogUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,54 +316,6 @@ export function setDialogData(dialogsMap: DialogsMap, dialogId: string, dataPath
return set(dialog, dataPath, data);
}

export function sanitizeDialogData(dialogData: any) {
if (dialogData === null || dialogData === '') {
return undefined;
}

if (Array.isArray(dialogData)) {
return dialogData.length > 0 ? dialogData.map(sanitizeDialogData).filter(Boolean) : undefined;
}

if (typeof dialogData === 'object') {
const obj = cloneDeep(dialogData); // Prevent mutation of source object.

for (const key in obj) {
if (obj[key] === undefined || obj[key] === null || obj[key] === '') {
delete obj[key];
continue;
}

const result = sanitizeDialogData(obj[key]);
switch (typeof result) {
case 'undefined':
delete obj[key];
break;
case 'boolean':
obj[key] = result;
break;
case 'object':
if (Object.keys(result).length === 0) {
delete obj[key];
} else {
obj[key] = result;
}
break;
default:
obj[key] = result;
}
}

if (Object.keys(obj).length === 0) {
return undefined;
}

return obj;
}

return dialogData;
}

export function getSelected(focused: string): string {
if (!focused) return '';
return focused.split('.')[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ const ObjectField: React.FC<FieldProps<object>> = function ObjectField(props) {
const handleChange = (field: string) => (data: any) => {
const newData = { ...value };

if (
typeof data === 'undefined' ||
(Array.isArray(data) && data.length === 0) ||
(typeof data === 'string' && data.length === 0)
) {
if (typeof data === 'undefined' || (typeof data === 'string' && data.length === 0)) {
delete newData[field];
} else {
newData[field] = data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,4 @@ describe.only('<ObjectField />', () => {
fireEvent.change(input, { target: { value: 'new name' } });
expect(onChange).toHaveBeenCalledWith({ name: 'new name', age: 21 });
});

it.each([[], ''])('removes property if value is %p', (newValue) => {
const onChange = jest.fn();
const value = {
name: 'old name',
age: 21,
};
const { container } = renderSubject({ onChange, schema, value });
const input = container.querySelectorAll('input')[0];
fireEvent.change(input, { target: { value: JSON.stringify(newValue) } });
expect(onChange).toHaveBeenCalledWith({ age: 21 });
});
});

0 comments on commit f4e5f30

Please sign in to comment.