Skip to content

Commit 0125c0f

Browse files
author
Crhistian
committed
🐛 fix bug where filter with boolean false is dropped #66
1 parent 0ada8d8 commit 0125c0f

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

codegen/templates/utils/paramsSerializer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export default function ParamSerializer(originalParams: {
1717
filterVal.forEach(val =>
1818
valuesArray.push(`${key}=${encodeURIComponent(val)}`)
1919
)
20-
} else if (filterVal) {
20+
}
21+
// exclude null, undefined, or empty string but permit boolean false to be serialized
22+
else if (typeof filterVal === 'boolean' || filterVal) {
2123
valuesArray.push(`${key}=${encodeURIComponent(filterVal)}`)
2224
}
2325
}

src/utils/paramsSerializer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export default function ParamSerializer(originalParams: {
1717
filterVal.forEach(val =>
1818
valuesArray.push(`${key}=${encodeURIComponent(val)}`)
1919
)
20-
} else if (filterVal) {
20+
}
21+
// exclude null, undefined, or empty string but permit boolean false to be serialized
22+
else if (typeof filterVal === 'boolean' || filterVal) {
2123
valuesArray.push(`${key}=${encodeURIComponent(filterVal)}`)
2224
}
2325
}

tests/filtering.test.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,30 @@ beforeEach(() => {
1010
Tokens.RemoveImpersonationToken()
1111
})
1212

13-
test('can filter call with boolean', async () => {
13+
test('can filter call with boolean (true)', async () => {
1414
Tokens.SetAccessToken(validToken)
15-
await Me.ListProducts({ filters: { 'xp.Featued': true } })
15+
await Me.ListProducts({ filters: { 'xp.Featured': true } })
1616
expect(mockAxios.get).toHaveBeenCalledTimes(1)
1717
expect(mockAxios.get).toHaveBeenCalledWith(`${apiUrl}/me/products`, {
1818
params: {
19-
filters: { 'xp.Featued': true },
19+
filters: { 'xp.Featured': true },
20+
},
21+
paramsSerializer: expect.any(Function),
22+
timeout: 60000,
23+
headers: {
24+
'Content-Type': 'application/json',
25+
'Authorization': `Bearer ${validToken}`,
26+
},
27+
})
28+
})
29+
30+
test('can filter call with boolean (false)', async () => {
31+
Tokens.SetAccessToken(validToken)
32+
await Me.ListProducts({ filters: { IsSubmitted: false } })
33+
expect(mockAxios.get).toHaveBeenCalledTimes(1)
34+
expect(mockAxios.get).toHaveBeenCalledWith(`${apiUrl}/me/products`, {
35+
params: {
36+
filters: { IsSubmitted: false },
2037
},
2138
paramsSerializer: expect.any(Function),
2239
timeout: 60000,

tests/paramsSerializer.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,23 @@ test('should ignore undefined values', () => {
5858
}
5959
expect(serialize(params)).toBe('FirstName=Bob&xp.FavoriteColor=red')
6060
})
61+
62+
test('should allow false boolean', () => {
63+
const params = {
64+
filters: {
65+
FirstName: 'Bob',
66+
IsSubmitted: false,
67+
},
68+
}
69+
expect(serialize(params)).toBe('FirstName=Bob&IsSubmitted=false')
70+
})
71+
72+
test('should allow true boolean', () => {
73+
const params = {
74+
filters: {
75+
FirstName: 'Bob',
76+
IsSubmitted: true,
77+
},
78+
}
79+
expect(serialize(params)).toBe('FirstName=Bob&IsSubmitted=true')
80+
})

0 commit comments

Comments
 (0)