Skip to content

Commit a6e8f12

Browse files
authored
Merge pull request #441 from nanotaboada/feat/rest-polish-empty-collection-automapper-id
fix(api): return 200 OK on empty collection and ignore Id in AutoMapper (#425)
2 parents 6ef067f + 425dfe3 commit a6e8f12

File tree

4 files changed

+12
-21
lines changed

4 files changed

+12
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ This project uses famous football stadiums (A-Z) that hosted FIFA World Cup matc
4848

4949
### Fixed
5050

51+
- `GET /players` now returns `200 OK` with an empty list `[]` when no players exist, instead of `404 Not Found` (#425)
52+
- AutoMapper `Player → PlayerResponseModel` profile now explicitly ignores the `Id` source member via `ForSourceMember`, making the exclusion intentional rather than implicit (#425)
53+
5154
### Removed
5255

5356
---

src/Dotnet.Samples.AspNetCore.WebApi/Controllers/PlayerController.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public async Task<IResult> PostAsync([FromBody] PlayerRequestModel player)
6969
Title = "Conflict",
7070
Status = StatusCodes.Status409Conflict,
7171
Detail = $"Player with Squad Number '{player.SquadNumber}' already exists.",
72-
Instance = HttpContext?.Request?.Path.ToString()
72+
Instance = HttpContext?.Request?.Path.ToString(),
7373
}
7474
);
7575
}
@@ -92,29 +92,15 @@ public async Task<IResult> PostAsync([FromBody] PlayerRequestModel player)
9292
/// Retrieves all Players
9393
/// </summary>
9494
/// <response code="200">OK</response>
95-
/// <response code="404">Not Found</response>
9695
[HttpGet(Name = "Retrieve")]
9796
[ProducesResponseType<PlayerResponseModel>(StatusCodes.Status200OK)]
98-
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
9997
public async Task<IResult> GetAsync()
10098
{
10199
var players = await playerService.RetrieveAsync();
102100

103-
if (players.Count > 0)
104-
{
105-
logger.LogInformation("GET /players retrieved");
106-
return TypedResults.Ok(players);
107-
}
108-
else
109-
{
110-
logger.LogWarning("GET /players not found");
111-
return TypedResults.Problem(
112-
statusCode: StatusCodes.Status404NotFound,
113-
title: NotFoundTitle,
114-
detail: "No players were found.",
115-
instance: HttpContext?.Request?.Path.ToString()
116-
);
117-
}
101+
logger.LogInformation("GET /players retrieved {Count} player(s)", players.Count);
102+
103+
return TypedResults.Ok(players);
118104
}
119105

120106
/// <summary>

src/Dotnet.Samples.AspNetCore.WebApi/Mappings/PlayerMappingProfile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public PlayerMappingProfile()
2828

2929
// Player → PlayerResponseModel
3030
CreateMap<Player, PlayerResponseModel>()
31+
.ForSourceMember(source => source.Id, options => options.DoNotValidate())
3132
.ForMember(
3233
destination => destination.FullName,
3334
options =>

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public async Task Get_Players_Existing_ReturnsPlayers()
183183

184184
[Fact]
185185
[Trait("Category", "Unit")]
186-
public async Task Get_Players_NonExisting_Returns404NotFound()
186+
public async Task Get_Players_Empty_Returns200OkWithEmptyList()
187187
{
188188
// Arrange
189189
var (service, logger, validator) = PlayerMocks.InitControllerMocks();
@@ -196,8 +196,9 @@ public async Task Get_Players_NonExisting_Returns404NotFound()
196196

197197
// Assert
198198
service.Verify(service => service.RetrieveAsync(), Times.Once);
199-
var httpResult = result.Should().BeOfType<ProblemHttpResult>().Subject;
200-
httpResult.StatusCode.Should().Be(StatusCodes.Status404NotFound);
199+
var httpResult = result.Should().BeOfType<Ok<List<PlayerResponseModel>>>().Subject;
200+
httpResult.StatusCode.Should().Be(StatusCodes.Status200OK);
201+
httpResult.Value.Should().NotBeNull().And.BeEmpty();
201202
}
202203

203204
[Fact]

0 commit comments

Comments
 (0)