Skip to content

Commit

Permalink
fix: event parsing iterator
Browse files Browse the repository at this point in the history
The EventParsingIterator implementation does not parse correctly events when their payload do not have the eventpayload property set. The current behavior is that if said property "eventpayload" is not present then it fallback to extracting the value from the headers, but this is not a reliable method for parsing event streams since seems that not all services uses this pattern to provide the events. One example is the InvokeModelWithResponseStream operation from bedrock-runtime service. To fix this we check if the event key is set in the headers, and if so then we pick the value from the headers, otherwise we set the event key value based on the event payload.
  • Loading branch information
yenfryherrerafeliz committed Nov 17, 2023
1 parent 5b8a381 commit 34c30be
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/Api/Parser/EventParsingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ private function parseEvent(array $event)
if (empty($event['headers'][':event-type'])) {
throw new ParserException('Failed to parse without event type.');
}
$eventShape = $this->shape->getMember($event['headers'][':event-type']);

$eventShape = $this->shape->getMember($event['headers'][':event-type']);
$parsedEvent = [];
foreach ($eventShape['members'] as $shape => $details) {
if (!empty($details['eventpayload'])) {
$payloadShape = $eventShape->getMember($shape);
if ($payloadShape['type'] === 'blob') {
$parsedEvent[$shape] = $event['payload'];
} else {
if (isset($event['headers'][$shape])) {
$parsedEvent[$shape] = $event['headers'][$shape];
} else {
$memberShape = $eventShape->getMember($shape);
if ($memberShape instanceof StructureShape) {
$parsedEvent[$shape] = $this->parser->parseMemberFromStream(
$event['payload'],
$payloadShape,
$memberShape,
null
);
} else {
$parsedEvent[$shape] = $event['payload'];
}
} else {
$parsedEvent[$shape] = $event['headers'][$shape];
}
}

Expand All @@ -109,4 +109,4 @@ private function parseError(array $event)
$event['headers'][':error-message']
);
}
}
}

0 comments on commit 34c30be

Please sign in to comment.