From 200dfb9dd5ae10a12ac7f17fbfb217068272f732 Mon Sep 17 00:00:00 2001 From: Jack Clark <1485654+jackdclark@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:09:43 +0000 Subject: [PATCH] fix: overwriteRequestBody root should overwrite --- .../overwriteRequestBody.test.ts.snap | 8 +++++++- .../overwrites/overwriteRequestBody.test.ts | 18 ++++++++++++++++-- .../overwrites/overwriteRequestBody.ts | 10 ++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/application/overwrites/__snapshots__/overwriteRequestBody.test.ts.snap b/src/application/overwrites/__snapshots__/overwriteRequestBody.test.ts.snap index 356e3270..ab501151 100644 --- a/src/application/overwrites/__snapshots__/overwriteRequestBody.test.ts.snap +++ b/src/application/overwrites/__snapshots__/overwriteRequestBody.test.ts.snap @@ -1751,7 +1751,7 @@ Array [ ] `; -exports[`overwriteRequestBody should extend the root request body 1`] = ` +exports[`overwriteRequestBody should extend the root request body when overwrite is false 1`] = ` "{ \\"name\\": \\"Copper\\", \\"owner_id\\": \\"12345\\", @@ -4151,6 +4151,12 @@ Array [ ] `; +exports[`overwriteRequestBody should overwrite the root request body when overwrite is true 1`] = ` +"{ + \\"foo\\": \\"foo-bar-baz\\" +}" +`; + exports[`overwriteRequestBody should overwrite the urlencoded form boolean variable with string value 1`] = ` Array [ Object { diff --git a/src/application/overwrites/overwriteRequestBody.test.ts b/src/application/overwrites/overwriteRequestBody.test.ts index 1cecf06b..a75b2049 100644 --- a/src/application/overwrites/overwriteRequestBody.test.ts +++ b/src/application/overwrites/overwriteRequestBody.test.ts @@ -11,11 +11,25 @@ import { } from '../../application' describe('overwriteRequestBody', () => { - it('should extend the root request body', async () => { + it('should overwrite the root request body when overwrite is true', async () => { const overwriteValues = [ { key: '.', - value: { foo: 'foo-bar-baz' } + value: { foo: 'foo-bar-baz' }, + overwrite: true + } + ] + const pmOperation = await getPostmanMappedCreateOperation() + const result = overwriteRequestBody(overwriteValues, pmOperation) + expect(result.item.request?.body?.raw).toMatchSnapshot() + }) + + it('should extend the root request body when overwrite is false', async () => { + const overwriteValues = [ + { + key: '.', + value: { foo: 'foo-bar-baz' }, + overwrite: false } ] const pmOperation = await getPostmanMappedCreateOperation() diff --git a/src/application/overwrites/overwriteRequestBody.ts b/src/application/overwrites/overwriteRequestBody.ts index f9b3ab9d..c9f7aad9 100644 --- a/src/application/overwrites/overwriteRequestBody.ts +++ b/src/application/overwrites/overwriteRequestBody.ts @@ -65,15 +65,17 @@ export const overwriteRequestBodyJson = ( if (Array.isArray(originalValue) && Array.isArray(newValue)) { newValue = originalValue.concat(newValue) } else if (isObject(originalValue)) { - newValue = { ...(originalValue as Record), newValue } + newValue = { ...(originalValue as Record), ...newValue } } else { newValue = originalValue + newValue } } - bodyData = root - ? { ...bodyData, ...newValue } - : setByPath(bodyData, overwriteValue.key, newValue) + if (root) { + bodyData = overwriteValue.overwrite ? newValue : { ...bodyData, ...newValue } + } else { + bodyData = setByPath(bodyData, overwriteValue.key, newValue) + } } if (overwriteValue.key && overwriteValue.remove === true) {