Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent AutoroutePartIndex from throwing exception when content definition is not found #12567

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class AutoroutePartIndex : MapIndex
public class AutoroutePartIndexProvider : ContentHandlerBase, IIndexProvider, IScopedIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<ContentItem> _itemRemoved = new HashSet<ContentItem>();
private readonly HashSet<string> _partRemoved = new HashSet<string>();
private readonly HashSet<ContentItem> _itemRemoved = new();
private readonly HashSet<string> _partRemoved = new();
private IContentDefinitionManager _contentDefinitionManager;
private IContentManager _contentManager;

Expand Down Expand Up @@ -92,7 +92,7 @@ public override async Task PublishedAsync(PublishContentContext context)

// Search for this part.
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentItem.ContentType);
if (!contentTypeDefinition.Parts.Any(ctpd => ctpd.Name == nameof(AutoroutePart)))
if (contentTypeDefinition != null && !contentTypeDefinition.Parts.Any(ctpd => ctpd.Name == nameof(AutoroutePart)))
Copy link
Contributor

@Skrypt Skrypt Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an issue because then the AutoroutePart record will stay in the database and thus will keep an active route/url for that content item while it should probably dispose of the AutoroutePart record of that Content Item. But at the same time if the Content Definition has been removed then the user should not be able to make any change to that content item because it should fail to retrieve its content definition and warn about it beforehand.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't recall exact steps that lead me to this case, maybe importing from migration. Regardless, I think we should be doing null check to prevent null reference exception.

{
context.ContentItem.Remove<AutoroutePart>();
_partRemoved.Add(context.ContentItem.ContentItemId);
Expand Down Expand Up @@ -125,7 +125,7 @@ public void Describe(DescribeContext<ContentItem> context)
var partRemoved = _partRemoved.Contains(contentItem.ContentItemId);

var part = contentItem.As<AutoroutePart>();
if (!partRemoved && (part == null || String.IsNullOrEmpty(part.Path)))
if (!partRemoved && String.IsNullOrEmpty(part?.Path))
{
return null;
}
Expand Down