Skip to content

Conversation

@desjoerd
Copy link
Contributor

OpenAPI: Fix duplicate xml documentation ids for Generic properties and references

Fixes two instances where xml comment documentation ids where duplicated.
First one was for constructed generic type properties. Foo<int>.
Second one was for Xml comment files exposing internal classes with the same namespace.

Fixes #64378

@desjoerd desjoerd requested review from a team and captainsafia as code owners November 17, 2025 21:27
Copilot AI review requested due to automatic review settings November 17, 2025 21:27
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Nov 17, 2025
Copilot finished reviewing on behalf of desjoerd November 17, 2025 21:29
return comments;
}

internal static IEnumerable<(string, XmlComment?)> ParseCommentsReference(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If someone has a better idea for this, let me know. The main difference compared to ParseCommentsApplicationAssembly is that it only allows "accessible" types

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Nov 17, 2025
@dotnet-policy-service
Copy link
Contributor

Thanks for your PR, @@desjoerd. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes duplicate XML documentation IDs in OpenAPI source generation by addressing two specific issues:

  1. Generic type properties with different type arguments (e.g., GenericFoo<string> vs GenericFoo<int>) were generating duplicate documentation IDs
  2. Internal classes with the same namespace in different referenced assemblies were causing duplicate documentation IDs

Key Changes

  • Separated comment parsing logic into application assembly vs reference assembly methods
  • Modified AssemblyTypeSymbolsVisitor to use unbound generic types for documentation ID generation
  • Added new test case to verify that duplicate documentation IDs are not generated

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
XmlCommentDocumentationIdTests.cs Added comprehensive test case verifying that generic properties and duplicate internal classes don't cause duplicate documentation IDs
OpenApiXmlCommentSupport.generated.verified.cs Generated snapshot file showing expected output with properly deduplicated documentation IDs
XmlCommentGenerator.cs Renamed ParseComments to separate methods for application and reference assemblies
XmlCommentGenerator.Parser.cs Split comment parsing into two methods with different filtering logic for application vs reference assemblies
AssemblyTypeSymbolsVisitor.cs Added logic to convert constructed generic types to unbound generic types for consistent documentation ID generation

Comment on lines +148 to +149
// Only include symbols that are accessible from the application assembly.
symbol.IsAccessibleType() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

@desjoerd this seems okay, but should this PR also revisit the use of symbol.IsAccessible() up on L124?

Specifically, it seems like this source generator should default to excluding types marked with EmbeddedAttribute, since those are accessible from the application assembly, but seem to be of dubious worth w.r.t. embedding constants into the assembly for (what I assume are largely going to be) marker attributes for source generators, dev dependencies, etc.

(e.g. thinking about this from the aot perspective where I don't want paragraphs and paragraphs xml docs from internal/dev-time-only marker attributes getting baked into the executable)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the application assembly types with [Embedded] are accessible and could even be used within your Api I think.

For referenced assemblies they will be already excluded as it will only emit xml comments for types marked as public.
https://github.com/dotnet/aspnetcore/blob/main/src%2FOpenApi%2Fgen%2FHelpers%2FISymbolExtensions.cs#L193

var parsedCommentsFromXmlFile = commentsFromXmlFile
.Combine(context.CompilationProvider)
.Select(ParseComments);
.Select(ParseCommentsReference);
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm...is this change related to the fix for generic types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one is to fix the comments coming from a documentation file when a type also exists in the main compilation.

When an internal type with the same namespace and class name exists in the Application assembly.

In the unit tests it's the Sample.Duplicate which is internal in the Application assembly. When it processes it from a different assembly it would resolve to Sample.Duplicate.

I duplicated the ParseComments into ParseCommentsReference and ParseCommentsApplicationAssembly to have a different condition. ParseCommentsReference only includes public types. ParseCommentsApplicationAssembly includes public types and types available in the compilation.

@gfoidl gfoidl added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Nov 19, 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 feature-openapi

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenApi XML comment cache gneration fails with System.ArgumentException: An item with the same key has already been added

4 participants