Skip to content
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

Fix generic type handling #612

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mentallabyrinth
Copy link

Love the direction Microsoft.AspNetCore.OpenApi.SourceGenerators is going, but there's a bug when extracting comments from types that support generics. The issue is in https://github.com/aspnet/AspLabs/blob/main/src/OpenApiXmlCommentGenerator/gen/XmlCommentGenerator.Parser.cs, specifically in these lines of code:

var typeInfo = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var typeInfo = property.ContainingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);

To fix this, I've added a new method ToNormalizedDisplayString to INamedTypeSymbolExtensions.cs. The method checks if the type is generic - if so, it constructs an unbounded generic type (which works well for the comment caching strategy) and uses that for the display name. Otherwise, it uses the general type:

public static string ToNormalizedDisplayString(this INamedTypeSymbol symbol, SymbolDisplayFormat? format = null)  
{  
    if (symbol.IsGenericType)  
    {        var genericType = symbol.ConstructUnboundGenericType();  
        return genericType.ToDisplayString(format);  
    }  
    return symbol.ToDisplayString(format);  
}

Looking forward to the general release. Thanks for your efforts!

* Added method `ToNormalizedDisplayString` to `INamedTypeSymbolExtensions` which handles both general and generic types
* Changed `XmlCommentGenerator` to account for generic types when evaluating the display name.
@mentallabyrinth
Copy link
Author

@dotnet-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant