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
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Testing10.0.11,Microsoft.NET.Test.Sdk18.9.0,xunit2.9.3,xunit.runner.visualstudio4.0.0,coverlet.collector10.0.1 (all current on NuGet today). The tests dropped their undeclaredNewtonsoft.Jsondependency, which only ever arrived transitively through the old test SDK, and useSystem.Net.Http.Jsoninstead.Swashbuckle removed. The sample is about route templates, and the article never shows
Program.csor the Swagger UI, so the package and its fourProgram.cscalls 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, andProductControllernever carried it, so that snippet had never been compiled or run. It is added, andGetBy_WithName_ReturnsProductMatchedByName/GetBy_WithInt_ReturnsProductMatchedByIdprove the two overlapping templates disambiguate instead of throwingAmbiguousMatchException.The missing test. No test called the endpoint without an id, which is the one thing the article is about.
GetById_WhenIdOmitted_ReturnsDefaultProductdoes.Test/WeatherForecastControllerTest.cswas an empty stub, so the article's headline{id?}snippet had no test at all; it now has two.New
RouteTemplateControllerand 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:Optional/{id?}withint id = 1, segment omittedid=1, route value not setOptionalNoDefault/{id?}withint id, no method default, segment omittedid=0, route value not set, HTTP 200Default/{id=1}, segment omittedid=1, route value setDefault/{id=1}with/1suppliedConstrainedDefault/{id:int=1}, segment omittedid=1, route value setConstrainedOptional/{id:int?}andConstrainedDefault/{id:int=1}with a non-integer segmentThe 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 binds0with no error.Cleanups. Unused
ILoggerinjection deleted from both controllers (nothing read it), and the twostring[]literals become collection expressions.dotnet buildanddotnet teston SDK 10.0.302: 0 warnings, 0 errors, 22 tests passing.