microsoftgraph / msgraph-sdk-dotnet

Microsoft Graph Client Library for .NET!
https://graph.microsoft.com
Other
699 stars 248 forks source link

Create Contact failed with set BusinessAddress,HomeAddress or OtherAddress to null #2368

Closed idiotsky closed 7 months ago

idiotsky commented 8 months ago

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.

andrueastman commented 8 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

idiotsky commented 7 months ago

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;
    }

`

andrueastman commented 7 months ago

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;
        }
idiotsky commented 7 months ago

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.