Skip to content

Conversation

Alex-Sob
Copy link

@Alex-Sob Alex-Sob commented Sep 25, 2025

This PR should address #45218

This PR is adding a new code analysis rule ASP0029: Invalid constraint for parameter type for Minimal APIs. The diagnostic should be reported if route constraint cannot be used with the type of the corresponding parameter declared in the route handler, for example:

app.MapGet("/{id:datetime}", (int id) => ...);
app.MapGet("/{id:alpha}", (int id) => ...);
app.MapGet("/{id:bool}", (int id) => ...);

Rule

Rule id Title Description
ASP0029 Invalid constraint for parameter type The constraint '{0}' on parameter '{1}' can't be used with type '{2}'

The following table lists constraints and types they can be used with:

Constraint Types
alpha string
bool bool
datetime System.DateTime
decimal decimal
double double
guid System.Guid
long long, ulong
int byte, sbyte, short, ushort, int, uint, long, ulong
min(10) byte, sbyte, short, ushort, int, uint, long, ulong
max(10) byte, sbyte, short, ushort, int, uint, long, ulong
range(1,10) byte, sbyte, short, ushort, int, uint, long, ulong
length(10) string
minlength(10) string
maxlength(10) string
regex(\w+) string
file string
nonfile string

Changes

  • Added a new rule to DiagnosticDescriptors.cs:

    I used a helper method with [CallerMemberName] string name = null parameter that makes creating localizable resource strings unnecessary and simplifies creating diagnostic descriptors. Can I simplify all other descriptors this way?

  • Augmented RouteHandlerAnalyzer with route constraints analysis to check if a constraint specified for route parameter is invalid for the type of the corresponding parameter declared in the route handler.

  • Added unit tests.

    I used the same approach for unit tests here that I suggested separately in [Proposal] Performance/usability improvements for analyzer tests #63684

@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Sep 25, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 25, 2025
Copy link
Contributor

Thanks for your PR, @@Alex-Sob. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@martincostello martincostello added analyzer Indicates an issue which is related to analyzer experience area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Sep 25, 2025
Copy link
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer Indicates an issue which is related to analyzer experience area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants