microsoft / kiota

OpenAPI based HTTP Client code generator
https://aka.ms/kiota/docs
MIT License
2.92k stars 203 forks source link

Emit [GeneratedCode] for C# code generation #4907

Closed martincostello closed 3 months ago

martincostello commented 3 months ago

Is your feature request related to a problem? Please describe the problem.

When generating C# client code with Kiota, the classes produced are not annotated with the [GeneratedCode] attribute.

While // <auto-generated/> is added to the source files, which is useful for tools like style analyzers, this is not visible to static analysis tools that work with compiled binaries.

For example, it is common to exclude generated code from tools such as code coverage tools, as often the coverage of such code is not considered interesting when determining test suite coverage. Many tools such as coverlet support excluding code marked with specific attributes, such as [ExcludeFromCodeCoverage] or [GeneratedCode], as these are easy to blanket-apply, rather than requiring types be excluded by specific names/namespaces.

See here for a similar feature request for source-generated code in the .NET runtime: https://github.com/dotnet/runtime/issues/89007

As well as the specific motivating factor of easier code coverage tooling exclusions, such annotations would also be useful for other static analysis that may wish to specifically find code that has been generated by Kiota as opposed to user-written.

Client library/SDK language

Csharp

Describe the solution you'd like

All C# types generated by Kiota are decorated with [GeneratedCode("{tool}", "{version}")].

For example:

+ [System.CodeDom.Compiler.GeneratedCode("kiota", "1.15.0")]
  public class ApiClient : BaseRequestBuilder
  {
  }

Additional context

No response

andrueastman commented 3 months ago

I think this is a valid suggestion. It's great that the attribute is part of .NET, Any objections @baywet?

Essentially, what we'd probably need to do is very similar to the PR at https://github.com/microsoft/kiota/pull/3034/files as well as add usings for System.CodeDom.Compiler (if we don't use the Fully qualified names) similar to

https://github.com/microsoft/kiota/blob/edaa755c3b492217e79446d7618edb83ca31c060/src/Kiota.Builder/Refiners/CSharpRefiner.cs#L163

baywet commented 3 months ago

No objections on my side. For some reason I had not caught the subtility of using a doc comment versus using an attribute for the static analysis options. I think adding this attribute will be a great improvement for tools like sonarCloud or others which work on compiled static analysis. @martincostello are you willing to submit a pull request for that?

martincostello commented 3 months ago

Sure - I was just waiting for an agreement it would be a wanted change before starting 😄

martincostello commented 3 months ago

4924