This is an automated email from the ASF dual-hosted git repository. chitralverma pushed a commit to branch core-config-map-json in repository https://gitbox.apache.org/repos/asf/opendal.git
commit b7a900d527a67e4752ff037752bb682b77dea1e1 Author: Chitral Verma <[email protected]> AuthorDate: Fri Nov 27 16:20:36 2026 +0530 feat(core): accept JSON objects for map-valued config options String-keyed configuration could not set HashMap<String, String> options such as hdfs-native `options` and S3 `assume_role_session_tags`. A map option now takes a JSON object with string values, which keeps the case of its keys. --- core/core/src/docs/specs/operator-configuration.md | 6 +++ core/core/src/raw/serde_util.rs | 61 +++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/core/core/src/docs/specs/operator-configuration.md b/core/core/src/docs/specs/operator-configuration.md index 162efe93b9..54a33c5733 100644 --- a/core/core/src/docs/specs/operator-configuration.md +++ b/core/core/src/docs/specs/operator-configuration.md @@ -74,6 +74,12 @@ or `PT1S`. Empty, zero, negative, or otherwise invalid duration values fail with invalid values for other supported built-in options also fail with `ConfigInvalid`. Available transport options depend on the provider and runtime. +An option that holds a map of strings takes a JSON object whose values are +strings, such as `{"fs.defaultFS":"hdfs://ns"}`. OpenDAL keeps the case of the +keys inside the object, and the last occurrence of a duplicate key wins. Any +other JSON value, including a number, `null`, an array, or a nested object, +fails with `ConfigInvalid`. + ## Trusted input Pass only trusted operator configuration, including the entire URI and any diff --git a/core/core/src/raw/serde_util.rs b/core/core/src/raw/serde_util.rs index 3c5ded9b86..f788b236d1 100644 --- a/core/core/src/raw/serde_util.rs +++ b/core/core/src/raw/serde_util.rs @@ -51,6 +51,10 @@ pub fn new_json_deserialize_error(e: serde_json::Error) -> Error { /// ConfigDeserializer is used to deserialize given configs from `HashMap<String, String>`. /// /// This is only used by our services' config. +/// +/// A map field takes a JSON object with string values, such as +/// `{"fs.defaultFS":"hdfs://ns"}`. Keys inside the object keep their case, +/// unlike the config keys, which are lowercased. pub struct ConfigDeserializer(MapDeserializer<'static, Pairs, de::value::Error>); impl ConfigDeserializer { @@ -303,9 +307,29 @@ impl<'de> Deserializer<'de> for Pair { } } + fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + // Return empty for an empty value, as `deserialize_seq` does. + if self.1.is_empty() { + return MapDeserializer::new(empty::<(String, String)>()).deserialize_map(visitor); + } + + // A JSON object keeps the case of its keys, and its values may contain + // the `,` and `=` that a delimited encoding would need to escape. + let entries: HashMap<String, String> = serde_json::from_str(&self.1).map_err(|e| { + de::Error::custom(format_args!( + "parse config '{}' failed: expected a JSON object with string values: {e}", + self.0 + )) + })?; + MapDeserializer::new(entries.into_iter()).deserialize_map(visitor) + } + serde::forward_to_deserialize_any! { char str string unit newtype_struct enum - bytes byte_buf map unit_struct tuple_struct + bytes byte_buf unit_struct tuple_struct identifier tuple ignored_any struct } @@ -344,6 +368,10 @@ mod tests { vec_value_two: Vec<String>, vec_none: Option<Vec<String>>, vec_empty: Vec<String>, + + map_value: HashMap<String, String>, + map_none: Option<HashMap<String, String>>, + map_empty: HashMap<String, String>, } #[test] @@ -369,6 +397,12 @@ mod tests { map.insert("vec_value_two", "hello,world"); map.insert("vec_none", ""); map.insert("vec_empty", ""); + map.insert( + "map_value", + r#"{"fs.defaultFS":"hdfs://ns","dfs.ha.namenodes.ns":"nn1,nn2"}"#, + ); + map.insert("map_none", ""); + map.insert("map_empty", ""); let map = map .into_iter() .map(|(k, v)| (k.to_string(), v.to_string())) @@ -398,10 +432,35 @@ mod tests { vec_value_two: vec!["hello".to_string(), "world".to_string()], vec_none: None, vec_empty: vec![], + map_value: HashMap::from([ + ("fs.defaultFS".to_string(), "hdfs://ns".to_string()), + ("dfs.ha.namenodes.ns".to_string(), "nn1,nn2".to_string()), + ]), + map_none: None, + map_empty: HashMap::new(), } ); } + #[test] + fn test_config_deserializer_rejects_invalid_map() { + for value in [ + "fs.defaultFS=hdfs://ns", + r#"["fs.defaultFS"]"#, + r#"{"dfs.replication":3}"#, + r#"{"fs.defaultFS":null}"#, + r#"{"fs":{"defaultFS":"hdfs://ns"}}"#, + ] { + let map = HashMap::from([("map_value".to_string(), value.to_string())]); + let err = TestConfig::deserialize(ConfigDeserializer::new(map)).unwrap_err(); + assert!( + err.to_string() + .starts_with("parse config 'map_value' failed: expected a JSON object"), + "{value}: {err}" + ); + } + } + #[test] fn test_part_config_deserializer() { let mut map = HashMap::new();
