Closed idiotsky closed 7 months ago
Thanks for raising this @idiotsky
This is because of the backingStore which allows the setting and sending of null values in the request made to the API. If you do not need or should not set a value to null the property should ideally not be.
https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v5.md#backing-store
I have a method convert ContactEntity to Contact for save or update graph api contact.
here is the code snippet, for the assign business address code "contact.BusinessAddress = businessAddress", if don't have business address, it will set to null. if I can not set that to null, I think I have to add lots of ugly if else, like when its a new contact and business address is null, we don't set it.
Is it an good design, just set a null property to null, then stop working?
` public Contact ConvertContact(ContactEntity contactEntity, Contact contact = default)
{
contact ??= new Contact();
PhysicalAddress businessAddress = null;
foreach (PostAddress postAddress in contactEntity.PostAddresses)
{
if (postAddress.Label == "Business")
{
businessAddress = new PhysicalAddress { Street = postAddress.Street };
}
}
contact.BusinessAddress = businessAddress;
return contact;
}
`
Any chance the code below works out? This way you do not need to have if statements and can set only when needed instead of setting null properties.
public Contact ConvertContact(ContactEntity contactEntity, Contact contact = default)
{
contact ??= new Contact();
foreach (PostAddress postAddress in contactEntity.PostAddresses)
{
if (postAddress.Label == "Business")
{
contact.businessAddress = new PhysicalAddress { Street = postAddress.Street };
}
}
return contact;
}
Any chance the code below works out? This way you do not need to have if statements and can set only when needed instead of setting null properties.
public Contact ConvertContact(ContactEntity contactEntity, Contact contact = default) { contact ??= new Contact(); foreach (PostAddress postAddress in contactEntity.PostAddresses) { if (postAddress.Label == "Business") { contact.businessAddress = new PhysicalAddress { Street = postAddress.Street }; } } return contact; }
the use case is when the customer remove business address, I have to remove it, that's why I set to to null, as alterative now, I set it to new PhysicalAddress() instead of null. to skip this bug. but I still think set to null should be the right way deal with it.
var contact = new Contact { GivenName = "Talbott", Surname = "Cady", BusinessAddress = null }; await graphServiceClient.Me.Contacts.PostAsync(contact);
if we run the post contact with set either BusinessAddress,HomeAddress or OtherAddress to null, we get Microsoft.Graph.Models.ODataErrors.ODataError : An internal server error occurred. The operation failed.