Skip to content

Allow same type names in different namespaces in validator generator #62050

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

Merged
merged 1 commit into from
May 21, 2025

Conversation

timdeschryver
Copy link
Contributor

@timdeschryver timdeschryver commented May 21, 2025

Allow same type names in different namespaces in validator generator

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

This PR inlines the validatable type information to prevent name collisions when e.g. a same name type is used in different namespaces.

Description

  • Remove the CreateMethods generation
  • Replace CreateMethods with adding the type information directly in TryGetValidatableTypeInfo`. We could also generate unique names by including the namespace, but this won't work for system types (e.g. collections or dictionaries)
  • Update snapshots
  • Create a new unit test that uses two namespaces with the same type, but with a different validation

Fixes #61971

@github-actions github-actions bot added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label May 21, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 21, 2025
Copy link
Member

@captainsafia captainsafia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for taking the time to file the bug and fix it.

As a side note: the code complete window for preview.5 closed yesterday so that means this will be merged in for release on preview.6. Hope the wait isn't a problem!


namespace Microsoft.AspNetCore.Http.ValidationsGenerator;

public sealed partial class ValidationsGenerator : IIncrementalGenerator
{
public static string GeneratedCodeConstructor => $@"global::System.CodeDom.Compiler.GeneratedCodeAttribute(""{typeof(ValidationsGenerator).Assembly.FullName}"", ""{typeof(ValidationsGenerator).Assembly.GetName().Version}"")";
public static string GeneratedCodeAttribute => $"[{GeneratedCodeConstructor}]";
private static readonly Regex InvalidNameCharsRegex = new("[^0-9A-Za-z_]", RegexOptions.Compiled);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love that we can get rid of this now! 😄

@captainsafia captainsafia merged commit c5d6996 into dotnet:main May 21, 2025
28 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview6 milestone May 21, 2025
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 community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Minimal API Validation with types using the same name
2 participants