-
Notifications
You must be signed in to change notification settings - Fork 129
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
Disallow serde serialize/deserialize unsigned integer types #72
Conversation
Should we replace these with functions that users can refer to, so people can still keep the original functionality if necessary? // something like this -- pseudocode!
#[inline]
fn serialize_u64_into_f64(serializer: Encoder, value: u64) -> EncoderResult<Bson> {
serializer.serialize_f64(value as f64)
}
#[derive(Serialize, Debug)]
struct Thing {
#[serde(serialize_with = "bson::encoder::serialize_u64_into_f64")]
s: u64,
} |
Hmm, maybe. But I don't think it is correct to serialize It is reasonable to do this for compatibility. |
Added backward compatibility support mod #[test]
fn test_compat_u2f() {
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
struct Foo {
#[serde(with = "bson::compat::u2f")]
x: u32
}
let foo = Foo { x: 20 };
let b = bson::to_bson(&foo).unwrap();
assert_eq!(b, Bson::Document(doc! { "x" => (Bson::FloatingPoint(20.0)) }));
let de_foo = bson::from_bson::<Foo>(b).unwrap();
assert_eq!(de_foo, foo);
} |
383c472
to
f25589c
Compare
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, LGTM. unfortunate that we don't have a changelog to document this 😅 Maybe we should add a section in the README to document the fact that mongodb doesn't support unsigned integers, and this module is provided for serialization compatibility.
@kyeah How about this? |
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.
some small grammatical comments but looks good otherwise. Thanks for adding it!
README.md
Outdated
|
||
## Breaking Changes | ||
|
||
In BSON specification, it doesn't support any kind of _unsigned integer types_, for example, `u32`. In the older version of this crate (< `v0.8.0`), if you uses `serde` to serialize _unsigned integer types_ into BSON, it will store them with `Bson::FloatingPoint` type. From `v0.8.0`, we removed this behavior and simply returned an error when you want to serialize _unsigned integer types_ to BSON. [#72](https://github.com/zonyitoo/bson-rs/pull/72) |
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.
"In the BSON specification, unsigned integer types are unsupported; for example"...
README.md
Outdated
|
||
In BSON specification, it doesn't support any kind of _unsigned integer types_, for example, `u32`. In the older version of this crate (< `v0.8.0`), if you uses `serde` to serialize _unsigned integer types_ into BSON, it will store them with `Bson::FloatingPoint` type. From `v0.8.0`, we removed this behavior and simply returned an error when you want to serialize _unsigned integer types_ to BSON. [#72](https://github.com/zonyitoo/bson-rs/pull/72) | ||
|
||
For backward compatibility, we provided a mod `bson::compat::u2f` to explicitly serialize _unsigned integer types_ into BSON's floating point value as follows: |
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.
we
=> we've
README.md
Outdated
} | ||
``` | ||
|
||
In this example, we added an attribute `#[serde(with = "bson::compat::u2f")]` on field `x`, which will tell `serde` to uses the `bson::compat::u2f::serialize` and `bson::compat::u2f::deserialize` to process this field. |
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.
uses
=> use
"the bson::compat::u2f::serialize
and bson::compat::u2f::deserialize
methods"
ffa0b64
to
8d396e8
Compare
Already done. |
awesome, LGTM! |
Already published to crates.io. |
* [mongodb#70] Disallow serde serialize/deserialize unsigned integer types * add compat mod for backward compatibility * Added breaking changes in README
u16
,u32
andu64
to be serialize to / deserialize from BSON