Skip to content

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

Merged
wtgodbe merged 6 commits into
dotnet:mainfrom
desjoerd:fix/gh-64378
Jun 12, 2026
Merged

wtgodbe merged 6 commits into
dotnet:mainfrom
desjoerd:fix/gh-64378

Conversation

@desjoerd

@desjoerd desjoerd commented Nov 17, 2025 •

Copy link
Copy Markdown
Contributor

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

Fixes instances where xml comment documentation ids where duplicated for constructed generic type properties. Foo<int>.

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
@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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 thread src/OpenApi/gen/XmlCommentGenerator.cs Resolved
@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
@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Nov 26, 2025
@twenzel

twenzel commented Apr 29, 2026

Copy link
Copy Markdown

@desjoerd any progress on this PR?

@desjoerd

Copy link
Copy Markdown
Contributor Author

This PR was ready for review. Probably I need to merge/regenerate the snapshot tests as there are probably some changes done around the xml doc generation. Will do that!

@Youssef1313

Copy link
Copy Markdown
Member

Note for us: when merging this, we need to backport this to 10.0.

@Youssef1313 Youssef1313 added the pr: pending author input For automation. Specifically separate from Needs: Author Feedback label May 25, 2026
@desjoerd

Copy link
Copy Markdown
Contributor Author

@Youssef1313 I've rebased and I think I've addressed your comments. Could you please review again :).

@desjoerd
desjoerd requested a review from Youssef1313 May 27, 2026 13:20
return comments;
}

internal static IEnumerable<(string, XmlComment?)> ParseComments(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure it's worth the extra complication implemented here.

If we want to truly guarantee de-duplication across types from current compilation and from other XML inputs, I think either calling TryAdd, or alternatively adjusting here to de-duplicate.

In theory, there can still be two public types in the same namespace but from different assemblies, and this is a case that can be ambiguous anyways, I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I think we could deduplicate here https://github.com/desjoerd/aspnetcore/blob/251854361046ac6c92f68d43f8b55cf777e760be/src/OpenApi/gen/XmlCommentGenerator.cs#L38-L41 or have a context with all the comments. I think it would be good to let the comments from the compilation win.

Adding a TryAdd is also an easier option if we change the order of the Emitting at the referenced lines (so the compilation will win over referenced assemblies in the case of a conflict).

@Youssef1313 let me know if you would prefer the TryAdd or deduplication.
I am partly in favor of deduplication as it will result in less runnable code and a smaller build output. We could also do both, then we're on the complete safe side.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For ordering, we already produce the code for compilation first then from other XML files right? In that case, this will already be correct as the first add call is going to win.

using System;
using System.Collections.Generic;

var x = new Dictionary<string, string>();
x.TryAdd("key", "val1");
x.TryAdd("key", "val2");

foreach (var (key, value) in x)
{
    Console.WriteLine($"{key} -> {value}");
}

// program prints key -> val1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It looks like it emits the xml comments first:
https://github.com/desjoerd/aspnetcore/blob/251854361046ac6c92f68d43f8b55cf777e760be/src/OpenApi/gen/XmlCommentGenerator.Emitter.cs#L88-L93

        private static Dictionary<string, XmlComment> GenerateCacheEntries()
        {
            var cache = new Dictionary<string, XmlComment>();
{{commentsFromXmlFile}}
{{commentsFromCompilation}}
            return cache;
        }

Its an easy re-order :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, right. I thought it was the other way around. There is no risk that I see with re-ordering here.

I'll discuss with the team to see if we want to switch to TryAdd here as a simple fix.

@Youssef1313 Youssef1313 removed the pr: pending author input For automation. Specifically separate from Needs: Author Feedback label Jun 3, 2026
input,
cancellationToken,
// Only include symbols that are accessible from the application assembly.
static (_, symbol) => symbol.IsAccessibleType());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if this can cause issues for symbols that are internal (IsAccessibleType is going to return false) and are consumed via IVT by the app and needs to have their doc comments populated.

Let's focus on the case of generics for the purpose of this PR without trying to fix more than the reported issue, and follow-up in a separate PR on follow-up fixes.

I guess for the case of generics (the issue reported) that only the change in the symbol visitor is needed?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also note that this PR might be backported to .NET 10, so it's better to reduce the risks associated with the change. So we can take the fix targeted for generics for .NET 10, but the follow-up fix might be .NET 11+ only.

@desjoerd desjoerd Jun 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This check is taken from removed line 124 of this file

(SymbolEqualityComparer.Default.Equals(symbol.ContainingAssembly, compilation.Assembly) || symbol.IsAccessibleType()) &&

This or has been split up in a targeted filter one for the files in the containing assembly and one for the xml comments.

If I would remove that it would change the behavior.

Maybe the distinct or the TryAdd solution you proposed is better than these targeted filters, but this filter logic is actually matching the current .NET 10 behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, but I think I can indeed split this PR into two, one for the generics and one for the symbol filter / de-duplication logic

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@desjoerd Let me know please if there is anything I can help with moving forward with the first step of only handling generics as part of this PR (and we can follow-up in a separate issue and PR for the other changes so we have full de-duplication)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've got some time now :).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Youssef1313 I've limited the fix to generics :). I also improved the unit test to assert a description of the schema and properties.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Let me know if you want me to create the re-ordering PR when this is merged or that you will be working on it yourself :).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@desjoerd It's best to have a new issue opened for that, let it flow through our triage process, and then discuss the exact fix. There were some pushback against my idea of TryAdd , so that might not be the approach we choose. The two alternatives then are:

  • deduplicating in some centralized place.
  • use your original approach.

For those, I would like to then understand if there are any behavioral differences between the two approaches.

But let's take the first step of having a new bug report for the scenario that remains broken .

@Youssef1313 Youssef1313 self-assigned this Jun 8, 2026
@desjoerd
desjoerd requested a review from Youssef1313 June 8, 2026 14:52
@Youssef1313

Copy link
Copy Markdown
Member

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

public GenericFoo<int> FooInt { get; set; }

/// <summary>
/// Method with GenericFoo<double> parameter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doc comment is malformed. I think Roslyn would attempt to parse <double> as a tag but it's not valid. That's why it's not appearing in the generated document as well. Can you fix that please?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah check!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

if (type.IsGenericType)
{
// Because type comments are based on the Unbound generic type, i.e. List`1, we want to use the unbound generic type
type = type.ConstructUnboundGenericType();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any reason not to use .OriginalDefinition? Any scenario where it could produce a different result?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to OriginalDefinition, this was me thinking in "Reflection".

@Youssef1313 Youssef1313 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@Youssef1313

Copy link
Copy Markdown
Member

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

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

9 participants