Delivery API: Handle more unhappy paths when querying (#14245)
* Handle more unhappy paths * Review comments --------- Co-authored-by: kjac <kja@umbraco.dk>
This commit is contained in:
committed by
GitHub
parent
b32b8c2265
commit
e572dcfa2d
@@ -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<string>(),
|
||||
Operator = alias.StartsWith('!')
|
||||
? FilterOperation.IsNot
|
||||
: FilterOperation.Is
|
||||
|
||||
@@ -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<string>(),
|
||||
Operator = value.StartsWith('!')
|
||||
? FilterOperation.IsNot
|
||||
: FilterOperation.Is
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -22,6 +22,11 @@ internal sealed class RequestRoutingService : RoutingServiceBase, IRequestRoutin
|
||||
/// <inheritdoc />
|
||||
public string GetContentRoute(string requestedPath)
|
||||
{
|
||||
if (requestedPath.IsNullOrWhiteSpace())
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
requestedPath = requestedPath.EnsureStartsWith("/");
|
||||
|
||||
// do we have an explicit start item?
|
||||
|
||||
Reference in New Issue
Block a user