Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overwriteRequestBody root should overwrite #524

Merged
merged 1 commit into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading