From bac60b136b03dfd33cf66fb7db54788babad8e18 Mon Sep 17 00:00:00 2001 From: Marc Schier Date: Tue, 15 Sep 2026 14:52:09 +0200 Subject: [PATCH] fix(cloudevents): correct prose example shapes and identities Signed-off-by: Marc Schier --- cloudevents/spec.md | 103 ++++++------ tools/issue636/README.md | 27 ++++ .../issue636/test_cloudevents_examples_636.py | 147 ++++++++++++++++++ 3 files changed, 231 insertions(+), 46 deletions(-) create mode 100644 tools/issue636/README.md create mode 100644 tools/issue636/test_cloudevents_examples_636.py diff --git a/cloudevents/spec.md b/cloudevents/spec.md index 71068927..96c425b1 100644 --- a/cloudevents/spec.md +++ b/cloudevents/spec.md @@ -119,8 +119,8 @@ endpoint with a single, embedded message definition using an embedded Protobuf { "$schema": "https://cloudevents.io/schemas/registry", "specversion": "1.0-rc4", - "registryid": "Example Registry", - "self": "http://example.com", + "registryid": "example-registry", + "self": "https://example.com", "xid": "/", "epoch": 4, "createdat": "2024-04-30T12:00:00Z", @@ -137,17 +137,14 @@ endpoint with a single, embedded message definition using an embedded Protobuf "createdat": "2024-04-30T12:00:00Z", "modifiedat": "2024-04-31T12:00:00Z", - "usage": "consumer", - "format": "CloudEvents/1.0", - - "config": { - "protocol": "MQTT/5.0", + "usage": [ "consumer" ], + "envelope": "CloudEvents/1.0", + "protocol": "MQTT/5.0", + "protocoloptions": { "endpoints": [ { "uri": "mqtt://mqtt.example.com:1883" } ], - "options": { - "topic": "{deviceid}/telemetry" - } + "topic": "{deviceid}/telemetry" }, "messagesurl": "https://example.com/endpoints/com.example.telemetry/messages", @@ -165,8 +162,8 @@ endpoint with a single, embedded message definition using an embedded Protobuf "modifiedat": "2024-04-31T12:00:00Z", "ancestorid": "1.0", - "format": "CloudEvents/1.0", - "metadata": { + "envelope": "CloudEvents/1.0", + "envelopemetadata": { "id": { "type": "string", "required": true @@ -187,7 +184,7 @@ endpoint with a single, embedded message definition using an embedded Protobuf } }, - "dataschemaformat": "Protobuf/3.0", + "dataschemaformat": "Protobuf/3", "dataschema": "syntax = \"proto3\"; message Metrics { float metric = 1; }", "datacontenttype": "application/x-protobuf", @@ -210,8 +207,8 @@ other scenarios: { "$schema": "https://cloudevents.io/schemas/registry", "specversion": "1.0-rc4", - "registryid": "Example Registry", - "self": "http://example.com", + "registryid": "example-registry", + "self": "https://example.com", "xid": "/", "epoch": 4, "createdat": "2024-04-30T12:00:00Z", @@ -228,17 +225,14 @@ other scenarios: "createdat": "2024-04-30T12:00:00Z", "modifiedat": "2024-04-31T12:00:00Z", - "usage": "consumer", - "format": "CloudEvents/1.0", - - "config": { - "protocol": "MQTT/5.0", + "usage": [ "consumer" ], + "envelope": "CloudEvents/1.0", + "protocol": "MQTT/5.0", + "protocoloptions": { "endpoints": [ - { "uri": "mqtt://mqtt.example.com:1883" } + { "uri": "mqtt://mqtt.example.com:1883" } ], - "options": { - "topic": "{deviceid}/telemetry" - } + "topic": "{deviceid}/telemetry" }, "messagegroups": [ "#/messagegroups/com.example.telemetryEvents" ], @@ -265,8 +259,8 @@ other scenarios: "com.example.telemetry": { "messageid": "com.example.telemetry", "versionid": "1.0", - "self": "https://example.com/endpoints/com.example.telemetry/messages/com.example.telemetry", - "xid": "/endpoints/com.example.telemetry/messages/com.example.telemetry", + "self": "https://example.com/messagegroups/com.example.telemetryEvents/messages/com.example.telemetry", + "xid": "/messagegroups/com.example.telemetryEvents/messages/com.example.telemetry", "epoch": 5, "isdefault": true, "description": "device telemetry event", @@ -274,8 +268,8 @@ other scenarios: "modifiedat": "2024-04-31T12:00:00Z", "ancestorid": "1.0", - "format": "CloudEvents/1.0", - "metadata": { + "envelope": "CloudEvents/1.0", + "envelopemetadata": { "id": { "type": "string", "required": true @@ -296,13 +290,13 @@ other scenarios: } }, - "dataschemaformat": "Protobuf/3.0", - "dataschemauri": "#/schemagroups/com.example.telemetry/schemas/com.example.telemetrydata/versions/1.0", + "dataschemaformat": "Protobuf/3", + "dataschemauri": "#/schemagroups/com.example.telemetry/schemas/com.example.telemetrydata/versions/1.0:Metrics", "datacontenttype": "application/x-protobuf", - "metaurl": "https://example.com/endpoints/com.example.telemetry/messages/com.example.telemetry/meta", + "metaurl": "https://example.com/messagegroups/com.example.telemetryEvents/messages/com.example.telemetry/meta", - "versionsurl": "https://example.com/endpoints/com.example.telemetry/messages/com.example.telemetry/versions", + "versionsurl": "https://example.com/messagegroups/com.example.telemetryEvents/messages/com.example.telemetry/versions", "versionscount": 1 } } @@ -335,13 +329,22 @@ other scenarios: "modifiedat": "2024-04-31T12:00:00Z", "ancestorid": "1.0", - "format": "Protobuf/3.0", + "format": "Protobuf/3", "schema": "syntax = \"proto3\"; message Metrics { float metric = 1;}", "metaurl": "https://example.com/schemagroups/com.example.telemetry/schemas/com.example.telemetrydata/meta", "versionscount": 1, - "versionsurl": "https://example.com/schemagroups/com.example.telemetry/schemas/com.example.telemetrydata/versions" + "versionsurl": "https://example.com/schemagroups/com.example.telemetry/schemas/com.example.telemetrydata/versions", + "versions": { + "1.0": { + "versionid": "1.0", + "self": "https://example.com/schemagroups/com.example.telemetry/schemas/com.example.telemetrydata/versions/1.0", + "xid": "/schemagroups/com.example.telemetry/schemas/com.example.telemetrydata/versions/1.0", + "format": "Protobuf/3", + "schema": "syntax = \"proto3\"; message Metrics { float metric = 1;}" + } + } } } } @@ -349,15 +352,23 @@ other scenarios: } ``` +The shared layout includes the selected Schema Version so that its local +reference can be followed within the document. The `:Metrics` suffix selects +the Protobuf message declaration using the existing +[Protobuf reference rule](../schema/spec.md#434-protobuf-schema). +Other server-managed attributes of that Version are omitted. + If we assume the message definitions and schemas to reside at an API endpoint, an endpoint definition might just reference the associated message definition -group with a deep link to the respective object in the service: +group with a deep link to the respective object in the service. This +illustration abbreviates the protocol options using the comment notation +defined above: ```json { "$schema": "https://cloudevents.io/schemas/registry", "specversion": "1.0-rc4", - "registryid": "Example Registry", + "registryid": "example-registry", "endpointsurl": "https://example.com/endpoints", "endpointscount": 1, @@ -370,10 +381,10 @@ group with a deep link to the respective object in the service: "createdat": "2024-04-30T12:00:00Z", "modifiedat": "2024-04-31T12:00:00Z", - "usage": "consumer", - "format": "CloudEvents/1.0", - - "config": { + "usage": [ "consumer" ], + "envelope": "CloudEvents/1.0", + "protocol": "MQTT/5.0", + "protocoloptions": { # ... details ... }, @@ -386,13 +397,13 @@ group with a deep link to the respective object in the service: If the message definitions and schemas are stored in a file-based registry, including files shared via public version control repositories, the reference link will first reference the file and then the object within the file, using -[JSON Pointer][JSON Pointer] syntax: +[JSON Pointer][JSON Pointer] syntax. Protocol options are again abbreviated: ```yaml { "$schema": "https://cloudevents.io/schemas/registry", "specversion": "1.0-rc4", - "registryid": "Example Registry", + "registryid": "example-registry", "endpointsurl": "https://example.com/endpoints", "endpointscount": 1, @@ -405,10 +416,10 @@ link will first reference the file and then the object within the file, using "createdat": "2024-04-30T12:00:00Z", "modifiedat": "2024-04-31T12:00:00Z", - "usage": "consumer", - "format": "CloudEvents/1.0", - - "config": { + "usage": [ "consumer" ], + "envelope": "CloudEvents/1.0", + "protocol": "MQTT/5.0", + "protocoloptions": { # ... details ... }, diff --git a/tools/issue636/README.md b/tools/issue636/README.md new file mode 100644 index 00000000..c49475b8 --- /dev/null +++ b/tools/issue636/README.md @@ -0,0 +1,27 @@ +# CloudEvents prose layout checks + + + +`test_cloudevents_examples_636.py` reads the actual four concrete/projection +examples in the [CloudEvents specification](../../cloudevents/spec.md). +The first two are strictly parsed as complete JSON with duplicate-key +rejection. The two explicitly abbreviated remote-reference illustrations are +parsed as YAML because they retain the document's declared comment notation. +The final placeholder-only format sketch is not treated as a Registry. + +```shell +python -B -m pytest tools/issue636 -q -p no:cacheprovider +``` + +The checks inspect native domain fields, scalar/array kinds, map-key/ID +agreement, parent-derived self/XID/Meta/Versions navigation, and compact versus +shared layouts. The existing Protobuf selector rule is exercised with +`jsonpointer` against the shown local Schema Version, followed by a check for +the selected declaration in that example's simple schema literal. This is not +a general Protobuf parser or a new selector grammar. + +Timestamps are intentionally opaque here; the separate date correction is +issue 638. These tests do not validate all Core/model constraints, schema +identifiers, or unresolved domain-model defects, and do not fetch remote +references. They are abstract/source evidence, not real server/SDK +interoperability. Scenario samples covered by earlier work are not changed. diff --git a/tools/issue636/test_cloudevents_examples_636.py b/tools/issue636/test_cloudevents_examples_636.py new file mode 100644 index 00000000..b91dd2e6 --- /dev/null +++ b/tools/issue636/test_cloudevents_examples_636.py @@ -0,0 +1,147 @@ +import json +from pathlib import Path +import re +from urllib.parse import urlsplit + +import jsonpointer +import pytest +import yaml + + +ROOT = Path(__file__).resolve().parents[2] +SPEC = ROOT / "cloudevents" / "spec.md" +_ID = re.compile(r"[A-Za-z0-9_][A-Za-z0-9_.~:@-]{0,127}") + + +def _unique_object(pairs): + result = {} + for key, value in pairs: + assert key not in result, f"duplicate JSON key: {key}" + result[key] = value + return result + + +@pytest.fixture +def examples(): + blocks = re.findall( + r"```(?:json|yaml)\n(.*?)\n```", SPEC.read_text(encoding="utf-8"), re.S + ) + assert len(blocks) == 5 + return [ + json.loads(block, object_pairs_hook=_unique_object) if index < 2 + else yaml.safe_load(block) + for index, block in enumerate(blocks[:4]) + ] + + +def _entities(registry): + yield "/", "registryid", registry["registryid"], registry + for groups, group_id, resources, resource_id in ( + ("endpoints", "endpointid", "messages", "messageid"), + ("messagegroups", "messagegroupid", "messages", "messageid"), + ("schemagroups", "schemagroupid", "schemas", "schemaid"), + ): + for key, group in registry.get(groups, {}).items(): + group_xid = f"/{groups}/{key}" + yield group_xid, group_id, key, group + for key, resource in group.get(resources, {}).items(): + resource_xid = f"{group_xid}/{resources}/{key}" + yield resource_xid, resource_id, key, resource + for key, version in resource.get("versions", {}).items(): + yield f"{resource_xid}/versions/{key}", "versionid", key, version + + +@pytest.mark.parametrize("index", range(4)) +def test_current_endpoint_shapes_in_all_four_layout_examples(examples, index): + endpoint = examples[index]["endpoints"]["com.example.telemetry"] + assert endpoint["usage"] == ["consumer"] + assert endpoint["envelope"] == "CloudEvents/1.0" + assert endpoint["protocol"] == "MQTT/5.0" + assert isinstance(endpoint["protocoloptions"], dict) + assert not {"format", "config", "options", "metadata"} & endpoint.keys() + if index < 2: + assert endpoint["protocoloptions"] == { + "endpoints": [{"uri": "mqtt://mqtt.example.com:1883"}], + "topic": "{deviceid}/telemetry", + } + + +@pytest.mark.parametrize("index", range(4)) +def test_actual_parent_ids_self_xid_and_navigation_are_consistent(examples, index): + registry = examples[index] + origin = registry.get("self", "https://example.com").rstrip("/") + count = 0 + for xid, id_field, key, entity in _entities(registry): + count += 1 + assert _ID.fullmatch(key), (xid, key) + assert entity[id_field] == key + if "xid" in entity: + assert entity["xid"] == xid + if "self" in entity: + assert entity["self"].rstrip("/") == origin + (xid if xid != "/" else "") + if "metaurl" in entity: + assert entity["metaurl"] == origin + xid + "/meta" + for collection in ("endpoints", "messagegroups", "schemagroups", "messages", "schemas", "versions"): + if collection + "url" in entity: + expected = origin + xid.rstrip("/") + "/" + collection + assert entity[collection + "url"] == expected + if isinstance(entity.get(collection), dict) and collection + "count" in entity: + assert entity[collection + "count"] == len(entity[collection]) + assert count >= 2 + + +def test_compact_and_shared_messages_use_flat_native_declarations(examples): + compact = examples[0]["endpoints"]["com.example.telemetry"]["messages"]["com.example.telemetry"] + shared = examples[1]["messagegroups"]["com.example.telemetryEvents"]["messages"]["com.example.telemetry"] + for message in (compact, shared): + assert message["envelope"] == "CloudEvents/1.0" + assert not {"format", "config", "options", "metadata"} & message.keys() + metadata = message["envelopemetadata"] + assert metadata["type"]["value"] == message["messageid"] + assert metadata["source"]["type"] == "uritemplate" + assert metadata["source"]["value"] == "{deploymentid}/{deviceid}" + assert metadata["time"]["required"] is True + assert "attributes" not in metadata + assert message["dataschemaformat"] == "Protobuf/3" + + +def test_shared_reference_selects_the_actual_inline_protobuf_version_and_message(examples): + registry = examples[1] + message = registry["messagegroups"]["com.example.telemetryEvents"]["messages"]["com.example.telemetry"] + reference = message["dataschemauri"] + assert reference.startswith("#/") + pointer, separator, name = reference[1:].rpartition(":") + assert separator == ":" and name == "Metrics" + version = jsonpointer.resolve_pointer(registry, pointer) + assert version["versionid"] == "1.0" + assert version["xid"] == pointer + assert version["format"] == "Protobuf/3" + declaration = re.search( + r"\bmessage\s+" + re.escape(name) + r"\s*\{\s*float\s+metric\s*=\s*1\s*;\s*\}", + version["schema"], + ) + assert declaration, "selector must name a declaration in the shown schema" + schema = registry["schemagroups"]["com.example.telemetry"]["schemas"]["com.example.telemetrydata"] + assert version["schema"] == schema["schema"] + compact = examples[0]["endpoints"]["com.example.telemetry"]["messages"]["com.example.telemetry"] + assert re.sub(r"\s+", "", compact["dataschema"]) == re.sub(r"\s+", "", version["schema"]) + + +def test_compact_shared_api_and_file_reference_layouts_remain_distinct(examples): + compact, shared, api, file = [ + registry["endpoints"]["com.example.telemetry"] for registry in examples + ] + assert compact["messagescount"] == len(compact["messages"]) == 1 + assert "messagegroups" not in compact + assert shared["messagescount"] == 0 + assert "messages" not in shared + group_ref = shared["messagegroups"][0] + assert group_ref.startswith("#/") + group = jsonpointer.resolve_pointer(examples[1], group_ref[1:]) + assert group["messagegroupid"] == "com.example.telemetryEvents" + api_ref = urlsplit(api["messagegroups"][0]) + assert api_ref.scheme == "https" and not api_ref.fragment + assert api_ref.path == group["xid"] + file_ref = urlsplit(file["messagegroups"][0]) + assert file_ref.scheme == "https" and file_ref.path.endswith(".cereg") + assert file_ref.fragment == group["xid"]