From ff02af95ad7362d96588cec0ad0a545ff648a8c2 Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Sat, 24 Aug 2024 00:58:56 +0200 Subject: [PATCH 1/4] Source generated models should not scream on CS0618 --- src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs index e02a5a5f52..45305f99bb 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs @@ -18,6 +18,7 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit if (codeElement.Parent?.Parent is CodeNamespace) { writer.WriteLine(AutoGenerationHeader); + writer.WriteLine("#pragma warning disable CS0618"); 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 ? From 7b317979552c7ffcb40f754e9676bae5a757628c Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Mon, 26 Aug 2024 13:21:43 +0200 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61d59f346a..e3b2fe62fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Expand properties types with null type for Typescript. [#4993](https://github.com/microsoft/kiota-typescript/issues/1188) - Added Collection, HashMap, Map, Objects, InputStream, BigDecimal to the list of reserved names for Java generation. [#5135](https://github.com/microsoft/kiota/issues/5135) - C# refiner now fixes data types for indexers. [#5201](https://github.com/microsoft/kiota/issues/5201) +- C# do not report CS0618 in the generated code. [#5229](https://github.com/microsoft/kiota/issues/5229) ## [1.17.0] - 2024-08-09 From e519975eb3863e294a9af4cf4bb97e5c3311dc98 Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Mon, 26 Aug 2024 13:31:47 +0200 Subject: [PATCH 3/4] Add WritePragmaDisable, WritePragmaRestore --- .../Writers/CSharp/CSharpConventionService.cs | 13 +++++++++++++ .../Writers/CSharp/CodeClassDeclarationWriter.cs | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs b/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs index 9522fa678e..5037cfe8d7 100644 --- a/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs +++ b/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs @@ -21,6 +21,9 @@ public class CSharpConventionService : CommonLanguageConventionService public const string NullableEnableDirective = "#nullable enable"; public const string NullableRestoreDirective = "#nullable restore"; + public const string CS0618 = "CS0618"; + public const string CS1591 = "CS1591"; + public static void WriteNullableOpening(LanguageWriter writer) { ArgumentNullException.ThrowIfNull(writer); @@ -38,6 +41,16 @@ public static void WriteNullableClosing(LanguageWriter writer) ArgumentNullException.ThrowIfNull(writer); writer.WriteLine("#endif", false); } + public void WritePragmaDisable(LanguageWriter writer, string code) + { + ArgumentNullException.ThrowIfNull(writer); + writer.WriteLine($"#pragma warning disable {code}"); + } + public void WritePragmaRestore(LanguageWriter writer, string code) + { + ArgumentNullException.ThrowIfNull(writer); + writer.WriteLine($"#pragma warning restore {code}"); + } private const string ReferenceTypePrefix = ""; #pragma warning disable S1006 // Method overrides should not change parameter defaults diff --git a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs index 45305f99bb..f457430ec9 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs @@ -18,7 +18,7 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit if (codeElement.Parent?.Parent is CodeNamespace) { writer.WriteLine(AutoGenerationHeader); - writer.WriteLine("#pragma warning disable CS0618"); + conventions.WritePragmaDisable(writer, CSharpConventionService.CS0618); 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 ? @@ -40,9 +40,9 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit bool hasDescription = conventions.WriteLongDescription(parentClass, writer); conventions.WriteDeprecationAttribute(parentClass, writer); writer.WriteLine(GeneratedCodeAttribute); - if (!hasDescription) writer.WriteLine("#pragma warning disable CS1591"); + if (!hasDescription) conventions.WritePragmaDisable(writer, CSharpConventionService.CS1591); writer.WriteLine($"public partial class {codeElement.Name.ToFirstCharacterUpperCase()} {derivation}"); - if (!hasDescription) writer.WriteLine("#pragma warning restore CS1591"); + if (!hasDescription) conventions.WritePragmaRestore(writer, CSharpConventionService.CS1591); writer.StartBlock(); } } From df1010de2013494936312184e93baecceea04f9b Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Mon, 26 Aug 2024 13:32:21 +0200 Subject: [PATCH 4/4] Add WritesWarningDisableCs0618, WritesWarningRestoreCs0618 --- src/Kiota.Builder/Writers/CSharp/CodeBlockEndWriter.cs | 1 + .../Writers/CSharp/CodeClassDeclarationWriterTests.cs | 8 ++++++++ .../Writers/CSharp/CodeClassEndWriterTests.cs | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/src/Kiota.Builder/Writers/CSharp/CodeBlockEndWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeBlockEndWriter.cs index 0949f10633..eb4c37b125 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeBlockEndWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeBlockEndWriter.cs @@ -12,6 +12,7 @@ public override void WriteCodeElement(BlockEnd codeElement, LanguageWriter write if (codeElement?.Parent is CodeClass codeClass && codeClass.Parent is CodeNamespace) { writer.CloseBlock(); + conventions.WritePragmaRestore(writer, CSharpConventionService.CS0618); } } } diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassDeclarationWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassDeclarationWriterTests.cs index d59f8e58d5..20d0b3df68 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassDeclarationWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassDeclarationWriterTests.cs @@ -54,6 +54,14 @@ public void WritesSimpleDeclaration() Assert.Contains("public partial class", result); } + [Fact] + public void WritesWarningDisableCs0618() + { + codeElementWriter.WriteCodeElement(parentClass.StartBlock, writer); + var result = tw.ToString(); + Assert.Contains("#pragma warning disable CS0618", result); + } + [Fact] public void WritesImplementation() { diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassEndWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassEndWriterTests.cs index 1024e62d5b..1d8878380f 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassEndWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeClassEndWriterTests.cs @@ -45,6 +45,14 @@ public void ClosesNestedClasses() codeElementWriter.WriteCodeElement(child.EndBlock, writer); var result = tw.ToString(); Assert.Equal(1, result.Count(x => x == '}')); + Assert.DoesNotContain("#pragma warning restore CS0618", result); + } + [Fact] + public void WritesWarningRestoreCs0618() + { + codeElementWriter.WriteCodeElement(parentClass.EndBlock, writer); + var result = tw.ToString(); + Assert.Contains("#pragma warning restore CS0618", result); } [Fact] public void ClosesNonNestedClasses()