From f98483d0a031a1239a7b958a1ba2140dab0bfd19 Mon Sep 17 00:00:00 2001 From: wusuopu Date: Mon, 10 Oct 2022 18:40:46 +0800 Subject: [PATCH] fix formatContentTypeData for json data --- .../src/content-manager/utils/formatContentTypeData.js | 8 ++++---- .../utils/tests/formatContentTypeData.test.js | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/core/helper-plugin/lib/src/content-manager/utils/formatContentTypeData.js b/packages/core/helper-plugin/lib/src/content-manager/utils/formatContentTypeData.js index 38c0d739f92..08a64513ad9 100644 --- a/packages/core/helper-plugin/lib/src/content-manager/utils/formatContentTypeData.js +++ b/packages/core/helper-plugin/lib/src/content-manager/utils/formatContentTypeData.js @@ -12,14 +12,14 @@ const formatContentTypeData = (data, ct, composSchema) => { const compoUid = getOtherInfos(schema, [current, 'component']); const isRepeatable = getOtherInfos(schema, [current, 'repeatable']); - if (!value) { - acc[current] = value; + if (type === 'json' && value !== undefined) { + acc[current] = JSON.stringify(value, null, 2); return acc; } - if (type === 'json') { - acc[current] = JSON.stringify(value, null, 2); + if (!value) { + acc[current] = value; return acc; } diff --git a/packages/core/helper-plugin/lib/src/content-manager/utils/tests/formatContentTypeData.test.js b/packages/core/helper-plugin/lib/src/content-manager/utils/tests/formatContentTypeData.test.js index 836f8b69608..d78b0485178 100644 --- a/packages/core/helper-plugin/lib/src/content-manager/utils/tests/formatContentTypeData.test.js +++ b/packages/core/helper-plugin/lib/src/content-manager/utils/tests/formatContentTypeData.test.js @@ -87,6 +87,7 @@ describe('STRAPI_HELPER_PLUGIN | utils | formatContentTypeData', () => { dz: { type: 'dynamiczone', components: ['compos.sub-compo'] }, jsonString: { type: 'json' }, jsonObject: { type: 'json' }, + jsonBool: { type: 'json' }, }, }; @@ -119,6 +120,7 @@ describe('STRAPI_HELPER_PLUGIN | utils | formatContentTypeData', () => { ], jsonString: 'hello', jsonObject: { hello: true }, + jsonBool: false, }; const expected = { @@ -136,6 +138,7 @@ describe('STRAPI_HELPER_PLUGIN | utils | formatContentTypeData', () => { ], jsonString: '"hello"', jsonObject: '{\n "hello": true\n}', + jsonBool: 'false', }; expect(formatContentTypeData(data, contentType, components)).toEqual(expected);