Skip to content

Min API Validation fails for some generic types #61388

Closed
@mikekistler

Description

@mikekistler

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When the input of an API is a generic type, such as Dictionary<string,string>, the build fails with errors in the generated code.

/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(80,24): error CS0106: The modifier 'new' is not valid for this item
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(80,24): error CS1520: Method must have a return type
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(83,25): error CS0106: The modifier 'new' is not valid for this item
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(83,25): error CS1520: Method must have a return type
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(99,210): error CS0246: The type or namespace name 'ValidationOptions' could not be found (are you missing a using directive or an assembly reference?)
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(98,10): error CS9206: An interceptor cannot be declared in the global namespace.
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(81,17): error CS0246: The type or namespace name 'type' could not be found (are you missing a using directive or an assembly reference?)
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(82,17): error CS0246: The type or namespace name 'members' could not be found (are you missing a using directive or an assembly reference?)
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(84,25): error CS0246: The type or namespace name 'containingType' could not be found (are you missing a using directive or an assembly reference?)
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(85,25): error CS0246: The type or namespace name 'propertyType' could not be found (are you missing a using directive or an assembly reference?)
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(86,25): error CS0246: The type or namespace name 'name' could not be found (are you missing a using directive or an assembly reference?)
/workspaces/dotnet10-issue-repros/validation-generic-types/obj/Debug/net10.0/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator/ValidatableInfoResolver.g.cs(87,25): error CS0246: The type or namespace name 'displayName' could not be found (are you missing a using directive or an assembly reference?)

The problem may come from the name mangling done for generic types, e.g. this line in the generated file ValidatableInfoResolver.g.cs:

        private ValidatableTypeInfo CreateDictionary`2()

Expected Behavior

Turning on validation should not cause build failures.

Steps To Reproduce

The "validation-generic-types" project in this repo is a minimal repro of the problem:

https://github.com/mikekistler/dotnet10-issue-repros

The .http file contains a request that will trigger the error.

If you comment out the AddValidations() call Program.cs, the project builds and works correctly.

Exceptions (if any)

No response

.NET Version

10.0.100-preview.3.25201.16

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions