callumbwhyte / meganav

A flexible, draggable link picker for constructing site navigation menus in Umbraco
MIT License
35 stars 34 forks source link

NullReference exception in LegacyDataTypeChangingHandler.Handle #64

Closed fcrocetti closed 2 years ago

fcrocetti commented 2 years ago

The following code inside the second .Where predicate is giving a null reference exception when creating a new MegaNav data type because "existingEntity" is null when x.Id is equal to 0:

var dataTypes = notification.SavedEntities
                .Where(x => x.EditorAlias == Constants.PropertyEditorAlias)
                .Where(x =>
                {
                    var existingEntity = _dataTypeService.GetDataType(x.Id);

                    return existingEntity?.EditorAlias != Constants.PropertyEditorAlias && LegacyEditors.Aliases.Any(existingEntity.EditorAlias.InvariantContains);
                });

For now, to work around the issue I have added a null-check condition in the return predicate of LegacyEditors.Aliases.Any:

var dataTypes = notification.SavedEntities
                .Where(x => x.EditorAlias == Constants.PropertyEditorAlias)
                .Where(x =>
                {
                    var existingEntity = _dataTypeService.GetDataType(x.Id);

                    return existingEntity?.EditorAlias != Constants.PropertyEditorAlias && LegacyEditors.Aliases.Any((existingEntity?.EditorAlias ?? "").InvariantContains);
                });
dawoe commented 2 years ago

I can confirm this one.

callumbwhyte commented 2 years ago

Hey @fcrocetti and @dawoe,

Thanks for bringing this to my attention so quickly. Teaches me for only testing the release with the existing navs I had...

This is now fixed in v2.0.3 / v3.0.3 on NuGet, hopefully it didn't cause you too much inconvenience.

Cheers, Callum