From 908c42fb588ce87f4e197df65db2d238f34837f4 Mon Sep 17 00:00:00 2001 From: Bartholomew Date: Wed, 26 Feb 2020 16:58:46 +0100 Subject: [PATCH] fix: preserve unknown fields value (#3314) --- .../Editor/__tests__/Editor.spec.js | 4 ++-- .../netlify-cms-core/src/formats/helpers.js | 7 +------ .../__tests__/serializeEntryValues.spec.js | 21 +++++++++++++++++++ .../src/lib/serializeEntryValues.js | 7 ++++++- 4 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 packages/netlify-cms-core/src/lib/__tests__/serializeEntryValues.spec.js diff --git a/packages/netlify-cms-core/src/components/Editor/__tests__/Editor.spec.js b/packages/netlify-cms-core/src/components/Editor/__tests__/Editor.spec.js index 20d61fcd..97960b76 100644 --- a/packages/netlify-cms-core/src/components/Editor/__tests__/Editor.spec.js +++ b/packages/netlify-cms-core/src/components/Editor/__tests__/Editor.spec.js @@ -203,7 +203,7 @@ describe('Editor', () => { , ); @@ -232,7 +232,7 @@ describe('Editor', () => { entry: {}, fieldsMetaData: {}, })} - entry={fromJS({ isFetching: false, mediaFiles: [{ id: '1' }] })} + entry={fromJS({ isFetching: false, mediaFiles: [{ id: '1' }], data: {} })} />, ); diff --git a/packages/netlify-cms-core/src/formats/helpers.js b/packages/netlify-cms-core/src/formats/helpers.js index 896cc4c2..ee09cafd 100644 --- a/packages/netlify-cms-core/src/formats/helpers.js +++ b/packages/netlify-cms-core/src/formats/helpers.js @@ -1,12 +1,7 @@ export const sortKeys = (sortedKeys = []) => (a, b) => { const idxA = sortedKeys.indexOf(a); const idxB = sortedKeys.indexOf(b); - if (idxA === -1 || idxB === -1) { - if (a > b) return 1; - if (a < b) return -1; - return 0; - } - + if (idxA === -1 || idxB === -1) return 0; if (idxA > idxB) return 1; if (idxA < idxB) return -1; return 0; diff --git a/packages/netlify-cms-core/src/lib/__tests__/serializeEntryValues.spec.js b/packages/netlify-cms-core/src/lib/__tests__/serializeEntryValues.spec.js new file mode 100644 index 00000000..85c54592 --- /dev/null +++ b/packages/netlify-cms-core/src/lib/__tests__/serializeEntryValues.spec.js @@ -0,0 +1,21 @@ +import { serializeValues, deserializeValues } from '../serializeEntryValues'; +import { fromJS } from 'immutable'; + +const values = fromJS({ title: 'New Post', unknown: 'Unknown Field' }); +const fields = fromJS([{ name: 'title', widget: 'string' }]); + +describe('serializeValues', () => { + it('should retain unknown fields', () => { + expect(serializeValues(values, fields)).toEqual( + fromJS({ title: 'New Post', unknown: 'Unknown Field' }), + ); + }); +}); + +describe('deserializeValues', () => { + it('should retain unknown fields', () => { + expect(deserializeValues(values, fields)).toEqual( + fromJS({ title: 'New Post', unknown: 'Unknown Field' }), + ); + }); +}); diff --git a/packages/netlify-cms-core/src/lib/serializeEntryValues.js b/packages/netlify-cms-core/src/lib/serializeEntryValues.js index dca2a343..9f13c46d 100644 --- a/packages/netlify-cms-core/src/lib/serializeEntryValues.js +++ b/packages/netlify-cms-core/src/lib/serializeEntryValues.js @@ -27,7 +27,7 @@ const runSerializer = (values, fields, method) => { * registered serializers. If the field is a list or object, call recursively * for nested fields. */ - return fields.reduce((acc, field) => { + let serializedData = fields.reduce((acc, field) => { const fieldName = field.get('name'); const value = values.get(fieldName); const serializer = getWidgetValueSerializer(field.get('widget')); @@ -58,6 +58,11 @@ const runSerializer = (values, fields, method) => { return acc; }, Map()); + + //preserve unknown fields value + serializedData = values.mergeDeep(serializedData); + + return serializedData; }; export const serializeValues = (values, fields) => {