Skip to content

Minimal API Validation with types using the same name #61971

@timdeschryver

Description

@timdeschryver

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When multiple types with the same name are used (but within a different namespace), the generated output contains errors. This leads in a failing build.

The output will contain duplicate names, causing the error.
Here's an extract of a class Entity that exists in the Persons and Pets namespaces:

if (type == typeof(global::MinApiValidationRepro.Persons.Entity))
{
  validatableInfo = CreateEntity();
  return true;
}
if (type == typeof(global::MinApiValidationRepro.Pets.Entity))
{
  validatableInfo = CreateEntity();
  return true;
}

private ValidatableTypeInfo CreateEntity()
{
            return new GeneratedValidatableTypeInfo(
                type: typeof(global::MinApiValidationRepro.Persons.Entity),
                members: [
                    new GeneratedValidatablePropertyInfo(
                        containingType: typeof(global::MinApiValidationRepro.Persons.Entity),
                        propertyType: typeof(int),
                        name: "Id",
                        displayName: "Id"
                    ),
                    new GeneratedValidatablePropertyInfo(
                        containingType: typeof(global::MinApiValidationRepro.Persons.Entity),
                        propertyType: typeof(string),
                        name: "Name",
                        displayName: "Name"
                    ),
                ]
            );
}

private ValidatableTypeInfo CreateEntity()
{
            return new GeneratedValidatableTypeInfo(
                type: typeof(global::MinApiValidationRepro.Pets.Entity),
                members: [
                    new GeneratedValidatablePropertyInfo(
                        containingType: typeof(global::MinApiValidationRepro.Pets.Entity),
                        propertyType: typeof(int),
                        name: "Id",
                        displayName: "Id"
                    ),
                    new GeneratedValidatablePropertyInfo(
                        containingType: typeof(global::MinApiValidationRepro.Pets.Entity),
                        propertyType: typeof(string),
                        name: "Breed",
                        displayName: "Breed"
                    ),
                ]
            );
}

Expected Behavior

I expect the build to succeed.
Maybe that the fully qualified type name can be used to generate the code?

Steps To Reproduce

You can checkout the following reproduction, https://github.com/timdeschryver/MinApiValidationRepro.

Try to run a build, which will fail.

The repo also contains the generated files.

Exceptions (if any)

MinApiValidationRepro failed with 1 error(s) (0,9s)
D:\MinApiValidationRepro\Generated\Microsoft.AspNetCore.Http.ValidationsGenerator\Microsoft.AspNetCore.Http.ValidationsGenerator.ValidationsGenerator\ValidatableInfoResolver.g.cs(103,37): error CS0111: Type 'GeneratedValidatableInfoResolver' already defines a member called 'CreateEntity' with the same parameter types

Build failed with 1 error(s) in 3,3s

The build failed. Fix the build errors and run again.

.NET Version

10.0.100-preview.4.25258.110

Anything else?

I really like the addition of this feature 🥳

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