Skip to content

Commit

Permalink
Emit [GeneratedCode] attribute for C# types
Browse files Browse the repository at this point in the history
Emit `[GeneratedCode]` attribute for C# types.
Resolves #4907.
  • Loading branch information
martincostello committed Jul 2, 2024
1 parent 59c637f commit 09a9d48
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- References to C# types generated by kiota are prefixed with `global::` to avoid name collisions. [#4796](https://github.com/microsoft/kiota/issues/4796)
- Ensures HashSet properties in `KiotaLock` maintain IgnoreCase comparer across runs [#4916](https://github.com/microsoft/kiota/issues/4916)
- Dropped `client base url set to` message when generating plugins. [#4905](https://github.com/microsoft/kiota/issues/4905)
- Emit `[GeneratedCode]` attribute for C# types. [#4907](https://github.com/microsoft/kiota/issues/4907)

## [1.15.0] - 2024-06-06

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Kiota.Builder.Writers.CSharp;
public class CodeClassDeclarationWriter : BaseElementWriter<ClassDeclaration, CSharpConventionService>
{
public static string AutoGenerationHeader => "// <auto-generated/>";
public static string GeneratedCodeAttribute { get; } = $"[global::System.CodeDom.Compiler.GeneratedCode(\"Kiota\", \"{typeof(CodeClassDeclarationWriter).Assembly.GetName().Version}\")]";
public CodeClassDeclarationWriter(CSharpConventionService conventionService) : base(conventionService) { }
public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWriter writer)
{
Expand Down Expand Up @@ -37,6 +38,7 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
var derivation = derivedTypes.Length != 0 ? ": " + derivedTypes.Aggregate(static (x, y) => $"{x}, {y}") : string.Empty;
bool hasDescription = conventions.WriteLongDescription(parentClass, writer);
conventions.WriteDeprecationAttribute(parentClass, writer);
writer.WriteLine(GeneratedCodeAttribute);
if (!hasDescription) writer.WriteLine("#pragma warning disable CS1591");
writer.WriteLine($"public partial class {codeElement.Name.ToFirstCharacterUpperCase()} {derivation}");
if (!hasDescription) writer.WriteLine("#pragma warning restore CS1591");
Expand Down
2 changes: 2 additions & 0 deletions src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Kiota.Builder.Writers.CSharp;
public class CodeEnumWriter : BaseElementWriter<CodeEnum, CSharpConventionService>
{
public static string AutoGenerationHeader => "// <auto-generated/>";
public static string GeneratedCodeAttribute { get; } = $"[global::System.CodeDom.Compiler.GeneratedCode(\"Kiota\", \"{typeof(CodeClassDeclarationWriter).Assembly.GetName().Version}\")]";
public CodeEnumWriter(CSharpConventionService conventionService) : base(conventionService) { }
public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter writer)
{
Expand All @@ -30,6 +31,7 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
writer.WriteLine($"namespace {codeNamespace.Name}");
writer.StartBlock();
}
writer.WriteLine(GeneratedCodeAttribute);
bool hasDescription = conventions.WriteShortDescription(codeElement, writer);
if (codeElement.Flags)
writer.WriteLine("[Flags]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,13 @@ public void WritesImports()
Assert.Contains("Project.Graph", result);
Assert.Contains("System.Util", result);
}

[Fact]
public void WritesGeneratedCodeAttribute()
{
codeElementWriter.WriteCodeElement(parentClass.StartBlock, writer);
var result = tw.ToString();
var pattern = @"\s+\[global::System\.CodeDom\.Compiler\.GeneratedCode\(""Kiota"", ""[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+""\)\]";
Assert.Matches(pattern, result);
}
}
12 changes: 12 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/CSharp/CodeEnumWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void WritesFlagsEnum()
Assert.Contains("= 1", result);
Assert.Contains("= 2", result);
}

[Fact]
public void WritesEnumOptionDescription()
{
Expand All @@ -120,11 +121,22 @@ public void WritesEnumOptionDescription()
Assert.Contains($"<summary>{Option.Documentation.DescriptionTemplate}</summary>", result);
AssertExtensions.CurlyBracesAreClosed(result, 1);
}

[Fact]
public void DoesntWriteAnythingOnNoOption()
{
writer.Write(currentEnum);
var result = tw.ToString();
Assert.Empty(result);
}

[Fact]
public void WritesGeneratedCodeAttribute()
{
currentEnum.AddOption(new CodeEnumOption { Name = "option2" });
writer.Write(currentEnum);
var result = tw.ToString();
var pattern = @"\s+\[global::System\.CodeDom\.Compiler\.GeneratedCode\(""Kiota"", ""[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+""\)\]";
Assert.Matches(pattern, result);
}
}

0 comments on commit 09a9d48

Please sign in to comment.