Skip to content

Handle user id 0 (Unknown/System) when building content version response#19361

Merged
lauraneto merged 1 commit intomainfrom
v16/bugfix/error-loading-rollback-section
May 20, 2025
Merged

Handle user id 0 (Unknown/System) when building content version response#19361
lauraneto merged 1 commit intomainfrom
v16/bugfix/error-loading-rollback-section

Conversation

@lauraneto
Copy link
Contributor

Prerequisites

  • I have added steps to test this contribution in the description below

This relates to issue #17149, as it was also being caused by having entries with user id 0.

Description

Details regarding the initial issue and how to test can be found in PR #19263.
In this case, instead of just opening the info tab, you will need to open the Rollback section.

The following code can be used to perform a publish with user id 0.

[Route("api/test")]
public class TestController : ControllerBase
{
    private readonly IContentService _contentService;

    public TestController(IContentService contentService)
    {
        _contentService = contentService;
    }

    [HttpGet("")]
    public IActionResult Get()
    {
        // Replace the GUID with the id of the content you created to test
        IContent content = _contentService.GetById(Guid.Parse("efd8a078-14d8-47f7-b22c-52d34944b6b0")) ?? throw new InvalidOperationException();
        _contentService.Save(content, 0);
        _contentService.Publish(content, ["*"], 0);

        return Ok();
    }
}

Before the fix

Recording 2025-05-19 at 15 27 03

After the fix

Recording 2025-05-19 at 15 23 56

…nse 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.
Copilot AI review requested due to automatic review settings May 19, 2025 13:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the document version response factory to correctly handle system/unknown users (ID 0) when building the API model.

  • Refactors CreateAsync from an expression-bodied method to a block body
  • Adds a switch to return an empty ReferenceByIdModel for Constants.Security.UnknownUserId
  • Cleans up formatting around entity key resolution calls
Comments suppressed due to low confidence (2)

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

  • Avoid using .Result inside an async method as it can block threads; consider awaiting the asynchronous GetKey call directly, e.g. await _entityService.GetKey(...).
new ReferenceByIdModel(_entityService.GetKey(contentVersion.ContentId, UmbracoObjectTypes.Document).Result),

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

  • Add a unit test to cover the UnknownUserId branch in CreateAsync to verify that an empty ReferenceByIdModel is returned when contentVersion.UserId is 0.
Constants.Security.UnknownUserId => new ReferenceByIdModel(),

Copy link
Member

@Zeegaan Zeegaan left a comment

Choose a reason for hiding this comment

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

Looks good, tests good ✔️

@lauraneto lauraneto merged commit 225fed5 into main May 20, 2025
26 checks passed
@lauraneto lauraneto deleted the v16/bugfix/error-loading-rollback-section branch May 20, 2025 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants