From 7a2dd1a60c3e85350c1001f94e61f75fa3174fb6 Mon Sep 17 00:00:00 2001 From: Vincent Germain Date: Tue, 26 Sep 2023 10:24:30 +0200 Subject: [PATCH 1/4] fix: resolveOneOf should work with false value --- .../clients/src/helpers/__tests__/marshalling.ts | 12 ++++++++++++ packages/clients/src/helpers/marshalling.ts | 8 +++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/clients/src/helpers/__tests__/marshalling.ts b/packages/clients/src/helpers/__tests__/marshalling.ts index c17bda431..b51702037 100644 --- a/packages/clients/src/helpers/__tests__/marshalling.ts +++ b/packages/clients/src/helpers/__tests__/marshalling.ts @@ -158,6 +158,18 @@ describe('resolveOneOf', () => { ), ).toStrictEqual({}) }) + + it('handles false kind values, like boolean.false', () => { + expect( + resolveOneOf( + [ + { default: undefined, param: 'my_key_1', value: false }, + { default: undefined, param: 'my_key_2', value: true }, + ], + false, + ), + ).toStrictEqual({ my_key_1: false }) + }) }) describe('unmarshalDate', () => { diff --git a/packages/clients/src/helpers/marshalling.ts b/packages/clients/src/helpers/marshalling.ts index c7bc0dd00..609f12a80 100644 --- a/packages/clients/src/helpers/marshalling.ts +++ b/packages/clients/src/helpers/marshalling.ts @@ -41,9 +41,11 @@ export const resolveOneOf = ( }[], isRequired = false, ): Record => { - const elt = list.find(obj => obj.value) || list.find(obj => obj.default) - const value = elt?.value || elt?.default - if (value) return { [elt.param]: value } + const elt = + list.find(obj => obj.value !== undefined) || + list.find(obj => obj.default !== undefined) + const value = elt?.value !== undefined ? elt.value : elt?.default + if (elt && value !== undefined) return { [elt.param]: value } if (isRequired) { const keyList = list.map(obj => obj.param).join(' or ') throw new TypeError(`one of ${keyList} must be indicated in the request`) From ff45458230304e266f5fc75cd87d18827f57ea49 Mon Sep 17 00:00:00 2001 From: Vincent Germain Date: Tue, 26 Sep 2023 11:26:09 +0200 Subject: [PATCH 2/4] feat: keep ?? operator Co-authored-by: Nathanael Demacon --- packages/clients/src/helpers/marshalling.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/clients/src/helpers/marshalling.ts b/packages/clients/src/helpers/marshalling.ts index 609f12a80..311fc4628 100644 --- a/packages/clients/src/helpers/marshalling.ts +++ b/packages/clients/src/helpers/marshalling.ts @@ -44,7 +44,7 @@ export const resolveOneOf = ( const elt = list.find(obj => obj.value !== undefined) || list.find(obj => obj.default !== undefined) - const value = elt?.value !== undefined ? elt.value : elt?.default + const value = elt?.value ?? elt?.default if (elt && value !== undefined) return { [elt.param]: value } if (isRequired) { const keyList = list.map(obj => obj.param).join(' or ') From 51d3916a756d9266a2b5f308dde960c43deb2796 Mon Sep 17 00:00:00 2001 From: Vincent Germain Date: Tue, 26 Sep 2023 11:37:08 +0200 Subject: [PATCH 3/4] refactor: improve formatting Co-authored-by: Nathanael Demacon --- packages/clients/src/helpers/marshalling.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/clients/src/helpers/marshalling.ts b/packages/clients/src/helpers/marshalling.ts index 311fc4628..28ba60913 100644 --- a/packages/clients/src/helpers/marshalling.ts +++ b/packages/clients/src/helpers/marshalling.ts @@ -45,7 +45,10 @@ export const resolveOneOf = ( list.find(obj => obj.value !== undefined) || list.find(obj => obj.default !== undefined) const value = elt?.value ?? elt?.default - if (elt && value !== undefined) return { [elt.param]: value } + if (elt && value !== undefined) { + return { [elt.param]: value } + } + if (isRequired) { const keyList = list.map(obj => obj.param).join(' or ') throw new TypeError(`one of ${keyList} must be indicated in the request`) From dc3e949dd390faa8d20799cdb3f1e3d28f247076 Mon Sep 17 00:00:00 2001 From: Vincent Germain Date: Tue, 26 Sep 2023 11:42:41 +0200 Subject: [PATCH 4/4] feat: prefer ?? operator Co-authored-by: Nathanael Demacon --- packages/clients/src/helpers/marshalling.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/clients/src/helpers/marshalling.ts b/packages/clients/src/helpers/marshalling.ts index 28ba60913..f6c3d0fb5 100644 --- a/packages/clients/src/helpers/marshalling.ts +++ b/packages/clients/src/helpers/marshalling.ts @@ -42,7 +42,7 @@ export const resolveOneOf = ( isRequired = false, ): Record => { const elt = - list.find(obj => obj.value !== undefined) || + list.find(obj => obj.value !== undefined) ?? list.find(obj => obj.default !== undefined) const value = elt?.value ?? elt?.default if (elt && value !== undefined) {