-
-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow deserialization of missing objects properties into Option<> #3767
Conversation
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.
I decided to make this PR tentatively as a start for the discussion. I was not sure if the current behaviour was discussed before (I could not find an discussion, issues or PRs that mentioned it). I think this PR makes sense in the context where people are interfacing rust code with JavaScript calls, as it removes a lot of unnecessary JS when calling into rust synthetic modules. |
I'm pretty sure the current behaviour is not intentional, but I'm pinging @Razican in case I'm wrong. |
@jedel1043 That's what I was hoping for. Do you know why CI is not happy? I think it detects |
This behavior was not defined at the beginning. What should we do when a field doesn't exist? What if the object has more fields? serde has attributes for these cases. I'm happy to go with whatever serde does :) |
I cannot reproduce the CI doc test failures locally. And it seems unrelated to my changes. |
@Razican For de-serialization, WDYT? |
Will take a look to the CI errors. |
Fixed. The circular dependency between |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3767 +/- ##
==========================================
+ Coverage 47.24% 47.73% +0.48%
==========================================
Files 476 454 -22
Lines 46892 44747 -2145
==========================================
- Hits 22154 21358 -796
+ Misses 24738 23389 -1349 ☔ View full report in Codecov by Sentry. |
Oh thank you. Somehow I missed that. This is ready for review as is then. Personally I'd rather this goes in and then work separately on serde-ifying TryFromJs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Looks good to me! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks good! Just a missing double colon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the contribution!
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.
It changes the following:
JsValue::undefined()
to deserialize instead.This is more in line with JavaScript's expected behaviour. It also allows structure to have partial defined fields.