Skip to content

Commit

Permalink
fix: overwriteRequestBody root should overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdclark committed Oct 27, 2023
1 parent 1478d4c commit 200dfb9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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\\",
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 16 additions & 2 deletions src/application/overwrites/overwriteRequestBody.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 6 additions & 4 deletions src/application/overwrites/overwriteRequestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>), newValue }
newValue = { ...(originalValue as Record<string, unknown>), ...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) {
Expand Down

0 comments on commit 200dfb9

Please sign in to comment.