Skip to content

Minimal API Validation includes validation for service provider service #63289

@martincostello

Description

@martincostello

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Adding Minimal API validation to an existing application running .NET 10, `` types are generated for TimeProvider despite being a service.

This was addressed previously in #61392, so I assume this is an edge case missed previously.

My guess would be because the endpoint points to a method group, rather than being defined inline:

group.MapGet("/time", GetTime);

private static Ok<TimeResponse> GetTime(TimeProvider timeProvider) { ... }

Viewing the source generator output shows the following code in the TryGetValidatableTypeInfo() method:

            if (type == typeof(global::System.TimeZoneInfo))
            {
                validatableInfo = new GeneratedValidatableTypeInfo(
                    type: typeof(global::System.TimeZoneInfo),
                    members: [
                        new GeneratedValidatablePropertyInfo(
                            containingType: typeof(global::System.TimeZoneInfo),
                            propertyType: typeof(global::System.TimeZoneInfo),
                            name: "Local",
                            displayName: "Local"
                        ),
                        new GeneratedValidatablePropertyInfo(
                            containingType: typeof(global::System.TimeZoneInfo),
                            propertyType: typeof(global::System.TimeZoneInfo),
                            name: "Utc",
                            displayName: "Utc"
                        ),
                    ]
                );
                return true;
            }
            if (type == typeof(global::System.TimeProvider))
            {
                validatableInfo = new GeneratedValidatableTypeInfo(
                    type: typeof(global::System.TimeProvider),
                    members: [
                        new GeneratedValidatablePropertyInfo(
                            containingType: typeof(global::System.TimeProvider),
                            propertyType: typeof(global::System.TimeZoneInfo),
                            name: "LocalTimeZone",
                            displayName: "LocalTimeZone"
                        ),
                        new GeneratedValidatablePropertyInfo(
                            containingType: typeof(global::System.TimeProvider),
                            propertyType: typeof(global::System.TimeProvider),
                            name: "System",
                            displayName: "System"
                        ),
                    ]
                );
                return true;
            }

Adding [FromServices] to the endpoint code explicitly removes the types from the generated code.

- private static Ok<TimeResponse> GetTime(TimeProvider timeProvider)
+ private static Ok<TimeResponse> GetTime([FromServices] TimeProvider timeProvider)

Expected Behavior

No GeneratedValidatableTypeInfo is generated for TimeProvider.

Steps To Reproduce

  1. Clone martincostello/api@024a90b
  2. Run build.ps1 in the root of the repo
  3. Run startvs.cmd
  4. View the generated code for Microsoft.Extensions.Validation.ValidationsGenerator in Visual Studio.

Exceptions (if any)

None.

.NET Version

10.0.100-rc.1.25414.111

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: By DesignResolved because the behavior in this issue is the intended design.Status: Resolvedarea-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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions