Repository navigation
OpenAPI: Fix duplicate xml documentation ids for Generic properties and references - #64404
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes duplicate XML documentation IDs in OpenAPI source generation by addressing two specific issues:
- Generic type properties with different type arguments (e.g.,
GenericFoo<string>vsGenericFoo<int>) were generating duplicate documentation IDs - 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
AssemblyTypeSymbolsVisitorto 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 |
806098f to
f8931ce
Compare
|
@desjoerd any progress on this PR? |
|
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! |
|
Note for us: when merging this, we need to backport this to 10.0. |
…internal xml comments
|
@Youssef1313 I've rebased and I think I've addressed your comments. Could you please review again :). |
| return comments; | ||
| } | ||
|
|
||
| internal static IEnumerable<(string, XmlComment?)> ParseComments( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
| input, | ||
| cancellationToken, | ||
| // Only include symbols that are accessible from the application assembly. | ||
| static (_, symbol) => symbol.IsAccessibleType()); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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)
There was a problem hiding this comment.
I've got some time now :).
There was a problem hiding this comment.
@Youssef1313 I've limited the fix to generics :). I also improved the unit test to assert a description of the schema and properties.
There was a problem hiding this comment.
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 :).
There was a problem hiding this comment.
@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 .
|
/backport to release/10.0 |
|
Started backporting to |
| public GenericFoo<int> FooInt { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Method with GenericFoo<double> parameter. |
There was a problem hiding this comment.
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?
| 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(); |
There was a problem hiding this comment.
Any reason not to use .OriginalDefinition? Any scenario where it could produce a different result?
There was a problem hiding this comment.
Changed to OriginalDefinition, this was me thinking in "Reflection".
…x comment in test
|
/backport to release/10.0 |
|
Started backporting to |
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