Skip to content

Commit e399b20

Browse files
authored
fix: process as Custom event any context.type not starting by "dev.cdevent." (#72)
Signed-off-by: David Bernard <david.bernard.31@gmail.com>
1 parent 5bf1ef3 commit e399b20

5 files changed

Lines changed: 50 additions & 7 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"context": {
3+
"specversion": "0.5.1",
4+
"id": "0",
5+
"source": "https://app.cdviz.dev/probe",
6+
"type": "dev.cdeventsx.probes.ping.0.1.0",
7+
"timestamp": "2026-06-25T12:00:00Z"
8+
},
9+
"subject": {
10+
"id": "0",
11+
"content": {}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"context": {
3+
"specversion": "0.5.1",
4+
"id": "0",
5+
"source": "https://app.cdviz.dev/probe",
6+
"type": "com.example.probes.ping.0.1.0",
7+
"timestamp": "2026-06-25T12:00:00Z"
8+
},
9+
"subject": {
10+
"id": "0",
11+
"content": {}
12+
}
13+
}

cdevents-sdk/src/generated/mod.rs

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cdevents-sdk/tests/specs.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ fn events_schemas() -> &'static EventsSchemas {
8787
}
8888

8989
#[rstest]
90-
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) {
90+
fn can_serde_example(
91+
#[files("../cdevents-specs/spec-*/examples/*.json")]
92+
#[files("../cdevents-specs/spec-*/conformance/*.json")]
93+
#[files("../cdevents-specs/spec-*/custom/conformance.json")]
94+
#[files("examples/samples_ok/*.json")]
95+
path: PathBuf) {
9196
let example_txt = fs::read_to_string(path).expect("to read file as string");
9297
// HACK uri are stored ad http::Uri, they are "normalized" when serialized, so prenormalization to avoid failure like
9398
// json atoms at path ".subject.content.repository.source" are not equal:

generator/templates/mod.hbs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,26 @@ pub enum Content {
3535

3636
impl Content {
3737
pub fn from_json(ty: &str, json: serde_json::Value) -> Result<Self, serde_json::Error>{
38+
if ty.chars().filter(|&c| c == '.').count() != 6 {
39+
return Err(serde_json::Error::custom(format_args!(
40+
"invalid context.type format `{}`, expected '\{{tld}}.\{{domain}}.\{{subject}}.\{{predicate}}.\{{version_major}}.\{{version_minor}}.\{{version_patch}}(-\{{version_modifier}})?'",
41+
ty,
42+
)))
43+
}
3844
match ty {
3945
{{#each variants }}
4046
{{to_screaming_snake_case this.rust_module}} => {
4147
let variant: {{this.rust_module}}::Content = serde_json::from_value(json)?;
4248
Ok(variant.into())
4349
},
4450
{{/each}}
45-
variant => if variant.starts_with("dev.cdeventsx.") {
46-
Ok(Self::Custom{ ty: ty.to_string(), json })
47-
} else {
51+
variant => if variant.starts_with("dev.cdevents.") {
4852
Err(serde_json::Error::custom(format_args!(
4953
"unknown variant `{}`, expected 'dev.cdevents.\{{subject}}.\{{predicate}}.\{{version}}'",
5054
variant,
5155
)))
56+
} else {
57+
Ok(Self::Custom{ ty: ty.to_string(), json })
5258
},
5359
}
5460
}

0 commit comments

Comments
 (0)