Skip to content

OptionalParameterinWebApi: retarget net10.0, drop Swashbuckle, add the GetBy pair and the missing optional-parameter tests - #2168

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/65547-optional-params
Open

OptionalParameterinWebApi: retarget net10.0, drop Swashbuckle, add the GetBy pair and the missing optional-parameter tests#2168
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/65547-optional-params

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample for Optional Parameters in ASP.NET Core Web API Routing, brought up to date alongside a rewrite of the article.

Retarget. Both projects move net6.0 -> net10.0. Test project packages: Microsoft.AspNetCore.Mvc.Testing 10.0.11, Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1 (all current on NuGet today). The tests dropped their undeclared Newtonsoft.Json dependency, which only ever arrived transitively through the old test SDK, and use System.Net.Http.Json instead.

Swashbuckle removed. The sample is about route templates, and the article never shows Program.cs or the Swagger UI, so the package and its four Program.cs calls are gone rather than bumped.

The article's second code block now exists in the sample. The article prints a GetBy(string name) / GetBy(int id) pair on {name} and {id:int} to show constraint-based disambiguation, and ProductController never carried it, so that snippet had never been compiled or run. It is added, and GetBy_WithName_ReturnsProductMatchedByName / GetBy_WithInt_ReturnsProductMatchedById prove the two overlapping templates disambiguate instead of throwing AmbiguousMatchException.

The missing test. No test called the endpoint without an id, which is the one thing the article is about. GetById_WhenIdOmitted_ReturnsDefaultProduct does. Test/WeatherForecastControllerTest.cs was an empty stub, so the article's headline {id?} snippet had no test at all; it now has two.

New RouteTemplateController and its tests. One action per template form, each reporting the bound value and whether the route value was set, so the article's central distinction is observed rather than asserted:

Request Result
Optional/{id?} with int id = 1, segment omitted id=1, route value not set
OptionalNoDefault/{id?} with int id, no method default, segment omitted id=0, route value not set, HTTP 200
Default/{id=1}, segment omitted id=1, route value set
Default/{id=1} with /1 supplied byte-identical response to the omitted case
ConstrainedDefault/{id:int=1}, segment omitted id=1, route value set
ConstrainedOptional/{id:int?} and ConstrainedDefault/{id:int=1} with a non-integer segment 404, not 400

The second row is the interesting one: a method default is not a requirement for {id?}. Omit it and the URL still matches, the parameter just binds 0 with no error.

Cleanups. Unused ILogger injection deleted from both controllers (nothing read it), and the two string[] literals become collection expressions.

dotnet build and dotnet test on SDK 10.0.302: 0 warnings, 0 errors, 22 tests passing.

…e GetBy pair and the missing optional-parameter tests

- net6.0 -> net10.0 on both projects; Mvc.Testing 10.0.11, Test.Sdk 18.9.0,
  xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1.
- Remove Swashbuckle and its Program.cs calls: the sample is about routing and
  the article never shows Program.cs or the Swagger UI.
- Add the GetBy(string name) / GetBy(int id) pair the article prints but the
  sample never carried, plus tests proving the int constraint disambiguates them.
- Fill the empty WeatherForecastControllerTest stub and add the test nobody had
  written: GetById with the id segment omitted.
- Add RouteTemplateController and its tests, isolating {id?}, {id?} with no
  method default, {id=1}, {id:int?} and {id:int=1} so the difference between an
  optional parameter and a route default is observed rather than asserted.
- Drop the unused ILogger injection from both controllers; collection
  expressions for the two string arrays.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant