Repository navigation
Description
Problem
The Proton AMQP binding reads a Data section with body.getValue().getArray(). A Qpid Binary can represent only a slice of its backing byte array, so the CloudEvent payload includes bytes outside that slice.
This affects both binary-mode events and structured messages. A zero-length slice backed by a nonempty array also becomes a nonempty event payload.
Reproduction
On current main (81d64ac49d6e4ecc8c2ff35b4b8e24dfa8ca8336), using cloudevents-amqp-proton and its Proton-J 0.34.1 dependency:
byte[] storage = "__payload__".getBytes(StandardCharsets.UTF_8);
ApplicationProperties props = new ApplicationProperties(Map.of(
"cloudEvents:specversion", "1.0",
"cloudEvents:id", "1",
"cloudEvents:source", "/test",
"cloudEvents:type", "test.event"
));
Section body = new Data(new Binary(storage, 2, 7));
CloudEvent event = ProtonAmqpMessageFactory
.createReader("text/plain", props, body)
.toEvent();
System.out.println(new String(event.getData().toBytes(), StandardCharsets.UTF_8));Expected: payload.
Actual: __payload__.
Binary's constructor and Binary.create(ByteBuffer) both support slices. Its getArray() returns the backing array; the valid region is given separately by getArrayOffset() and getLength().
Validation
Seven new regression cases exercise the public message factory: prefix and suffix padding, prefix only, suffix only, the full-array control, an empty slice of a nonempty array, the empty-array control, and a padded structured message. Against unchanged production code, four cases fail assertions and the structured case throws Invalid specversion: !!!1.0. The existing factory cases and both controls pass (25 tests total).
Proposed fix
Honor the Binary offset and length when extracting Data payload bytes. Preserve the existing full-array fast path and leave AmqpValue handling unchanged. I have prepared a focused fix with regression tests. This is independent of the header-prefix change in #757.
AI assistance: investigated and reproduced with Codex.
Activity