kontent-ai / delivery-sdk-net

Kontent.ai Delivery .NET SDK

Home Page:https://www.nuget.org/packages/Kontent.Ai.Delivery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NullReference in DeliveryEndpointUrlBuilder

alanta opened this issue · comments

While investigating an other issue, I came across this bug:

https://github.com/Kentico/kontent-delivery-sdk-net/blob/aa802518a22f25ef6f31c608964788f5a12b33af/Kentico.Kontent.Urls/Delivery/DeliveryEndpointUrlBuilder.cs#L33

The code incorrectly assumes _deliveryOptionsMonitor is initialized which is not the case if the object was constructed using the second constructor.
I'd put up a PR for it but I'm not sure if the second constructor is worth having since it's not being referenced in the project at all.

Hello @alanta,

thanks for the report. You are right, that this constructor is not used in this SDK solution. I think proper way to fix this problem is to

  1. Remove unused constructor public DeliveryEndpointUrlBuilder(DeliveryOptions deliveryOptions), as internally the second one with options monitor is used. This removes one source of null options monitor issue.
  2. Update CurrentDeliveryOptions implementation (remove setter and also usage of related private variable deliveryOptions).
  3. Mark whole class DeliveryEndpointUrlBuilder as internal. The fact is, that it is internal code that is strongly tied to Kontent Delivery API routing that is out of standard SDK user control. Therefore I don't expect any customization/usage outside of SDK and there is no point in exposing it as public.
  4. DeliveryClient constructor initialization of variable from its parameter IOptionsMonitor<DeliveryOptions> deliveryOptions should be safeguarded by ArgumentNullException to allow detection of invalid options monitor as soon as possible.

We discussed this with @Simply007. I will fix this problem, but in a safer way. I will fix that potential null problem and deprecate public DeliveryEndpointUrlBuilder(DeliveryOptions deliveryOptions) constructor instead of mentioned points 1-3.