Skip to content
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

Test #[serde(flatten)] support #50

Merged
merged 3 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Added

- Add support for map (de)serialization.
- Add support for `#[serde(flatten)]` (de)serialization ([#20]).

[#20]: https://github.com/CosmWasm/serde-json-wasm/issues/20

### Changed

Expand Down
35 changes: 34 additions & 1 deletion src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ where
#[cfg(test)]
mod tests {
use super::from_str;
use serde_derive::Deserialize;
use serde_derive::{Deserialize, Serialize};

#[derive(Debug, Deserialize, PartialEq)]
enum Type {
Expand Down Expand Up @@ -1018,6 +1018,39 @@ mod tests {
assert_eq!(serde_json::from_str::<Nothing>(r#"null"#).unwrap(), Nothing);
}

#[test]
fn struct_with_flatten() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Pagination {
limit: u64,
offset: u64,
total: u64,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great test case. But it would be good to have one or two for the specific version we would use in CosmWasm.

struct ExecMsg1 {
  something: String,
}

struct ExecMsg2 {
  other: u64,
}

/// one example to test
enum ExecMsg {
  #[serde(flatten)]
  One(ExecMsg1),
  #[serde(flatten)]
  Two(ExecMsg2),
}

/// another example to test
enum ExecMsg {
  #[serde(flatten)]
  One(ExecMsg1),
  // important to leave top level along with a flattened variant
  SetAge { age: u32 },
  // also interesting to see if we can use this format with them (unflattened) 
  Custom(CustomData),
}

You can make it a bit nicer than that, but combining a cw20 ExecMsg and my contracts custom one is a clear case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, right. I can easily make the flatten test for enums instead of structs. This is the part we need for CosmWasm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out flatten is not for enums (see feature request).

So this PR would close the ticket and if we hit new issues, we can create more refined ones.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great link.
Yes, we need untagged for that, which is another issue #43

This PR closes the linked issue well

struct Users {
users: Vec<String>,

#[serde(flatten)]
pagination: Pagination,
}

let expected = Users {
users: vec!["joe".to_string(), "alice".to_string()],
pagination: Pagination {
offset: 100,
limit: 20,
total: 102,
},
};

assert_eq!(
from_str::<Users>(r#"{"users":["joe","alice"],"limit":20,"offset":100,"total":102}"#)
.unwrap(),
expected,
);
}

#[test]
fn ignoring_extra_fields() {
#[derive(Debug, Deserialize, PartialEq)]
Expand Down
39 changes: 38 additions & 1 deletion src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ impl ser::SerializeStructVariant for Unreachable {
mod tests {

use super::to_string;
use serde_derive::Serialize;
use serde_derive::{Deserialize, Serialize};

#[test]
fn bool() {
Expand Down Expand Up @@ -988,6 +988,43 @@ mod tests {
);
}

#[test]
fn struct_with_flatten() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Pagination {
limit: u64,
offset: u64,
total: u64,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Users {
users: Vec<String>,

#[serde(flatten)]
pagination: Pagination,
}

let users = Users {
users: vec!["joe".to_string(), "alice".to_string()],
pagination: Pagination {
offset: 100,
limit: 20,
total: 102,
},
};

assert_eq!(
to_string(&users).unwrap(),
r#"{"users":["joe","alice"],"limit":20,"offset":100,"total":102}"#
);
assert_eq!(
to_string(&users).unwrap(),
serde_json::to_string(&users).unwrap(),
"serialization must match serde_json implementation"
);
}

#[test]
fn btree_map() {
use std::collections::BTreeMap;
Expand Down