Skip to content

Commit 5be28f8

Browse files
Kero van Gelderlpil
authored andcommitted
Fix: do not crash w/ TypeError when field decodes a null object (possibly obtained via JSON)
1 parent dc1d195 commit 5be28f8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/gleam_stdlib.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ export function decode_field(value, name) {
673673
) {
674674
const entry = map_get(value, name);
675675
return new Ok(entry.isOk() ? new Some(entry[0]) : new None());
676+
} else if (value === null) {
677+
return not_a_map_error()
676678
} else if (Object.getPrototypeOf(value) == Object.prototype) {
677679
return try_get_field(value, name, () => new Ok(new None()));
678680
} else {

test/gleam/dynamic_test.gleam

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ pub fn dict_from_null_test() {
7979
)
8080
}
8181

82+
@target(javascript)
83+
pub fn null_for_field_test() {
84+
get_null()
85+
|> dynamic.from
86+
|> dynamic.field("x", dynamic.string)
87+
|> should.equal(
88+
Error([DecodeError(expected: "Dict", found: "Null", path: [])]),
89+
)
90+
}
91+
8292
pub fn string_test() {
8393
""
8494
|> dynamic.from

0 commit comments

Comments
 (0)