-
-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow deserialization of missing objects properties into Option<> (#3767
) * Allow deserialization of missing objects properties into Option<> Currently the TryFromJs derive macro requires that all properties be defined in the object, whether they are declared as Option<> or not. This commit makes it so if the field is not found in the JsObject, we try to deserialize undefined. This is more in line with JavaScript behaviour. * Fix cargo fmt * Fix clippies * Empty commit to trigger rerun of workflow * Remove circular dependency between boa_engine and boa_macros * Missing "::" in macro --------- Co-authored-by: jedel1043 <jedel0124@gmail.com>
- Loading branch information
Showing
6 changed files
with
78 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#![allow(unused_crate_dependencies)] | ||
|
||
use boa_engine::value::TryFromJs; | ||
use boa_engine::Source; | ||
|
||
#[derive(PartialEq, Eq, TryFromJs)] | ||
struct Deserialize { | ||
required: String, | ||
optional: Option<String>, | ||
} | ||
|
||
#[test] | ||
fn optional_missing_try_from_js() { | ||
let mut context = boa_engine::Context::default(); | ||
let value = context | ||
.eval(Source::from_bytes( | ||
r#" | ||
let empty = { | ||
"required":"foo", | ||
}; | ||
empty | ||
"#, | ||
)) | ||
.unwrap(); | ||
|
||
let deserialized: Deserialize = Deserialize::try_from_js(&value, &mut context).unwrap(); | ||
assert_eq!(deserialized.required, "foo"); | ||
assert_eq!(deserialized.optional, None); | ||
} | ||
|
||
#[test] | ||
fn optional_try_from_js() { | ||
let mut context = boa_engine::Context::default(); | ||
let value = context | ||
.eval(Source::from_bytes( | ||
r#" | ||
let empty = { | ||
"required": "foo", | ||
"optional": "bar", | ||
}; | ||
empty | ||
"#, | ||
)) | ||
.unwrap(); | ||
|
||
let deserialized: Deserialize = Deserialize::try_from_js(&value, &mut context).unwrap(); | ||
assert_eq!(deserialized.required, "foo"); | ||
assert_eq!(deserialized.optional, Some("bar".to_string())); | ||
} | ||
|
||
#[test] | ||
fn required_missing_try_from_js() { | ||
let mut context = boa_engine::Context::default(); | ||
let value = context | ||
.eval(Source::from_bytes( | ||
r" | ||
let value = {}; | ||
value | ||
", | ||
)) | ||
.unwrap(); | ||
|
||
assert!( | ||
Deserialize::try_from_js(&value, &mut context).is_err(), | ||
"foo" | ||
); | ||
} |