From 5096e7ff3dc8a19fab5506f59c75c60b4f7f6e40 Mon Sep 17 00:00:00 2001 From: Thomas Cameron Date: Fri, 21 Apr 2023 08:39:10 +0000 Subject: [PATCH] meta data serde test --- .../aws-smithy-types/src/error/metadata.rs | 34 +++++++++++++++++++ .../test_data/error_meta_data.json | 7 ++++ .../test_data/error_meta_data_empty.json | 5 +++ 3 files changed, 46 insertions(+) create mode 100644 rust-runtime/aws-smithy-types/test_data/error_meta_data.json create mode 100644 rust-runtime/aws-smithy-types/test_data/error_meta_data_empty.json diff --git a/rust-runtime/aws-smithy-types/src/error/metadata.rs b/rust-runtime/aws-smithy-types/src/error/metadata.rs index 9036dc94ec8..bb07f55bfe1 100644 --- a/rust-runtime/aws-smithy-types/src/error/metadata.rs +++ b/rust-runtime/aws-smithy-types/src/error/metadata.rs @@ -56,8 +56,23 @@ pub struct ErrorMetadata { } /// Builder for [`ErrorMetadata`]. +#[cfg_attr( + all(aws_sdk_unstable, feature = "serde-serialize"), + derive(serde::Serialize) +)] +#[cfg_attr( + all(aws_sdk_unstable, feature = "serde-deserialize"), + derive(serde::Deserialize) +)] #[derive(Debug, Default)] pub struct Builder { + #[cfg_attr( + any( + all(aws_sdk_unstable, feature = "serde-deserialize"), + all(aws_sdk_unstable, feature = "serde-serialize") + ), + serde(flatten) + )] inner: ErrorMetadata, } @@ -173,3 +188,22 @@ impl fmt::Display for ErrorMetadata { } impl std::error::Error for ErrorMetadata {} + +#[cfg(all(test, any( + all(aws_sdk_unstable, feature = "serde-deserialize"), + all(aws_sdk_unstable, feature = "serde-serialize") +)))] +mod test { + use super::*; + + #[test] + /// tests de/ser on ErrorMetaData. + fn test_error_meta_data() { + let data = Builder::default().code("code").message("message").custom("hello", "world"); + let ok = serde_json::to_string_pretty(&EMPTY_ERROR_METADATA).unwrap(); + assert_eq!(&ok, include_str!("../../test-data/error_meta_data_empty.json")); + assert_eq!(serde_json::from_str(include_str!("../../test-data/error_meta_data.json")).unwrap(), &data); + assert_eq!(serde_json::from_str(include_str!("../../test-data/error_meta_data.json")).unwrap(), data.build()); + + } +} diff --git a/rust-runtime/aws-smithy-types/test_data/error_meta_data.json b/rust-runtime/aws-smithy-types/test_data/error_meta_data.json new file mode 100644 index 00000000000..db7067d86e4 --- /dev/null +++ b/rust-runtime/aws-smithy-types/test_data/error_meta_data.json @@ -0,0 +1,7 @@ +{ + "code": "code", + "message": "message", + "extras": { + "hello": "world" + } +} diff --git a/rust-runtime/aws-smithy-types/test_data/error_meta_data_empty.json b/rust-runtime/aws-smithy-types/test_data/error_meta_data_empty.json new file mode 100644 index 00000000000..41165f4a49c --- /dev/null +++ b/rust-runtime/aws-smithy-types/test_data/error_meta_data_empty.json @@ -0,0 +1,5 @@ +{ + "code": null, + "message": null, + "extras": null +}