Skip to content

Commit cefa875

Browse files
GET request parameters: retarget net10.0, drop AutoMapper, real test assertions (#2177)
Retarget both projects from net6.0 to net10.0. Remove AutoMapper. Product and ProductDto declared the same six properties in the same order, so the mapper copied an object onto its identical twin, which taught nothing about passing parameters to a GET action. The package, Mapper/Map.cs, Models/ProductDto.cs, the IMapper field and constructor, and all eight Map calls are gone; every endpoint now returns the entity and the JSON responses are byte-identical to before. Program.cs moves to top-level statements, with the public partial class Program declaration WebApplicationFactory<Program> needs in the test project. Packages: Swashbuckle.AspNetCore 6.2.3 to 10.2.3, MSTest 2.2.8 to the 4.3.3 meta package, Microsoft.NET.Test.Sdk 17.1.0 to 18.9.0, Microsoft.AspNetCore.Mvc.Testing 6.0.0 to 10.0.11, coverlet.collector 3.1.2 to 10.0.1. Newtonsoft.Json 13.0.4 is now referenced explicitly: it used to arrive transitively through Microsoft.TestPlatform.TestHost 17.1.0, which declared Newtonsoft.Json 9.0.1; TestHost 18.9.0 declares no such dependency, so the tests would not compile without it. Tests: all eight asserted nothing. Each did Assert.IsNotNull followed by a foreach over the response, so an empty list passed green, and there was no count assertion in the file. One of the eight really did return empty: the route-plus-query test called ?waranty=2, one r, against a warranty parameter, so the value never bound, warranty defaulted to 0, and no product matched. Fixed the spelling and gave every test a status-code check, a count check and an exact identity check on the returned products. With the misspelling still in place the corrected test fails on the count, expected 2 and actual 0. Build clean with 0 warnings, 8 of 8 tests passing on net10.0.
1 parent 0b8e13b commit cefa875

7 files changed

Lines changed: 106 additions & 180 deletions

File tree

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using AutoMapper;
2-
using Microsoft.AspNetCore.Authorization;
31
using Microsoft.AspNetCore.Mvc;
42
using MultipleParametersInGetMethod.Models;
53

@@ -19,12 +17,6 @@ public class ProductController : ControllerBase
1917
new Product{ Id = 6, Category = "Sports", Brand = "Adidas", Name = "Football", WarrantyYears = 3, IsAvailable = false },
2018
new Product{ Id = 7, Category = "Electronic", Brand = "Apple", Name = "Mobile", WarrantyYears = 2, IsAvailable = true }
2119
};
22-
private readonly IMapper _mapper;
23-
24-
public ProductController(IMapper mapper)
25-
{
26-
_mapper = mapper;
27-
}
2820

2921
[HttpGet]
3022
public IActionResult GetProductsByCategoryAndBrand(string category, string brand)
@@ -33,7 +25,7 @@ public IActionResult GetProductsByCategoryAndBrand(string category, string brand
3325
.Where(x => x.Category == category && x.Brand == brand)
3426
.ToList();
3527

36-
return Ok(_mapper.Map<List<ProductDto>>(result));
28+
return Ok(result);
3729
}
3830

3931
[HttpGet("type-manufacturer")]
@@ -45,57 +37,57 @@ public IActionResult GetProductsByCategoryAndBrandUsingFromQuery(
4537
.Where(x => x.Category == category && x.Brand == brand)
4638
.ToList();
4739

48-
return Ok(_mapper.Map<List<ProductDto>>(result));
40+
return Ok(result);
4941
}
5042

5143
[HttpGet("{id}")]
5244
public IActionResult GetProductById(int id)
5345
{
54-
return Ok(_mapper.Map<ProductDto>(_products
46+
return Ok(_products
5547
.Where(x => x.Id == id)
56-
.FirstOrDefault()));
48+
.FirstOrDefault());
5749
}
5850

5951
[HttpGet("productId/{productId}")]
6052
public IActionResult GetProductByIdUsingFromRoute([FromRoute(Name = "productId")] int id)
6153
{
62-
return Ok(_mapper.Map<ProductDto>(_products
54+
return Ok(_products
6355
.Where(x => x.Id == id)
64-
.FirstOrDefault()));
56+
.FirstOrDefault());
6557
}
6658

6759
[HttpGet("brand/{brand}")]
6860
public IActionResult GetProductsByBrandAndWarranty(string brand, int warranty)
6961
{
70-
return Ok(_mapper.Map<List<ProductDto>>(_products
62+
return Ok(_products
7163
.Where(x => x.Brand == brand && x.WarrantyYears == warranty)
72-
.ToList()));
64+
.ToList());
7365
}
7466

7567
[HttpGet("manufacturer/{manufacturer}")]
7668
public IActionResult GetProductsByBrandAndWarrantyUsingAttributes(
7769
[FromRoute(Name = "manufacturer")] string brand,
7870
[FromQuery(Name = "coverage")] int warranty)
7971
{
80-
return Ok(_mapper.Map<List<ProductDto>>(_products
72+
return Ok(_products
8173
.Where(x => x.Brand == brand && x.WarrantyYears == warranty)
82-
.ToList()));
74+
.ToList());
8375
}
8476

8577
[HttpGet("category")]
86-
public IActionResult GetProductsByCategory([FromBody] ProductDto model)
78+
public IActionResult GetProductsByCategory([FromBody] Product model)
8779
{
88-
return Ok(_mapper.Map<List<ProductDto>>(_products
80+
return Ok(_products
8981
.Where(x => x.Category == model.Category)
90-
.ToList()));
82+
.ToList());
9183
}
9284

9385
[HttpGet("category-brand")]
9486
public IActionResult GetProductsByCategoryAndBrandViaHeaders([FromHeader] string category, [FromHeader] string brand)
9587
{
96-
return Ok(_mapper.Map<List<ProductDto>>(_products
88+
return Ok(_products
9789
.Where(x => x.Category == category && x.Brand == brand)
98-
.ToList()));
90+
.ToList());
9991
}
10092
}
101-
}
93+
}

‎aspnetcore-webapi/MultipleParametersInGetMethod/MultipleParametersInGetMethod/Mapper/Map.cs‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎aspnetcore-webapi/MultipleParametersInGetMethod/MultipleParametersInGetMethod/Models/ProductDto.cs‎

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
11-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.3" />
1211
</ItemGroup>
1312

1413
</Project>
Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
namespace MultipleParametersInGetMethod
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
builder.Services.AddControllers();
5+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
6+
builder.Services.AddEndpointsApiExplorer();
7+
builder.Services.AddSwaggerGen();
8+
9+
var app = builder.Build();
10+
11+
// Configure the HTTP request pipeline.
12+
if (app.Environment.IsDevelopment())
213
{
3-
public class Program
4-
{
5-
public static void Main(string[] args)
6-
{
7-
var builder = WebApplication.CreateBuilder(args);
8-
9-
// Add services to the container.
10-
builder.Services.AddAutoMapper(typeof(Program));
11-
builder.Services.AddControllers();
12-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
13-
builder.Services.AddEndpointsApiExplorer();
14-
builder.Services.AddSwaggerGen();
15-
16-
var app = builder.Build();
17-
18-
// Configure the HTTP request pipeline.
19-
if (app.Environment.IsDevelopment())
20-
{
21-
app.UseSwagger();
22-
app.UseSwaggerUI();
23-
}
24-
25-
app.UseHttpsRedirection();
26-
27-
app.UseAuthorization();
28-
29-
app.MapControllers();
30-
31-
app.Run();
32-
}
33-
}
34-
}
14+
app.UseSwagger();
15+
app.UseSwaggerUI();
16+
}
17+
18+
app.UseHttpsRedirection();
19+
20+
app.UseAuthorization();
21+
22+
app.MapControllers();
23+
24+
app.Run();
25+
26+
public partial class Program { }

0 commit comments

Comments
 (0)