Skip to content

Commit 6fc728c

Browse files
authored
Make the Delivery API "ancestors" selector work with preview (#17938) (#17950)
1 parent 45603e2 commit 6fc728c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/Umbraco.Cms.Api.Delivery/Querying/Selectors/AncestorsSelector.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,28 @@ public sealed class AncestorsSelector : QueryOptionBase, ISelectorHandler
1313
{
1414
private readonly IPublishedContentCache _publishedContentCache;
1515
private readonly IDocumentNavigationQueryService _navigationQueryService;
16+
private readonly IRequestPreviewService _requestPreviewService;
1617
private const string AncestorsSpecifier = "ancestors:";
1718

1819
public AncestorsSelector(
1920
IPublishedContentCache publishedContentCache,
2021
IRequestRoutingService requestRoutingService,
21-
IDocumentNavigationQueryService navigationQueryService)
22+
IDocumentNavigationQueryService navigationQueryService,
23+
IRequestPreviewService requestPreviewService)
2224
: base(publishedContentCache, requestRoutingService)
2325
{
2426
_publishedContentCache = publishedContentCache;
2527
_navigationQueryService = navigationQueryService;
28+
_requestPreviewService = requestPreviewService;
29+
}
30+
31+
[Obsolete("Use the constructor that takes all parameters. Scheduled for removal in V17.")]
32+
public AncestorsSelector(
33+
IPublishedContentCache publishedContentCache,
34+
IRequestRoutingService requestRoutingService,
35+
IDocumentNavigationQueryService navigationQueryService)
36+
: this(publishedContentCache, requestRoutingService, navigationQueryService, StaticServiceProvider.Instance.GetRequiredService<IRequestPreviewService>())
37+
{
2638
}
2739

2840
[Obsolete("Use the constructor that takes all parameters. Scheduled for removal in V17.")]
@@ -53,8 +65,17 @@ public SelectorOption BuildSelectorOption(string selector)
5365
};
5466
}
5567

56-
IPublishedContent contentItem = _publishedContentCache.GetById((Guid)id)
57-
?? throw new InvalidOperationException("Could not obtain the content cache");
68+
IPublishedContent? contentItem = _publishedContentCache.GetById(_requestPreviewService.IsPreview(), id.Value);
69+
70+
if (contentItem is null)
71+
{
72+
// no such content item, make sure the selector does not yield any results
73+
return new SelectorOption
74+
{
75+
FieldName = AncestorsSelectorIndexer.FieldName,
76+
Values = Array.Empty<string>()
77+
};
78+
}
5879

5980
var ancestorKeys = contentItem.Ancestors(_publishedContentCache, _navigationQueryService).Select(a => a.Key.ToString("D")).ToArray();
6081

0 commit comments

Comments
 (0)