Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generating parameter validations via source generation #46349

Open
Tracked by #32557
captainsafia opened this issue Jan 31, 2023 · 3 comments
Open
Tracked by #32557

Support generating parameter validations via source generation #46349

captainsafia opened this issue Jan 31, 2023 · 3 comments
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Milestone

Comments

@captainsafia
Copy link
Member

captainsafia commented Jan 31, 2023

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:

  • Examines parameter types to determine what validation attributes might exist on them
  • Generates a static cache of validation attribute instances
  • Generates Validate overloads for each validatable parameter type
    • Support generating Validate overloads for parameter types associated with complex objects
  • Registers an endpoint filter to invoke the validate overloads and return ProblemDetails if validation fails
  • Expose abstractions that allow consumption from both minimal APIs and Blazor
@captainsafia captainsafia added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jan 31, 2023
@captainsafia captainsafia added this to the .NET 8 Planning milestone Jan 31, 2023
@mitchdenny mitchdenny added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels May 24, 2023
@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 Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@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 Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@captainsafia
Copy link
Member Author

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.

@captainsafia captainsafia modified the milestones: .NET 9 Planning, Backlog Jun 18, 2024
@Simonl9l
Copy link

Simonl9l commented Sep 14, 2024

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 - dotnet/csharplang#6373 so perhaps the C# and Asp Net Core team, and others, can get together on this?

@captainsafia
Copy link
Member Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Projects
No open projects
Status: No status
Development

No branches or pull requests

5 participants