Skip to content

Commit 225fed5

Browse files
authored
Handle user id 0 (Unknown/System) when building content version response (#19361)
Handle user id 0 (Unknown/System) when building content version response model `IUserIdKeyResolver.GetAsync` throws an exception when a user is not found. As user 0 does not really exist, the exception was being thrown. We now handle this scenario to return an empty reference.
1 parent 419625a commit 225fed5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/Umbraco.Cms.Api.Management/Factories/DocumentVersionPresentationFactory.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Umbraco.Cms.Api.Management.ViewModels;
22
using Umbraco.Cms.Api.Management.ViewModels.Document;
3+
using Umbraco.Cms.Core;
34
using Umbraco.Cms.Core.Models;
45
using Umbraco.Cms.Core.Services;
56
using Umbraco.Extensions;
@@ -19,17 +20,26 @@ public DocumentVersionPresentationFactory(
1920
_userIdKeyResolver = userIdKeyResolver;
2021
}
2122

22-
public async Task<DocumentVersionItemResponseModel> CreateAsync(ContentVersionMeta contentVersion) =>
23-
new(
23+
public async Task<DocumentVersionItemResponseModel> CreateAsync(ContentVersionMeta contentVersion)
24+
{
25+
ReferenceByIdModel userReference = contentVersion.UserId switch
26+
{
27+
Constants.Security.UnknownUserId => new ReferenceByIdModel(),
28+
_ => new ReferenceByIdModel(await _userIdKeyResolver.GetAsync(contentVersion.UserId)),
29+
};
30+
31+
return new DocumentVersionItemResponseModel(
2432
contentVersion.VersionId.ToGuid(), // this is a magic guid since versions do not have keys in the DB
2533
new ReferenceByIdModel(_entityService.GetKey(contentVersion.ContentId, UmbracoObjectTypes.Document).Result),
26-
new ReferenceByIdModel(_entityService.GetKey(contentVersion.ContentTypeId, UmbracoObjectTypes.DocumentType)
27-
.Result),
28-
new ReferenceByIdModel(await _userIdKeyResolver.GetAsync(contentVersion.UserId)),
34+
new ReferenceByIdModel(
35+
_entityService.GetKey(contentVersion.ContentTypeId, UmbracoObjectTypes.DocumentType)
36+
.Result),
37+
userReference,
2938
new DateTimeOffset(contentVersion.VersionDate),
3039
contentVersion.CurrentPublishedVersion,
3140
contentVersion.CurrentDraftVersion,
3241
contentVersion.PreventCleanup);
42+
}
3343

3444
public async Task<IEnumerable<DocumentVersionItemResponseModel>> CreateMultipleAsync(IEnumerable<ContentVersionMeta> contentVersions)
3545
=> await CreateMultipleImplAsync(contentVersions).ToArrayAsync();

0 commit comments

Comments
 (0)