Skip to content

Commit a4b36e7

Browse files
authored
Merge pull request #50 from CosmWasm/test-flatten
Test #[serde(flatten)] support
2 parents 6158fe3 + 56c81ec commit a4b36e7

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ project adheres to [Semantic Versioning](http://semver.org/).
1010
### Added
1111

1212
- Add support for map (de)serialization.
13+
- Add support for `#[serde(flatten)]` (de)serialization ([#20]).
14+
15+
[#20]: https://github.com/CosmWasm/serde-json-wasm/issues/20
1316

1417
### Changed
1518

src/de/mod.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ where
687687
#[cfg(test)]
688688
mod tests {
689689
use super::from_str;
690-
use serde_derive::Deserialize;
690+
use serde_derive::{Deserialize, Serialize};
691691

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

1021+
#[test]
1022+
fn struct_with_flatten() {
1023+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
1024+
struct Pagination {
1025+
limit: u64,
1026+
offset: u64,
1027+
total: u64,
1028+
}
1029+
1030+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
1031+
struct Users {
1032+
users: Vec<String>,
1033+
1034+
#[serde(flatten)]
1035+
pagination: Pagination,
1036+
}
1037+
1038+
let expected = Users {
1039+
users: vec!["joe".to_string(), "alice".to_string()],
1040+
pagination: Pagination {
1041+
offset: 100,
1042+
limit: 20,
1043+
total: 102,
1044+
},
1045+
};
1046+
1047+
assert_eq!(
1048+
from_str::<Users>(r#"{"users":["joe","alice"],"limit":20,"offset":100,"total":102}"#)
1049+
.unwrap(),
1050+
expected,
1051+
);
1052+
}
1053+
10211054
#[test]
10221055
fn ignoring_extra_fields() {
10231056
#[derive(Debug, Deserialize, PartialEq)]

src/ser/mod.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl ser::SerializeStructVariant for Unreachable {
539539
mod tests {
540540

541541
use super::to_string;
542-
use serde_derive::Serialize;
542+
use serde_derive::{Deserialize, Serialize};
543543

544544
#[test]
545545
fn bool() {
@@ -988,6 +988,43 @@ mod tests {
988988
);
989989
}
990990

991+
#[test]
992+
fn struct_with_flatten() {
993+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
994+
struct Pagination {
995+
limit: u64,
996+
offset: u64,
997+
total: u64,
998+
}
999+
1000+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
1001+
struct Users {
1002+
users: Vec<String>,
1003+
1004+
#[serde(flatten)]
1005+
pagination: Pagination,
1006+
}
1007+
1008+
let users = Users {
1009+
users: vec!["joe".to_string(), "alice".to_string()],
1010+
pagination: Pagination {
1011+
offset: 100,
1012+
limit: 20,
1013+
total: 102,
1014+
},
1015+
};
1016+
1017+
assert_eq!(
1018+
to_string(&users).unwrap(),
1019+
r#"{"users":["joe","alice"],"limit":20,"offset":100,"total":102}"#
1020+
);
1021+
assert_eq!(
1022+
to_string(&users).unwrap(),
1023+
serde_json::to_string(&users).unwrap(),
1024+
"serialization must match serde_json implementation"
1025+
);
1026+
}
1027+
9911028
#[test]
9921029
fn btree_map() {
9931030
use std::collections::BTreeMap;

0 commit comments

Comments
 (0)