dotnet / dotnet/aspnetcore

Warning on use of regex constraints in route parameters.

Open
#46,546 3 comments 0 reactions 0 assignees View on GitHub
analyzer api-suggestion area-minimal feature-routing
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

## Background and Motivation

As part of #46227 we are removing the default regex constraint from the set of constraints available when ```CreateSlimBuilder(...)``` is used. The reason for this is that including regular expression support on constraints pulls in about 800K of extra file size. We still include the regex constraint resolver when ```CreateBuilder(...)``` is used.

However ... there is an opportunity for people to continue to use regular expressions for router parameters if they implement their own route constraints and provided they know what the regular expression is in advance they can take advantage of source generated regular expressions. We want an analyzer which detects the use of regex constraints and adds a warning which links to documentation on how to create a custom route constraint that uses source generated regex.

## Proposed Analyzer

### Analyzer Behavior and Message

When a route pattern is used that includes a regex constraint, we would output the following info message:

> INFO: Recommend use of custom reoute constraint with source generated regular expressions. See https://aka.ms/aspet/tba for more details.

Note, if we detect that we are using CreateSlimBuilder (won't always be possible) then we can increase this to a warning?

### Category

- [ ] Design
- [ ] Documentation
- [ ] Globalization
- [ ] Interoperability
- [ ] Maintainability
- [ ] Naming
- [ ] Performance
- [ ] Reliability
- [ ] Security
- [ ] Style
- [x] Usage

### Severity Level

- [ ] Error
- [ ] Warning
- [x] Info
- [ ] Hidden

## Usage Scenarios

```csharp
app.MapGet("/products/{productId:regex(...)}", (string productId) => {});
```

## Risks

To discuss.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.