From e1a530ba779dcdd0cd74fbd0edf6022b7bd73caf Mon Sep 17 00:00:00 2001 From: Philip Jenvey Date: Tue, 17 Dec 2019 11:33:59 -0800 Subject: [PATCH] fix: ignore the collection field in POSTS also (followup to 013eaafd) Closes #376 --- src/server/test.rs | 16 ++++++++++++---- src/web/extractors.rs | 11 +++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/server/test.rs b/src/server/test.rs index 80e9bdb1..98d7391f 100644 --- a/src/server/test.rs +++ b/src/server/test.rs @@ -373,13 +373,21 @@ fn put_bso() { } #[test] -fn put_meta_storage() { +fn bsos_can_have_a_collection_field() { let start = SyncTimestamp::default(); // test that "collection" is accepted, even if ignored - let body = json!({"id": "global", "collection": "meta", "payload": "SomePayload"}); - let bytes = test_endpoint_with_body(http::Method::PUT, "/1.5/42/storage/meta/global", body); - let result: PutBso = serde_json::from_slice(&bytes).unwrap(); + let bso1 = json!({"id": "global", "collection": "meta", "payload": "SomePayload"}); + let bsos = json!( + [bso1, + {"id": "2", "collection": "foo", "payload": "SomePayload"}, + ]); + let bytes = test_endpoint_with_body(http::Method::POST, "/1.5/42/storage/meta", bsos); + let result: PostBsos = serde_json::from_slice(&bytes.to_vec()).unwrap(); + assert_eq!(result.success.len(), 2); + assert_eq!(result.failed.len(), 0); + let bytes = test_endpoint_with_body(http::Method::PUT, "/1.5/42/storage/meta/global", bso1); + let result: PutBso = serde_json::from_slice(&bytes).unwrap(); assert!(result >= start); } diff --git a/src/web/extractors.rs b/src/web/extractors.rs index 6269bb96..b0e5dfb6 100644 --- a/src/web/extractors.rs +++ b/src/web/extractors.rs @@ -75,8 +75,15 @@ impl BatchBsoBody { /// Function to convert valid raw JSON BSO body to a BatchBsoBody fn from_raw_bso(val: &Value) -> Result { let map = val.as_object().ok_or("invalid json")?; - // Verify all the keys are valid. modified is allowed but ignored - let valid_keys = ["id", "sortindex", "payload", "ttl", "modified"]; + // Verify all the keys are valid. modified/collection are allowed but ignored + let valid_keys = [ + "id", + "sortindex", + "payload", + "ttl", + "modified", + "collection", + ]; for key_name in map.keys() { if !valid_keys.contains(&key_name.as_str()) { return Err(format!("unknown field {}", key_name));