Skip to content

Commit

Permalink
fix: ignore the collection field in POSTS also
Browse files Browse the repository at this point in the history
(followup to 013eaaf)

Closes #376
  • Loading branch information
pjenvey committed Dec 17, 2019
1 parent 9809cc5 commit e1a530b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/server/test.rs
Expand Up @@ -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);
}

Expand Down
11 changes: 9 additions & 2 deletions src/web/extractors.rs
Expand Up @@ -75,8 +75,15 @@ impl BatchBsoBody {
/// Function to convert valid raw JSON BSO body to a BatchBsoBody
fn from_raw_bso(val: &Value) -> Result<BatchBsoBody, String> {
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));
Expand Down

0 comments on commit e1a530b

Please sign in to comment.