From d09a67b9c640fd58d479694f34ece6e1a869f2d8 Mon Sep 17 00:00:00 2001 From: Sonam <120879799+BiNKss7@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:37:15 -0600 Subject: [PATCH 1/3] fix(http): honor structured content type Honor the supplied event format before checking ce- headers, as required by the HTTP binding sections 3 and 3.2.3. Assisted-by: ChatGPT (Codex) Signed-off-by: Sonam <120879799+BiNKss7@users.noreply.github.com> --- src/cloudevents/core/bindings/http.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/cloudevents/core/bindings/http.py b/src/cloudevents/core/bindings/http.py index fbdcc3b0..c87c4ce3 100644 --- a/src/cloudevents/core/bindings/http.py +++ b/src/cloudevents/core/bindings/http.py @@ -271,7 +271,8 @@ def from_http( Auto-detects CloudEvents version and uses appropriate event factory if not provided. Automatically detects whether the message uses binary or structured content mode: - - If any ce- prefixed headers are present → binary mode + - If Content-Type matches the supplied event format → structured mode + - Otherwise, if any ce- prefixed headers are present → binary mode - Otherwise → structured mode This function provides a convenient way to handle both content modes without @@ -300,6 +301,22 @@ def from_http( :param event_factory: Factory function to create CloudEvent instances (auto-detected if None) :return: CloudEvent instance """ + # Structured messages may also carry ce- metadata headers (HTTP binding 3.2.3). + # Their Content-Type determines the mode; the body remains authoritative. + content_type = next( + ( + value + for key, value in message.headers.items() + if key.lower() == CONTENT_TYPE_HEADER + ), + "", + ) + if ( + content_type.partition(";")[0].strip().lower() + == event_format.get_content_type().partition(";")[0].strip().lower() + ): + return from_structured(message, event_format, event_factory) + if any(key.lower().startswith(CE_PREFIX) for key in message.headers.keys()): return from_binary(message, event_format, event_factory) From 98987f09f728fe3b3dca9de0bf5b6330ec854d9b Mon Sep 17 00:00:00 2001 From: Sonam <120879799+BiNKss7@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:38:04 -0600 Subject: [PATCH 2/3] test(http): cover structured metadata precedence Cover redundant metadata headers, Content-Type casing and parameters, explicit and inferred factories, partial metadata, and custom formats. All eight regression cases fail without the source fix. Assisted-by: ChatGPT (Codex) Signed-off-by: Sonam <120879799+BiNKss7@users.noreply.github.com> --- tests/test_core/test_bindings/test_http.py | 62 +++++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/tests/test_core/test_bindings/test_http.py b/tests/test_core/test_bindings/test_http.py index cf46ba4d..883895c3 100644 --- a/tests/test_core/test_bindings/test_http.py +++ b/tests/test_core/test_bindings/test_http.py @@ -710,23 +710,71 @@ def test_from_http_case_insensitive_detection() -> None: assert event.get_type() == "com.example.test" -def test_from_http_mixed_headers() -> None: - """Test from_http when both ce- headers and structured content are present""" +@pytest.mark.parametrize( + "content_type_header, content_type", + [ + ("content-type", "application/cloudevents+json"), + ("Content-Type", "application/cloudevents+json; charset=UTF-8"), + ("CONTENT-TYPE", "Application/CloudEvents+JSON"), + ], +) +@pytest.mark.parametrize("explicit_factory", [False, True]) +def test_from_http_mixed_headers( + content_type_header: str, content_type: str, explicit_factory: bool +) -> None: + """Structured Content-Type takes precedence over redundant metadata headers.""" message = HTTPMessage( headers={ "ce-type": "com.example.binary", "ce-source": "/binary", "ce-id": "123", "ce-specversion": "1.0", + content_type_header: content_type, + }, + body=b'{"type": "com.example.structured", "source": "/structured", "id": "456", "specversion": "1.0", "data": {"ok": true}}', + ) + event = from_http(message, JSONFormat(), CloudEvent if explicit_factory else None) + + assert event.get_type() == "com.example.structured" + assert event.get_source() == "/structured" + assert event.get_id() == "456" + assert event.get_data() == {"ok": True} + + +def test_from_http_structured_with_partial_metadata_headers() -> None: + """Optional metadata headers need not contain a complete binary event.""" + message = HTTPMessage( + headers={ "content-type": "application/cloudevents+json", + "ce-subject": "redundant-header", }, - body=b'{"type": "com.example.structured", "source": "/structured", "id": "456", "specversion": "1.0"}', + body=b'{"type": "com.example.test", "source": "/test", "id": "123", "specversion": "1.0"}', ) - event = from_http(message, JSONFormat(), CloudEvent) - # Binary mode should take precedence (ce- headers present) - assert event.get_type() == "com.example.binary" - assert event.get_source() == "/binary" + event = from_http_event(message) + + assert event.get_id() == "123" + assert "subject" not in event.get_attributes() + + +def test_from_http_structured_with_custom_format() -> None: + """Mode detection uses the supplied format's media type.""" + + class CustomJSONFormat(JSONFormat): + def get_content_type(self) -> str: + return "application/cloudevents+custom" + + original = create_event(data={"ok": True}) + structured = to_structured(original, CustomJSONFormat()) + message = HTTPMessage( + headers={**structured.headers, "ce-subject": "redundant-header"}, + body=structured.body, + ) + + event = from_http(message, CustomJSONFormat()) + + assert event.get_id() == original.get_id() + assert event.get_data() == {"ok": True} def test_percent_encoding_special_chars() -> None: From 9f1c70bdaea03d72316625f21abc09531bca3c0e Mon Sep 17 00:00:00 2001 From: Sonam <120879799+BiNKss7@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:39:03 -0600 Subject: [PATCH 3/3] docs: record structured HTTP parsing fix Document the corrected precedence of structured Content-Type over redundant metadata headers. Assisted-by: ChatGPT (Codex) Signed-off-by: Sonam <120879799+BiNKss7@users.noreply.github.com> --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa654e2a..e71ee7f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Honor structured HTTP Content-Type when redundant CloudEvents metadata headers + are present, using the event attributes and data from the message body. + ## [2.2.0] ### Changed