Skip to content

Commit 15d5c76

Browse files
authored
Merge pull request #1156 from kaleido-io/webhooks-fix
Fix panic on webhooks without messages
2 parents 9ffba93 + d1c613c commit 15d5c76

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

internal/events/webhooks/webhooks.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,19 @@ func (wh *WebHooks) attemptRequest(sub *core.Subscription, event *core.EventDeli
211211

212212
if req.method == http.MethodPost || req.method == http.MethodPatch || req.method == http.MethodPut {
213213
switch {
214-
case !withData:
215-
// We are just sending the event itself
216-
req.r.SetBody(event)
217214
case req.body != nil:
218215
// We might have been told to extract a body from the first data record
219216
req.r.SetBody(req.body)
220217
case len(allData) > 1:
221218
// We've got an array of data to POST
222219
req.r.SetBody(allData)
223-
default:
224-
// Otherwise just send the first object directly
220+
case len(allData) == 1:
221+
// Just send the first object directly
225222
req.r.SetBody(firstData)
223+
default:
224+
// Just send the event itself
225+
req.r.SetBody(event)
226+
226227
}
227228
}
228229

@@ -292,7 +293,7 @@ func (wh *WebHooks) doDelivery(connID string, reply bool, sub *core.Subscription
292293
log.L(wh.ctx).Tracef("Webhook response: %s", string(b))
293294

294295
// Emit the response
295-
if reply {
296+
if reply && event.Message != nil {
296297
txType := fftypes.FFEnum(strings.ToLower(sub.Options.TransportOptions().GetString("replytx")))
297298
if req != nil && req.replyTx != "" {
298299
txType = fftypes.FFEnum(strings.ToLower(req.replyTx))
@@ -332,13 +333,8 @@ func (wh *WebHooks) doDelivery(connID string, reply bool, sub *core.Subscription
332333
}
333334

334335
func (wh *WebHooks) DeliveryRequest(connID string, sub *core.Subscription, event *core.EventDelivery, data core.DataArray) error {
335-
if event.Message == nil && sub.Options.WithData != nil && *sub.Options.WithData {
336-
log.L(wh.ctx).Debugf("Webhook withData=true subscription called with non-message event '%s'", event.ID)
337-
return nil
338-
}
339-
340336
reply := sub.Options.TransportOptions().GetBool("reply")
341-
if reply && event.Message.Header.CID != nil {
337+
if reply && event.Message != nil && event.Message.Header.CID != nil {
342338
// We cowardly refuse to dispatch a message that is itself a reply, as it's hard for users to
343339
// avoid loops - and there's no way for us to detect here if a user has configured correctly
344340
// to avoid a loop.

internal/events/webhooks/webhooks_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,8 @@ func TestWebhookFailFastAsk(t *testing.T) {
715715
func TestDeliveryRequestNilMessage(t *testing.T) {
716716
wh, cancel := newTestWebHooks(t)
717717
defer cancel()
718+
mcb := wh.callbacks["ns1"].(*eventsmocks.Callbacks)
719+
mcb.On("DeliveryResponse", mock.Anything, mock.Anything).Return("", &core.EventDelivery{})
718720

719721
yes := true
720722
sub := &core.Subscription{
@@ -741,6 +743,7 @@ func TestDeliveryRequestNilMessage(t *testing.T) {
741743

742744
err := wh.DeliveryRequest(mock.Anything, sub, event, nil)
743745
assert.NoError(t, err)
746+
mcb.AssertExpectations(t)
744747
}
745748

746749
func TestDeliveryRequestReplyToReply(t *testing.T) {

0 commit comments

Comments
 (0)