dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.48k stars 10.03k forks source link

Support generating parameter validations via source generation #46349

Open captainsafia opened 1 year ago

captainsafia commented 1 year ago

Minimal APIs doesn't currently support validations on request parameters. Ideally, this is functionality that we would be able to support by taking advantage of the endpoint filters feature introduced in .NET 7 and the existing of the DataAnnotations attributes.

In line with the implementation of the compile-time based generator for route handler based endpoints, we want to support this functionality using a source generator. Some of this is prototyped in this repo.

The code generated for validation does the following:

captainsafia commented 4 months ago

Hi all --

We originally intended to ship this feature in .NET 9. However, due to shifting priorities and some additional requirements associated with this feature, this work is being punted to a future release.

For the time being, we recommend using packages, like MiniValidation, to add validation support to your minimal APIs. While we work through all the technical details to ship a high-quality feature in a future release, we'd still appreciate your feedback to scope out the shape of the API as needed.

Simonl9l commented 2 months ago

I'm exploring this subject and by no mean an expert

I'd suggest from a scope perspective, there are use cases as simple as a console app not using a Host, and perhaps a simple DI Service Container in support of say a CLI of some kind.

System.ComponentModel.Annotations seems to have a lot of dependencies on Aps.Net (Core), and the ValidationContext with [RequiresUnreferencedCode] attributes all over it is not AoT-able.

It's also a logical need to be able to inject a context with the DI Service Provider into validations equivalent to [ValidationAttribute].

I also ran into this related discussion - https://github.com/dotnet/csharplang/discussions/6373 so perhaps the C# and Asp Net Core team, and others, can get together on this?

captainsafia commented 2 months ago

@Simonl9l Thanks for sharing these thoughts!

There have been some conversations about the possibility of generating a "common type validation source generator" based on the System.ComponentModel.Annotations library. The primary motivations was the goal of standardizing the way validation is done across implementations in the ecosystem (the Options validation generator that shipped in .NET 8, the minimal APIs validation generator once we ship it, and support for Blazor Web) and providing an avenue for code sharing amongst the stacks.

We've only had a few conversations about what this might look like and haven't committed to a full design yet, but the possibility of some sort of common layer for source generator-based validation engines has definitely been discussed.