From b587517e4ea8a601e8045d83999b644528da650d Mon Sep 17 00:00:00 2001 From: David Bernard Date: Thu, 25 Jun 2026 10:13:26 +0200 Subject: [PATCH] fix: process as Custom event any context.type not starting by "dev.cdevent." Signed-off-by: David Bernard --- .../examples/samples_ok/custom_cdeventsx.json | 13 +++++++++++++ cdevents-sdk/examples/samples_ok/custom_domain.json | 13 +++++++++++++ cdevents-sdk/src/generated/mod.rs | 12 +++++++++--- cdevents-sdk/tests/specs.rs | 7 ++++++- generator/templates/mod.hbs | 12 +++++++++--- 5 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 cdevents-sdk/examples/samples_ok/custom_cdeventsx.json create mode 100644 cdevents-sdk/examples/samples_ok/custom_domain.json diff --git a/cdevents-sdk/examples/samples_ok/custom_cdeventsx.json b/cdevents-sdk/examples/samples_ok/custom_cdeventsx.json new file mode 100644 index 0000000..206eb9f --- /dev/null +++ b/cdevents-sdk/examples/samples_ok/custom_cdeventsx.json @@ -0,0 +1,13 @@ +{ + "context": { + "specversion": "0.5.1", + "id": "0", + "source": "https://app.cdviz.dev/probe", + "type": "dev.cdeventsx.probes.ping.0.1.0", + "timestamp": "2026-06-25T12:00:00Z" + }, + "subject": { + "id": "0", + "content": {} + } +} diff --git a/cdevents-sdk/examples/samples_ok/custom_domain.json b/cdevents-sdk/examples/samples_ok/custom_domain.json new file mode 100644 index 0000000..6888c6f --- /dev/null +++ b/cdevents-sdk/examples/samples_ok/custom_domain.json @@ -0,0 +1,13 @@ +{ + "context": { + "specversion": "0.5.1", + "id": "0", + "source": "https://app.cdviz.dev/probe", + "type": "com.example.probes.ping.0.1.0", + "timestamp": "2026-06-25T12:00:00Z" + }, + "subject": { + "id": "0", + "content": {} + } +} diff --git a/cdevents-sdk/src/generated/mod.rs b/cdevents-sdk/src/generated/mod.rs index 7389c62..9fa8bd7 100644 --- a/cdevents-sdk/src/generated/mod.rs +++ b/cdevents-sdk/src/generated/mod.rs @@ -650,6 +650,12 @@ pub enum Content { impl Content { pub fn from_json(ty: &str, json: serde_json::Value) -> Result{ + if ty.chars().filter(|&c| c == '.').count() != 6 { + return Err(serde_json::Error::custom(format_args!( + "invalid context.type format `{}`, expected '{{tld}}.{{domain}}.{{subject}}.{{predicate}}.{{version_major}}.{{version_minor}}.{{version_patch}}(-{{version_modifier}})?'", + ty, + ))) + } match ty { APPROVAL_CLOSED_0_1_0 => { let variant: approval_closed_0_1_0::Content = serde_json::from_value(json)?; @@ -1179,13 +1185,13 @@ impl Content { let variant: ticket_updated_0_1_0::Content = serde_json::from_value(json)?; Ok(variant.into()) }, - variant => if variant.starts_with("dev.cdeventsx.") { - Ok(Self::Custom{ ty: ty.to_string(), json }) - } else { + variant => if variant.starts_with("dev.cdevents.") { Err(serde_json::Error::custom(format_args!( "unknown variant `{}`, expected 'dev.cdevents.{{subject}}.{{predicate}}.{{version}}'", variant, ))) + } else { + Ok(Self::Custom{ ty: ty.to_string(), json }) }, } } diff --git a/cdevents-sdk/tests/specs.rs b/cdevents-sdk/tests/specs.rs index c5670b7..a144588 100644 --- a/cdevents-sdk/tests/specs.rs +++ b/cdevents-sdk/tests/specs.rs @@ -87,7 +87,12 @@ fn events_schemas() -> &'static EventsSchemas { } #[rstest] -fn can_serde_example(#[files("../cdevents-specs/spec-*/examples/*.json")] #[files("../cdevents-specs/spec-*/conformance/*.json")] #[files("../cdevents-specs/spec-*/custom/conformance.json")] path: PathBuf) { +fn can_serde_example( + #[files("../cdevents-specs/spec-*/examples/*.json")] + #[files("../cdevents-specs/spec-*/conformance/*.json")] + #[files("../cdevents-specs/spec-*/custom/conformance.json")] + #[files("examples/samples_ok/*.json")] + path: PathBuf) { let example_txt = fs::read_to_string(path).expect("to read file as string"); // HACK uri are stored ad http::Uri, they are "normalized" when serialized, so prenormalization to avoid failure like // json atoms at path ".subject.content.repository.source" are not equal: diff --git a/generator/templates/mod.hbs b/generator/templates/mod.hbs index 0704274..cb49b52 100644 --- a/generator/templates/mod.hbs +++ b/generator/templates/mod.hbs @@ -35,6 +35,12 @@ pub enum Content { impl Content { pub fn from_json(ty: &str, json: serde_json::Value) -> Result{ + if ty.chars().filter(|&c| c == '.').count() != 6 { + return Err(serde_json::Error::custom(format_args!( + "invalid context.type format `{}`, expected '\{{tld}}.\{{domain}}.\{{subject}}.\{{predicate}}.\{{version_major}}.\{{version_minor}}.\{{version_patch}}(-\{{version_modifier}})?'", + ty, + ))) + } match ty { {{#each variants }} {{to_screaming_snake_case this.rust_module}} => { @@ -42,13 +48,13 @@ impl Content { Ok(variant.into()) }, {{/each}} - variant => if variant.starts_with("dev.cdeventsx.") { - Ok(Self::Custom{ ty: ty.to_string(), json }) - } else { + variant => if variant.starts_with("dev.cdevents.") { Err(serde_json::Error::custom(format_args!( "unknown variant `{}`, expected 'dev.cdevents.\{{subject}}.\{{predicate}}.\{{version}}'", variant, ))) + } else { + Ok(Self::Custom{ ty: ty.to_string(), json }) }, } }