diff --git a/src/Umbraco.Cms.Api.Delivery/Querying/Filters/ContentTypeFilter.cs b/src/Umbraco.Cms.Api.Delivery/Querying/Filters/ContentTypeFilter.cs index e851158b87..13d9dc77ec 100644 --- a/src/Umbraco.Cms.Api.Delivery/Querying/Filters/ContentTypeFilter.cs +++ b/src/Umbraco.Cms.Api.Delivery/Querying/Filters/ContentTypeFilter.cs @@ -1,5 +1,6 @@ using Umbraco.Cms.Api.Delivery.Indexing.Filters; using Umbraco.Cms.Core.DeliveryApi; +using Umbraco.Extensions; namespace Umbraco.Cms.Api.Delivery.Querying.Filters; @@ -19,7 +20,9 @@ public sealed class ContentTypeFilter : IFilterHandler return new FilterOption { FieldName = ContentTypeFilterIndexer.FieldName, - Values = new[] { alias.TrimStart('!') }, + Values = alias.IsNullOrWhiteSpace() == false + ? new[] { alias.TrimStart('!') } + : Array.Empty(), Operator = alias.StartsWith('!') ? FilterOperation.IsNot : FilterOperation.Is diff --git a/src/Umbraco.Cms.Api.Delivery/Querying/Filters/NameFilter.cs b/src/Umbraco.Cms.Api.Delivery/Querying/Filters/NameFilter.cs index 64aa5b2776..0bf3d5e460 100644 --- a/src/Umbraco.Cms.Api.Delivery/Querying/Filters/NameFilter.cs +++ b/src/Umbraco.Cms.Api.Delivery/Querying/Filters/NameFilter.cs @@ -1,5 +1,6 @@ using Umbraco.Cms.Api.Delivery.Indexing.Sorts; using Umbraco.Cms.Core.DeliveryApi; +using Umbraco.Extensions; namespace Umbraco.Cms.Api.Delivery.Querying.Filters; @@ -19,7 +20,9 @@ public sealed class NameFilter : IFilterHandler return new FilterOption { FieldName = NameSortIndexer.FieldName, - Values = new[] { value.TrimStart('!') }, + Values = value.IsNullOrWhiteSpace() == false + ? new[] { value.TrimStart('!') } + : Array.Empty(), Operator = value.StartsWith('!') ? FilterOperation.IsNot : FilterOperation.Is diff --git a/src/Umbraco.Cms.Api.Delivery/Querying/QueryOptionBase.cs b/src/Umbraco.Cms.Api.Delivery/Querying/QueryOptionBase.cs index 8934889715..f29e0465f5 100644 --- a/src/Umbraco.Cms.Api.Delivery/Querying/QueryOptionBase.cs +++ b/src/Umbraco.Cms.Api.Delivery/Querying/QueryOptionBase.cs @@ -1,6 +1,7 @@ using Umbraco.Cms.Core.DeliveryApi; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PublishedCache; +using Umbraco.Extensions; namespace Umbraco.Cms.Api.Delivery.Querying; @@ -16,22 +17,23 @@ public abstract class QueryOptionBase _requestRoutingService = requestRoutingService; } - public Guid? GetGuidFromQuery(string queryStringValue) + protected Guid? GetGuidFromQuery(string queryStringValue) { + if (queryStringValue.IsNullOrWhiteSpace()) + { + return null; + } + if (Guid.TryParse(queryStringValue, out Guid id)) { return id; } - if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out IPublishedSnapshot? publishedSnapshot) || - publishedSnapshot?.Content is null) - { - return null; - } + IPublishedSnapshot publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); // Check if the passed value is a path of a content item var contentRoute = _requestRoutingService.GetContentRoute(queryStringValue); - IPublishedContent? contentItem = publishedSnapshot.Content.GetByRoute(contentRoute); + IPublishedContent? contentItem = publishedSnapshot.Content?.GetByRoute(contentRoute); return contentItem?.Key; } diff --git a/src/Umbraco.Cms.Api.Delivery/Querying/Selectors/AncestorsSelector.cs b/src/Umbraco.Cms.Api.Delivery/Querying/Selectors/AncestorsSelector.cs index 67490e38f4..5e8e7e2019 100644 --- a/src/Umbraco.Cms.Api.Delivery/Querying/Selectors/AncestorsSelector.cs +++ b/src/Umbraco.Cms.Api.Delivery/Querying/Selectors/AncestorsSelector.cs @@ -25,9 +25,7 @@ public sealed class AncestorsSelector : QueryOptionBase, ISelectorHandler var fieldValue = selector[AncestorsSpecifier.Length..]; Guid? id = GetGuidFromQuery(fieldValue); - if (id is null || - !_publishedSnapshotAccessor.TryGetPublishedSnapshot(out IPublishedSnapshot? publishedSnapshot) || - publishedSnapshot?.Content is null) + if (id is null) { // Setting the Value to "" since that would yield no results. // It won't be appropriate to return null here since if we reached this, @@ -39,8 +37,11 @@ public sealed class AncestorsSelector : QueryOptionBase, ISelectorHandler }; } - // With the previous check we made sure that if we reach this, we already made sure that there is a valid content item - IPublishedContent contentItem = publishedSnapshot.Content.GetById((Guid)id)!; // so it can't be null + IPublishedSnapshot publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); + + IPublishedContent contentItem = publishedSnapshot.Content?.GetById((Guid)id) + ?? throw new InvalidOperationException("Could not obtain the content cache"); + var ancestorKeys = contentItem.Ancestors().Select(a => a.Key.ToString("D")).ToArray(); return new SelectorOption diff --git a/src/Umbraco.Cms.Api.Delivery/Services/RequestRoutingService.cs b/src/Umbraco.Cms.Api.Delivery/Services/RequestRoutingService.cs index 993780680d..6bf0dbc887 100644 --- a/src/Umbraco.Cms.Api.Delivery/Services/RequestRoutingService.cs +++ b/src/Umbraco.Cms.Api.Delivery/Services/RequestRoutingService.cs @@ -22,6 +22,11 @@ internal sealed class RequestRoutingService : RoutingServiceBase, IRequestRoutin /// public string GetContentRoute(string requestedPath) { + if (requestedPath.IsNullOrWhiteSpace()) + { + return string.Empty; + } + requestedPath = requestedPath.EnsureStartsWith("/"); // do we have an explicit start item?