Skip to content

Commit c108287

Browse files
authored
populate defaultValues in webidl dict. converter when passing null or undefined (#3518)
* populate defaultValues in webidl dict. converter when passing null or undefined * fixup
1 parent 8f950c4 commit c108287

2 files changed

Lines changed: 51 additions & 9 deletions

File tree

lib/web/fetch/webidl.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,9 @@ webidl.interfaceConverter = function (i) {
354354

355355
webidl.dictionaryConverter = function (converters) {
356356
return (dictionary, prefix, argument) => {
357-
const type = webidl.util.Type(dictionary)
358357
const dict = {}
359358

360-
if (type === 'Null' || type === 'Undefined') {
361-
return dict
362-
} else if (type !== 'Object') {
359+
if (dictionary != null && webidl.util.Type(dictionary) !== 'Object') {
363360
throw webidl.errors.exception({
364361
header: prefix,
365362
message: `Expected ${dictionary} to be one of: Null, Undefined, Object.`
@@ -370,21 +367,21 @@ webidl.dictionaryConverter = function (converters) {
370367
const { key, defaultValue, required, converter } = options
371368

372369
if (required === true) {
373-
if (!Object.hasOwn(dictionary, key)) {
370+
if (dictionary == null || !Object.hasOwn(dictionary, key)) {
374371
throw webidl.errors.exception({
375372
header: prefix,
376373
message: `Missing required key "${key}".`
377374
})
378375
}
379376
}
380377

381-
let value = dictionary[key]
382-
const hasDefault = Object.hasOwn(options, 'defaultValue')
378+
let value = dictionary?.[key]
379+
const hasDefault = defaultValue !== undefined
383380

384381
// Only use defaultValue if value is undefined and
385382
// a defaultValue options was provided.
386-
if (hasDefault && value !== null) {
387-
value ??= defaultValue()
383+
if (hasDefault && value === undefined) {
384+
value = defaultValue()
388385
}
389386

390387
// A key can be optional and have no default value.

test/webidl/converters.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,51 @@ describe('webidl.dictionaryConverter', () => {
7676
converter({ Key: 'this key was required!' }, 'converter', 'converter')
7777
})
7878
})
79+
80+
test('null and undefined still populates defaultValue(s)', () => {
81+
const dict = webidl.dictionaryConverter([
82+
{
83+
key: 'key',
84+
converter: webidl.converters.any,
85+
defaultValue: () => 3
86+
}
87+
])
88+
89+
assert.deepStrictEqual(dict(null), { key: 3 })
90+
assert.deepStrictEqual(dict(undefined), { key: 3 })
91+
})
92+
93+
test('null and undefined throw a webidl TypeError with a required key', () => {
94+
const dict = webidl.dictionaryConverter([
95+
{
96+
key: 'key',
97+
converter: webidl.converters.any,
98+
required: true
99+
}
100+
])
101+
102+
assert.throws(() => dict(null, 'prefix'), new TypeError('prefix: Missing required key "key".'))
103+
assert.throws(() => dict(undefined, 'prefix'), new TypeError('prefix: Missing required key "key".'))
104+
})
105+
106+
test('Object type works for functions and regex (etc.)', () => {
107+
const dict = webidl.dictionaryConverter([
108+
{
109+
key: 'key',
110+
converter: webidl.converters.any,
111+
required: true
112+
}
113+
])
114+
115+
function obj () {}
116+
obj.key = 1
117+
118+
const obj2 = / /
119+
obj2.key = 1
120+
121+
assert.deepStrictEqual(dict(obj), { key: 1 })
122+
assert.deepStrictEqual(dict(obj2), { key: 1 })
123+
})
79124
})
80125

81126
test('ArrayBuffer', () => {

0 commit comments

Comments
 (0)