From 00b882935f160e6d7567d9a4d2618e02bba96e78 Mon Sep 17 00:00:00 2001 From: Miroslav Prasil Date: Tue, 14 Aug 2018 11:06:42 +0100 Subject: [PATCH] Deserialize "null" to empty Vec for Collections --- src/api/core/organizations.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index fe7313e9..a4568562 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -8,6 +8,8 @@ use db::models::*; use api::{PasswordData, JsonResult, EmptyResult, NumberOrString, JsonUpcase}; use auth::{Headers, AdminHeaders, OwnerHeaders}; +use serde::{Deserialize, Deserializer}; + #[derive(Deserialize)] #[allow(non_snake_case)] @@ -327,6 +329,14 @@ fn get_org_users(org_id: String, headers: AdminHeaders, conn: DbConn) -> JsonRes }))) } +fn deserialize_collections<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + // Deserialize null to empty Vec + Deserialize::deserialize(deserializer).or(Ok(vec![])) +} + #[derive(Deserialize)] #[allow(non_snake_case)] struct CollectionData { @@ -339,6 +349,7 @@ struct CollectionData { struct InviteData { Emails: Vec, Type: NumberOrString, + #[serde(deserialize_with = "deserialize_collections")] Collections: Vec, AccessAll: Option, } @@ -443,11 +454,11 @@ fn get_user(org_id: String, user_id: String, _headers: AdminHeaders, conn: DbCon #[allow(non_snake_case)] struct EditUserData { Type: NumberOrString, + #[serde(deserialize_with = "deserialize_collections")] Collections: Vec, AccessAll: bool, } - #[put("/organizations//users/", data = "", rank = 1)] fn put_organization_user(org_id: String, user_id: String, data: JsonUpcase, headers: AdminHeaders, conn: DbConn) -> EmptyResult { edit_user(org_id, user_id, data, headers, conn)