use serde_path_to_error as serde_path;
use super::StixError;
pub fn json_kind(value: &serde_json::Value) -> &'static str {
match value {
serde_json::Value::Null => "null",
serde_json::Value::Bool(_) => "boolean",
serde_json::Value::Number(_) => "number",
serde_json::Value::String(_) => "string",
serde_json::Value::Array(_) => "array",
serde_json::Value::Object(_) => "object",
}
}
pub fn non_object_json_message(received: &str) -> String {
format!("Expected a STIX object (JSON object), got {received}.")
}
fn json_kind_from_serde_got(got: &str) -> &'static str {
if got.starts_with("sequence") {
"array"
} else if got.starts_with("map") {
"object"
} else if got.starts_with("integer")
|| got.starts_with("floating point")
|| got.starts_with("number")
{
"number"
} else if got.starts_with("string") {
"string"
} else if got.starts_with("boolean") {
"boolean"
} else if got == "null" {
"null"
} else {
"unexpected input"
}
}
pub fn classify_serde_error(
error: serde_path::Error<serde_json::Error>,
object_type: &str,
) -> StixError {
let path = error.path().to_string();
let inner = error.into_inner();
let message = inner.to_string();
if inner.classify() != serde_json::error::Category::Data {
return StixError::DeserializationError(message);
}
if let Some(rest) = message.strip_prefix("missing field ") {
let property = strip_position_suffix(rest).trim_matches('`');
return StixError::MissingProperty {
object_type: object_type.to_string(),
property: property.to_string(),
};
}
if let Some(rest) = message.strip_prefix("invalid type: ") {
let rest = strip_position_suffix(rest);
if let Some((got, expected)) = rest.split_once(", expected ") {
if path == "." && expected.starts_with("struct") {
return StixError::DeserializationError(non_object_json_message(
json_kind_from_serde_got(got),
));
}
if path != "." {
return StixError::InvalidPropertyType {
object_type: object_type.to_string(),
property: path,
expected: expected.to_string(),
got: got.to_string(),
};
}
}
}
StixError::DeserializationError(message)
}
fn strip_position_suffix(message: &str) -> &str {
match message.find(" at line ") {
Some(pos) => &message[..pos],
None => message,
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde::Deserialize;
fn classify_root(json: &str) -> StixError {
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Probe {
#[serde(flatten)]
#[expect(dead_code)]
common: crate::base::CommonProperties,
}
let result: Result<Probe, _> =
serde_path::deserialize(&mut serde_json::Deserializer::from_str(json));
match result {
Ok(_) => panic!("probe should not deserialize"),
Err(e) => classify_serde_error(e, "attack-pattern"),
}
}
#[test]
fn non_object_input_is_reported_plainly() {
for (json, kind) in [
("[1,2,3]", "array"),
("42", "number"),
("\"hello\"", "string"),
("true", "boolean"),
("null", "null"),
] {
let error = classify_root(json);
match error {
StixError::DeserializationError(message) => {
assert_eq!(
message,
format!("Expected a STIX object (JSON object), got {kind}."),
"input: {json}"
);
}
other => panic!("expected DeserializationError for {json}, got {other:?}"),
}
}
}
#[test]
fn object_input_with_bad_property_keeps_property_classification() {
let error = classify_root(
r#"{"id": "attack-pattern--12345678-1234-5678-1234-567812345678", "created": 123}"#,
);
match error {
StixError::DeserializationError(message) => {
assert!(
!message.starts_with("Expected a STIX object"),
"object input misclassified: {message}"
);
}
other => match other {
StixError::InvalidPropertyType { .. } | StixError::MissingProperty { .. } => {}
other => panic!("unexpected variant: {other:?}"),
},
}
}
}