Skip to content

[OpenApi] SourceGen error using xml docs for Static methods taking generic parameters. #61864

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

Closed
1 task done
jasonswearingen opened this issue May 10, 2025 · 8 comments
Closed
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.

Comments

@jasonswearingen
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

it seems that a static class with Generic methods causes errors with the OpenApi sourcegen.

If I change the library's .csproj to include: <GenerateDocumentationFile>false</GenerateDocumentationFile> the error disappears.

a library referenced by my aspnetcore api has:

/// <summary>
///    helpers to allocate a WriteMem instance
/// </summary>
public static class Mem
{
   /// <summary>
   ///    Create a temporary (no-pooled) mem using your own backing data object
   /// </summary>
   public static Mem<T> CreateUsing<T>(T[] array)
   {
      return Mem<T>.CreateUsing(array);
   }
}

causes the following line to be generated in .Generated\Microsoft.AspNetCore.OpenApi.SourceGenerators\Microsoft.AspNetCore.OpenApi.SourceGenerators.XmlCommentGenerator\OpenApiXmlCommentSupport.generated.cs

_cache.Add(new MemberKey(typeof(global::NotNot.Mem), MemberType.Method, "CreateUsing", typeof(global::NotNot.Mem<>), [typeof(T[])]), new XmlComment(@"Create a temporary (no-pooled) mem using your own backing data object.", null, null, null, null, false, null, null, null));

which triggers the following error in build output:

1>V:\project\path\.Generated\Microsoft.AspNetCore.OpenApi.SourceGenerators\Microsoft.AspNetCore.OpenApi.SourceGenerators.XmlCommentGenerator\OpenApiXmlCommentSupport.generated.cs(599,138,599,139): error CS0246: The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

Expected Behavior

see above

Steps To Reproduce

see above

Exceptions (if any)

see above

.NET Version

10.0.0-preview.3.25172.1

Anything else?

Microsoft.AspNetCore.OpenApi v10.0.0-preview.3.25172.1

@github-actions github-actions bot added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label May 10, 2025
@jasonswearingen
Copy link
Author

jasonswearingen commented May 10, 2025

also, related workaround: if there is some way I can remove the problematic assembly from openapi doc gen (so I don't have to stop generating xml docs for it), or even better, remove problematic types individually, it would be great to hear. I found the doc pages on this openapi feature to be very good ( https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/openapi-tools?view=aspnetcore-10.0 ), but they don't go over the .AddOpenApi() options, and I couldn't figure out a graceful way to configure options for this either.

@martincostello
Copy link
Member

@jasonswearingen
Copy link
Author

Thanks, @martincostello. I saw that, but don't think it's exactly what I want. I think it's for disabling the sourcegen entirely? To clarify, I don't want to disable the source generation or use of xml docs, just disable use of certain docs or certain types in the docs.

@martincostello
Copy link
Member

Oh I see, sorry I misunderstood. At this point I'd maybe wait until preview.4 which has a bunch of fixes for various issues, and then see if the problem you're having that makes you want to suppress some things persists.

@jasonswearingen
Copy link
Author

it's alright, i will just disable the problematic assembly's xml doc for now. I just wanted to bring this to your attention. The OpenApi solution you all are making is so much better than the legacy choices.

Thank you.

@captainsafia
Copy link
Member

@jasonswearingen .NET 10 Preview 4 is out as of an hour ago (see https://devblogs.microsoft.com/dotnet/dotnet-10-preview-4/) which includes this PR I opened a while back (#61145).

It contains a bug fix that moves to string-based keys instead of using typeof() which should reduce the chance of build errors being introduced by the source generator.

Can you try it and report back?

@captainsafia captainsafia added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label May 13, 2025
@jasonswearingen
Copy link
Author

@captainsafia I'm sorry I was required to move back to net9 due to azure compatibility issues.

you all should include this in your test cases, it is very straight forward (see the first post in this issue for a snippet)

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Status: No Recent Activity labels May 19, 2025
@captainsafia
Copy link
Member

We included test cases as part of the Preview 4. Closing this as resolved for now.

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 feature-openapi Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
None yet
Development

No branches or pull requests

3 participants