Skip to content

Management API: Return not found from request for content references when entity does not exist (closes #20997)#20999

Merged
Zeegaan merged 8 commits intomainfrom
v17/bugfix/return-not-found-for-references-requests
Dec 2, 2025
Merged

Management API: Return not found from request for content references when entity does not exist (closes #20997)#20999
Zeegaan merged 8 commits intomainfrom
v17/bugfix/return-not-found-for-references-requests

Conversation

@AndyButland
Copy link
Contributor

Prerequisites

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

Addresses: #20997

Description

Between 16 and 17 it is reported that these reference endpoints would return all items instead of an empty collection when the provided ID was not recognised. It seems better really that these would return "not found", which is what I've done in this PR.

Testing

Using the management API, make requests to e.g. umbraco/management/api/v1/media/{id}/referenced-descendants and verify that providing an ID that doesn't exist, or isn't of the right type, returns a 404 response.

Copilot AI review requested due to automatic review settings November 28, 2025 15:34
@AndyButland AndyButland changed the title Management API: Return not found from request for content references when entity does not exist Management API: Return not found from request for content references when entity does not exist (closes #20997) Nov 28, 2025
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 fixes a regression in the Management API where reference endpoints (ReferencedBy and ReferencedDescendants) would return all items instead of a 404 Not Found response when an invalid entity ID was provided. The change adds entity existence validation using IEntityService before processing reference queries.

Key changes:

  • Added 404 Not Found validation for all reference endpoints across Document, Media, and Member controllers
  • Introduced IEntityService dependency injection with backward-compatible obsolete constructors
  • Added ProducesResponseType attributes documenting the new 404 responses

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
ReferencedDescendantsMemberController.cs Added entity validation to return 404 for non-existent member IDs
ReferencedByMemberController.cs Added entity validation to return 404 for non-existent member IDs
ReferencedDescendantsMediaController.cs Added entity validation to return 404 for non-existent media IDs
ReferencedByMediaController.cs Added entity validation to return 404 for non-existent media IDs
ReferencedDescendantsDocumentController.cs Added entity validation to return 404 for non-existent document IDs
ReferencedByDocumentController.cs Added entity validation to return 404 for non-existent document IDs

Comment on lines +61 to +65
IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Document);
if (entity is null)
{
return NotFound();
}
Copy link
Member

@Zeegaan Zeegaan Dec 1, 2025

Choose a reason for hiding this comment

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

I think I disagree on this approach, sorry 😢
I think this logic doesn't belong in the controller itself, but in the service.

I think we should update the ITrackedReferencesService
to instead adopt the Attempt<T> pattern, which most of our services have adopted at this point, and then it will return a failed attempt, if the IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Document); returns null. I guess you also need to pass the ObjectType, but I don't see the problem in that.
We actually already inject the IEntityService in the service, but it's not used for now 🙈

Image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need to apologies, you are right in my view. I've refactored to implement this in the way you have suggested. Would you mind having another look please? Thanks.

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated 14 comments.

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 to me 👍

@Zeegaan Zeegaan enabled auto-merge (squash) December 2, 2025 04:25
@Zeegaan Zeegaan disabled auto-merge December 2, 2025 04:25
@Zeegaan Zeegaan merged commit da94e09 into main Dec 2, 2025
25 of 26 checks passed
@Zeegaan Zeegaan deleted the v17/bugfix/return-not-found-for-references-requests branch December 2, 2025 04:25
Zeegaan pushed a commit that referenced this pull request Dec 2, 2025
…when entity does not exist (closes #20997) (#20999)

* Return not found when request for content references when entity does not exist.

* Apply suggestions from code review

Co-authored-by: Copilot <[email protected]>

* Move check for entity existence from controller to the service.

* Update OpenApi.json.

* Apply suggestions from code review

Co-authored-by: Copilot <[email protected]>

* Addressed points raised in code review.

* Update OpenApi.json

* Resolved breaking changes.

---------

Co-authored-by: Copilot <[email protected]>
(cherry picked from commit da94e09)
@Zeegaan
Copy link
Member

Zeegaan commented Dec 2, 2025

Cherry picked for 17.0.1 🚀

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