Skip to content

Commit

Permalink
Merge pull request #3034 from samarthasthana/sasthana/add-autogenerat…
Browse files Browse the repository at this point in the history
…ion-header

Autogenerated header support for C# language
  • Loading branch information
baywet authored Aug 1, 2023
2 parents 134dda0 + 883145e commit 594ae64
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
namespace Kiota.Builder.Writers.CSharp;
public class CodeClassDeclarationWriter : BaseElementWriter<ClassDeclaration, CSharpConventionService>
{
public static string AutoGenerationHeader => "// <auto-generated/>";
public CodeClassDeclarationWriter(CSharpConventionService conventionService) : base(conventionService) { }
public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWriter writer)
{
ArgumentNullException.ThrowIfNull(codeElement);
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 ?
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 @@ -8,6 +8,7 @@
namespace Kiota.Builder.Writers.CSharp;
public class CodeEnumWriter : BaseElementWriter<CodeEnum, CSharpConventionService>
{
public static string AutoGenerationHeader => "// <auto-generated/>";
public CodeEnumWriter(CSharpConventionService conventionService) : base(conventionService) { }
public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter writer)
{
Expand All @@ -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(".")};")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Kiota.Builder.Tests.Writers.CSharp;
public class CodeClassDeclarationWriterTests : IDisposable
{
private const string AutoGenerationHeader = "// <auto-generated/>";
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
Expand All @@ -30,18 +31,29 @@ 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()
{
codeElementWriter.WriteCodeElement(parentClass.StartBlock, writer);
var result = tw.ToString();
Assert.Contains("public class", result);
}

[Fact]
public void WritesImplementation()
{
Expand All @@ -54,6 +66,7 @@ public void WritesImplementation()
var result = tw.ToString();
Assert.Contains(":", result);
}

[Fact]
public void WritesInheritance()
{
Expand All @@ -66,6 +79,7 @@ public void WritesInheritance()
var result = tw.ToString();
Assert.Contains(":", result);
}

[Fact]
public void WritesImports()
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/CSharp/CodeEnumWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Kiota.Builder.Tests.Writers.CSharp;
public class CodeEnumWriterTests : IDisposable
{
private const string AutoGenerationHeader = "// <auto-generated/>";
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
Expand All @@ -37,6 +38,7 @@ public void Dispose()
tw?.Dispose();
GC.SuppressFinalize(this);
}

[Fact]
public void WritesEnum()
{
Expand All @@ -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()
{
Expand Down

0 comments on commit 594ae64

Please sign in to comment.