Skip to content
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

Autogenerated header support for C# language #3034

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/>";
samarthasthana marked this conversation as resolved.
Show resolved Hide resolved
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
Loading