From e3a6703d30d341cefd8271bca1dc6568ad935e97 Mon Sep 17 00:00:00 2001 From: Samarth Asthana Date: Tue, 1 Aug 2023 18:56:40 +0000 Subject: [PATCH 1/3] Autogenerated header support for C# language --- src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs | 2 ++ src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs index 5c5a00108d..97d98bb1c2 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs @@ -8,6 +8,7 @@ namespace Kiota.Builder.Writers.CSharp; public class CodeClassDeclarationWriter : BaseElementWriter { + public static string AutoGenerationHeader => "// "; public CodeClassDeclarationWriter(CSharpConventionService conventionService) : base(conventionService) { } public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWriter writer) { @@ -15,6 +16,7 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit ArgumentNullException.ThrowIfNull(writer); if (codeElement.Parent?.Parent is CodeNamespace) { + writer.WriteLine(AutoGenerationHeader); codeElement.Usings .Where(x => (x.Declaration?.IsExternal ?? true) || !x.Declaration.Name.Equals(codeElement.Name, StringComparison.OrdinalIgnoreCase)) // needed for circular requests patterns like message folder .Select(static x => x.Declaration?.IsExternal ?? false ? diff --git a/src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs index e9cf88986c..fb679e0d4a 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs @@ -8,6 +8,7 @@ namespace Kiota.Builder.Writers.CSharp; public class CodeEnumWriter : BaseElementWriter { + public static string AutoGenerationHeader => "// "; public CodeEnumWriter(CSharpConventionService conventionService) : base(conventionService) { } public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter writer) { @@ -19,6 +20,7 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write var codeNamespace = codeElement.Parent as CodeNamespace; if (codeNamespace != null) { + writer.WriteLine(AutoGenerationHeader); foreach (var x in codeElement.Usings .Where(static x => x.Declaration?.IsExternal ?? true) .Select(static x => $"using {(x.Declaration?.Name ?? x.Name).NormalizeNameSpaceName(".")};") From 1795579c42b1eb2cb0b3f9299b2d5463b03bc868 Mon Sep 17 00:00:00 2001 From: Samarth Asthana Date: Tue, 1 Aug 2023 19:23:12 +0000 Subject: [PATCH 2/3] Added tests to enforce header behavior --- .../CSharp/CodeClassDeclarationWriterTests.cs | 14 ++++++++++++++ .../Writers/CSharp/CodeEnumWriterTests.cs | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassDeclarationWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassDeclarationWriterTests.cs index 8aae979cf0..eb79ea470d 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassDeclarationWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassDeclarationWriterTests.cs @@ -10,6 +10,7 @@ namespace Kiota.Builder.Tests.Writers.CSharp; public class CodeClassDeclarationWriterTests : IDisposable { + private const string AutoGenerationHeader = "// "; private const string DefaultPath = "./"; private const string DefaultName = "name"; private readonly StringWriter tw; @@ -30,11 +31,21 @@ public CodeClassDeclarationWriterTests() }; root.AddClass(parentClass); } + public void Dispose() { tw?.Dispose(); GC.SuppressFinalize(this); } + + [Fact] + public void WritesAutoGeneratedHeader() + { + codeElementWriter.WriteCodeElement(parentClass.StartBlock, writer); + var result = tw.ToString(); + Assert.Contains(AutoGenerationHeader, result); + } + [Fact] public void WritesSimpleDeclaration() { @@ -42,6 +53,7 @@ public void WritesSimpleDeclaration() var result = tw.ToString(); Assert.Contains("public class", result); } + [Fact] public void WritesImplementation() { @@ -54,6 +66,7 @@ public void WritesImplementation() var result = tw.ToString(); Assert.Contains(":", result); } + [Fact] public void WritesInheritance() { @@ -66,6 +79,7 @@ public void WritesInheritance() var result = tw.ToString(); Assert.Contains(":", result); } + [Fact] public void WritesImports() { diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeEnumWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeEnumWriterTests.cs index 6e70c0d253..d51469fe64 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeEnumWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeEnumWriterTests.cs @@ -11,6 +11,7 @@ namespace Kiota.Builder.Tests.Writers.CSharp; public class CodeEnumWriterTests : IDisposable { + private const string AutoGenerationHeader = "// "; private const string DefaultPath = "./"; private const string DefaultName = "name"; private readonly StringWriter tw; @@ -37,6 +38,7 @@ public void Dispose() tw?.Dispose(); GC.SuppressFinalize(this); } + [Fact] public void WritesEnum() { @@ -48,6 +50,15 @@ public void WritesEnum() Assert.Contains(Option.Name, result); } + [Fact] + public void WritesAutoGeneratedHeader() + { + currentEnum.AddOption(Option); + writer.Write(currentEnum); + var result = tw.ToString(); + Assert.Contains(AutoGenerationHeader, result); + } + [Fact] public void NamesDiffer_WritesEnumMember() { From 883145e2d9f3d58b7c080da8336b69448218d9c0 Mon Sep 17 00:00:00 2001 From: Samarth Asthana Date: Tue, 1 Aug 2023 19:26:43 +0000 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdbbfbd8ad..e85f6e2a12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added the title of the API in the VSCode tree view. [#2779](https://github.com/microsoft/kiota/issues/2779) - Added capability to serialize and deserialize UUIDs in typescript[#40](https://github.com/microsoft/kiota-typescript/issues/40) - Use `import type` statement if the generated code is an interface[#2959](https://github.com/microsoft/kiota/issues/2959) +- Added auto-generation header for class and enums in CSharp [#2886](https://github.com/microsoft/kiota/issues/2886) ### Changed