Bug in Rest API Patch Webhook  Topic is solved

Discussions relating to the REST API of Jiwa 7.

Bug in Rest API Patch Webhook

Postby SBarnes » Wed Mar 03, 2021 11:02 am

In terms of the webhook patch I have discovered two things that are an issue, the first is that if you don't set the patch body as Json in postman it doesn't make it through the serialisation process and you get null reference exceptions, this I can understand as it probably means postman is not sending the appropriate content headers without this setting.

The second issue is definitely a bug as I have checked the code and that is a patch won't do anything with the headers in the payload in other words this bit from the post or the equivalent for the patch is missing

Code: Select all
 // Now do the headers (if any)
            if (request.Headers != null)
            {
                foreach (WebhooksSubscriptionHeader webhooksSubscriptionHeader in request.Headers)
                {
                    string webhooksSubscriptionHeaderRecID = System.Guid.NewGuid().ToString();
                    Db.ExecuteSql(@"INSERT INTO SY_WebhookSubscriptionRequestHeader (RecID, SY_WebhookSubscription_RecID, Name, Value, ItemNo, LastSavedDateTime)
                                 VALUES (@RecID, @SY_WebhookSubscription_RecID, @Name, @Value, (SELECT COALESCE(MAX(ItemNo), 0) + 1 FROM SY_WebhookSubscriptionRequestHeader WHERE SY_WebhookSubscriptionRequestHeader.SY_WebhookSubscription_RecID = @ItemNo_SY_WebhookSubscription_RecID), SYSUTCDATETIME())",
                              new
                              {
                                  RecID = webhooksSubscriptionHeaderRecID,
                                  SY_WebhookSubscription_RecID = newSubscription.RecID,
                                  Name = webhooksSubscriptionHeader.Name,
                                  Value = webhooksSubscriptionHeader.Value,
                                  ItemNo_SY_WebhookSubscription_RecID = newSubscription.RecID
                              });
                    SY_WebhookSubscriptionRequestHeader newSubscriptionRequestHeader = Db.SingleById<SY_WebhookSubscriptionRequestHeader>(webhooksSubscriptionHeaderRecID);
                    RESTAPIPlugin.WebhookSubscriptionRequestHeaders.Add(newSubscriptionRequestHeader);
                }
            }
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Bug in Rest API Patch Webhook

Postby SBarnes » Wed Mar 03, 2021 11:46 am

I think this should fix it, but I confirmation would be helpful, also I noticed there is actually no route to confirm what headers are there.

Code: Select all

        public SY_WebhookSubscription Patch(WebhooksSubscriptionsPATCHRequest request)
        {
         //System.Diagnostics.Debugger.Launch();
            if (RESTAPIPlugin.WebhookSubscribers.Where(x => x.RecID.ToString().ToLower() == request.SubscriberID.ToLower()).FirstOrDefault() == null)
                throw new JiwaFinancials.Jiwa.JiwaApplication.Exceptions.RecordNotFoundException(String.Format("No subscriber with ID '{0}'", request.SubscriberID.ToLower()));

            SY_WebhookSubscription subscription = RESTAPIPlugin.WebhookSubscriptions.Where(x => x.RecID.ToString().ToLower() == request.SubscriptionID.ToLower()).FirstOrDefault();

            if (subscription == null)
                throw new JiwaFinancials.Jiwa.JiwaApplication.Exceptions.RecordNotFoundException(String.Format("No subscription with ID '{0}'", request.SubscriptionID.ToLower()));

            Db.Update<SY_WebhookSubscription>(new { URL = request.URL, EventName = request.EventName }, x => x.RecID.ToString().ToLower() == request.SubscriptionID.ToLower());
            // read back and return that as our item no and lastsaved date time is set at the database level and we want to return that.
            SY_WebhookSubscription updatedSubscription = Db.SingleById<SY_WebhookSubscription>(subscription.RecID);

            // Update the subscription in the internal collection
            SY_WebhookSubscription internalSubscription = RESTAPIPlugin.WebhookSubscriptions.FirstOrDefault(x => x.RecID.ToString().ToLower() == request.SubscriptionID.ToLower());
            internalSubscription.URL = updatedSubscription.URL;
            internalSubscription.EventName = updatedSubscription.EventName;
         
         
         // Now do the headers (if any)
            if (request.Headers != null)
            {
            //fix the internal headers
            List<SY_WebhookSubscriptionRequestHeader> removals = RESTAPIPlugin.WebhookSubscriptionRequestHeaders.Where(h => h.SY_WebhookSubscription_RecID.ToString().ToLower() == request.SubscriptionID.ToLower()).ToList();
            foreach(SY_WebhookSubscriptionRequestHeader header in removals )
            {
               RESTAPIPlugin.WebhookSubscriptionRequestHeaders.Remove(header);
            }
            
            
            //fix the database
            Db.ExecuteSql(@"delete from  SY_WebhookSubscriptionRequestHeader where SY_WebhookSubscription_RecID = @SY_WebhookSubscription_RecID",
                              new
                              {
                                  SY_WebhookSubscription_RecID = request.SubscriptionID
                                 
                              });
            
                foreach (WebhooksSubscriptionHeader webhooksSubscriptionHeader in request.Headers)
                {
                    string webhooksSubscriptionHeaderRecID = System.Guid.NewGuid().ToString();
                    Db.ExecuteSql(@"INSERT INTO SY_WebhookSubscriptionRequestHeader (RecID, SY_WebhookSubscription_RecID, Name, Value, ItemNo, LastSavedDateTime)
                                 VALUES (@RecID, @SY_WebhookSubscription_RecID, @Name, @Value, (SELECT COALESCE(MAX(ItemNo), 0) + 1 FROM SY_WebhookSubscriptionRequestHeader WHERE SY_WebhookSubscriptionRequestHeader.SY_WebhookSubscription_RecID = @ItemNo_SY_WebhookSubscription_RecID), SYSUTCDATETIME())",
                              new
                              {
                                  RecID = webhooksSubscriptionHeaderRecID,
                                  SY_WebhookSubscription_RecID = internalSubscription.RecID,
                                  Name = webhooksSubscriptionHeader.Name,
                                  Value = webhooksSubscriptionHeader.Value,
                                  ItemNo_SY_WebhookSubscription_RecID = internalSubscription.RecID
                              });
                    SY_WebhookSubscriptionRequestHeader newSubscriptionRequestHeader = Db.SingleById<SY_WebhookSubscriptionRequestHeader>(webhooksSubscriptionHeaderRecID);
                    RESTAPIPlugin.WebhookSubscriptionRequestHeaders.Add(newSubscriptionRequestHeader);
                }
            }
         
         
         
         
         
         
         
            return updatedSubscription;
        }
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Bug in Rest API Patch Webhook  Topic is solved

Postby Mike.Sheen » Thu Mar 04, 2021 2:44 pm

Hi Stuart,

Yep - we completely ignore the headers in the body when PATCHing /Webhooks/Subscribers/{SubscriberID}/Subscriptions

Added bug DEV-8625 to address this!
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Bug in Rest API Patch Webhook

Postby SBarnes » Thu Mar 04, 2021 2:57 pm

The patch above works but as mentioned you can't see what headers there are through the api, which is ok if can execute SQL against the database but not if you are an external programmer without access.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175


Return to REST API

Who is online

Users browsing this forum: No registered users and 2 guests

cron