Skip to content

Commit

Permalink
Merge pull request #4924 from martincostello/gh-4907-generatedcode-at…
Browse files Browse the repository at this point in the history
…tribute

Emit [GeneratedCode] attribute for C# types
  • Loading branch information
andrueastman authored Jul 3, 2024
2 parents 59c637f + aee7fb3 commit f21a8a7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!-- markdownlint-disable-file MD024 -->

## [Unreleased]

### Added
Expand All @@ -22,6 +24,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 Expand Up @@ -880,7 +883,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Split parsable interface and additional property/data interface in Ruby. [#1654](https://github.com/microsoft/kiota/issues/1654)
- Changed format of datetimes in Go to be converted to ISO 8601 by default when place in path parameters(Go)
- Defined the Access Token Provider Interface for Ruby authentication. [#1638](https://github.com/microsoft/kiota/issues/1638)
- Reduce code verbosity on Go Getters and Setters. [G0#26][https://github.com/microsoftgraph/msgraph-sdk-go-core/issues/26]
- Reduce code verbosity on Go Getters and Setters. [G0#26](https://github.com/microsoftgraph/msgraph-sdk-go-core/issues/26)

## [0.3.0] - 2022-07-08

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\", \"{Kiota.Generated.KiotaVersion.Current()}\")]";
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\", \"{Kiota.Generated.KiotaVersion.Current()}\")]";
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]+\""\)\]";
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]+\""\)\]";
Assert.Matches(pattern, result);
}
}

0 comments on commit f21a8a7

Please sign in to comment.