Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When deserializing JSON, accept both int and string in 'intValue' #1906

Merged
merged 7 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
"value": {
"intValue": "100"
}
},
{
"key": "number/int",
"value": {
"intValue": 100
}
}
]
}
Expand Down
145 changes: 82 additions & 63 deletions opentelemetry-proto/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ pub(crate) mod serializers {
Ok(s) => s,
Err(e) => return Err(e), // Handle the error or return it
};

// Attempt to serialize the intValue field
if let Err(e) = state.serialize_field("intValue", &i.to_string()) {
return Err(e); // Handle the error or return it
}

// Finalize the struct serialization
state.end()
},
}
Some(value) => value.serialize(serializer),
None => serializer.serialize_none(),
},
Expand All @@ -74,76 +74,95 @@ pub(crate) mod serializers {
}

pub fn deserialize_from_value<'de, D>(deserializer: D) -> Result<Option<AnyValue>, D::Error>
where
D: Deserializer<'de>,
{
struct ValueVisitor;
where
D: Deserializer<'de>,
{
struct ValueVisitor;

impl<'de> de::Visitor<'de> for ValueVisitor {
type Value = AnyValue;
#[derive(Deserialize)]
#[serde(untagged)]
enum StringOrInt {
Int(i64),
String(String),
}

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a JSON object for AnyValue")
impl StringOrInt {
fn get_int<'de, V>(&self) -> Result<i64, V::Error>
where
V: de::MapAccess<'de>,
{
match self {
Self::Int(val) => Ok(*val),
Self::String(val) => Ok(val.parse::<i64>().map_err(de::Error::custom)?),
}
}
}

fn visit_map<V>(self, mut map: V) -> Result<AnyValue, V::Error>
where
V: de::MapAccess<'de>,
{
let mut value: Option<any_value::Value> = None;

while let Some(key) = map.next_key::<String>()? {
let key_str = key.as_str();
match key_str {
"stringValue" => {
let s = map.next_value()?;
value = Some(any_value::Value::StringValue(s));
},
"boolValue" => {
let b = map.next_value()?;
value = Some(any_value::Value::BoolValue(b));
},
"intValue" => {
let value_str = map.next_value::<String>()?;
let int_value = value_str.parse::<i64>()
.map_err(de::Error::custom)?;
value = Some(any_value::Value::IntValue(int_value));
},
"doubleValue" => {
let d = map.next_value()?;
value = Some(any_value::Value::DoubleValue(d));
},
"arrayValue" => {
let a = map.next_value()?;
value = Some(any_value::Value::ArrayValue(a));
},
"kvlistValue" => {
let kv = map.next_value()?;
value = Some(any_value::Value::KvlistValue(kv));
},
"bytesValue" => {
let bytes = map.next_value()?;
value = Some(any_value::Value::BytesValue(bytes));
},
_ => {
//skip unknown keys, and handle error later.
continue
impl<'de> de::Visitor<'de> for ValueVisitor {
type Value = AnyValue;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a JSON object for AnyValue")
}

fn visit_map<V>(self, mut map: V) -> Result<AnyValue, V::Error>
where
V: de::MapAccess<'de>,
{
let mut value: Option<any_value::Value> = None;

while let Some(key) = map.next_key::<String>()? {
let key_str = key.as_str();
match key_str {
"stringValue" => {
let s = map.next_value()?;
value = Some(any_value::Value::StringValue(s));
}
"boolValue" => {
let b = map.next_value()?;
value = Some(any_value::Value::BoolValue(b));
}
"intValue" => {
let int_value = map.next_value::<StringOrInt>()?.get_int::<V>()?;
value = Some(any_value::Value::IntValue(int_value));
}
"doubleValue" => {
let d = map.next_value()?;
value = Some(any_value::Value::DoubleValue(d));
}
"arrayValue" => {
let a = map.next_value()?;
value = Some(any_value::Value::ArrayValue(a));
}
"kvlistValue" => {
let kv = map.next_value()?;
value = Some(any_value::Value::KvlistValue(kv));
}
"bytesValue" => {
let bytes = map.next_value()?;
value = Some(any_value::Value::BytesValue(bytes));
}
_ => {
//skip unknown keys, and handle error later.
continue;
}
}
}
}

if let Some(v) = value {
Ok(AnyValue { value: Some(v) })
} else {
Err(de::Error::custom("Invalid data for AnyValue, no known keys found"))
if let Some(v) = value {
Ok(AnyValue { value: Some(v) })
} else {
Err(de::Error::custom(
"Invalid data for AnyValue, no known keys found",
))
}
}
}

let value = deserializer.deserialize_map(ValueVisitor)?;
Ok(Some(value))
}

let value = deserializer.deserialize_map(ValueVisitor)?;
Ok(Some(value))
}

pub fn serialize_u64_to_string<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -167,7 +186,7 @@ where
let s = value.to_string();
serializer.serialize_str(&s)
}

pub fn deserialize_string_to_i64<'de, D>(deserializer: D) -> Result<i64, D::Error>
where
D: Deserializer<'de>,
Expand Down
30 changes: 30 additions & 0 deletions opentelemetry-proto/tests/json_deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,36 @@ mod json_deserialize {
keyvalue.value.unwrap().value.unwrap(),
Value::StringValue("my.service".to_string())
);

let keyvalue: KeyValue = serde_json::from_str(
r#"
{
"key": "service.name",
"value": {
"intValue": "303"
}
}
"#,
)
.unwrap();

assert_eq!(keyvalue.key, "service.name".to_string());
assert_eq!(keyvalue.value.unwrap().value.unwrap(), Value::IntValue(303));

let keyvalue: KeyValue = serde_json::from_str(
r#"
{
"key": "service.name",
"value": {
"intValue": 303
}
}
"#,
)
.unwrap();

assert_eq!(keyvalue.key, "service.name".to_string());
assert_eq!(keyvalue.value.unwrap().value.unwrap(), Value::IntValue(303));
}

#[test]
Expand Down
Loading