From e8622991d463515e1496db48d9d29f4c961c19a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Thu, 21 Dec 2023 16:46:13 +0100 Subject: [PATCH] =?UTF-8?q?Unconditionally=20generate=20C#=C2=A0classes=20?= =?UTF-8?q?with=20nullable=20reference=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3944 --- .../Writers/CSharp/CSharpConventionService.cs | 17 ----------------- .../CSharp/CodeClassDeclarationWriter.cs | 2 ++ .../Writers/CSharp/CodeMethodWriter.cs | 12 ++++-------- .../Writers/CSharp/CodePropertyWriter.cs | 12 +----------- 4 files changed, 7 insertions(+), 36 deletions(-) diff --git a/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs b/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs index f7197d2894..09ecb6bcf4 100644 --- a/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs +++ b/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs @@ -19,23 +19,6 @@ public class CSharpConventionService : CommonLanguageConventionService public static string NullableMarkerAsString => "?"; public override string ParseNodeInterfaceName => "IParseNode"; - public static void WriteNullableOpening(LanguageWriter writer) - { - ArgumentNullException.ThrowIfNull(writer); - writer.WriteLine($"#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER", false); - writer.WriteLine($"#nullable enable", false); - } - public static void WriteNullableMiddle(LanguageWriter writer) - { - ArgumentNullException.ThrowIfNull(writer); - writer.WriteLine($"#nullable restore", false); - writer.WriteLine("#else", false); - } - public static void WriteNullableClosing(LanguageWriter writer) - { - ArgumentNullException.ThrowIfNull(writer); - writer.WriteLine("#endif", false); - } public override void WriteShortDescription(string description, LanguageWriter writer) { ArgumentNullException.ThrowIfNull(writer); diff --git a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs index cf1c4d97da..72e328f7ae 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs @@ -7,6 +7,7 @@ namespace Kiota.Builder.Writers.CSharp; public class CodeClassDeclarationWriter : BaseElementWriter { public static string AutoGenerationHeader => "// "; + public static string NullableHeader => "#nullable enable"; public CodeClassDeclarationWriter(CSharpConventionService conventionService) : base(conventionService) { } public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWriter writer) { @@ -17,6 +18,7 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit if (codeElement.Parent?.Parent is CodeNamespace) { writer.WriteLine(AutoGenerationHeader); + writer.WriteLine(NullableHeader); 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/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs index 8ecebe7690..fb6f4e2668 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs @@ -611,16 +611,12 @@ private void WriteMethodPrototype(CodeMethod code, CodeClass parentClass, Langua GetParameterSignatureWithNullableRefType(p, code) : conventions.GetParameterSignature(p, code)) .ToList()); - CSharpConventionService.WriteNullableOpening(writer); writer.WriteLine($"{conventions.GetAccessModifier(code.Access)} {staticModifier}{hideModifier}{completeReturnTypeWithNullable}{methodName}({nullableParameters}){baseSuffix} {{"); - CSharpConventionService.WriteNullableMiddle(writer); } - - writer.WriteLine($"{conventions.GetAccessModifier(code.Access)} {staticModifier}{hideModifier}{completeReturnType}{methodName}({parameters}){baseSuffix} {{"); - - if (includeNullableReferenceType) - CSharpConventionService.WriteNullableClosing(writer); - + else + { + writer.WriteLine($"{conventions.GetAccessModifier(code.Access)} {staticModifier}{hideModifier}{completeReturnType}{methodName}({parameters}){baseSuffix} {{"); + } } private string GetParameterSignatureWithNullableRefType(CodeParameter parameter, CodeElement targetElement) diff --git a/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs index 48fef8fa98..04a30f0ceb 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs @@ -18,17 +18,7 @@ public override void WriteCodeElement(CodeProperty codeElement, LanguageWriter w CodePropertyKind.QueryParameter);// Other property types are appropriately constructor initialized conventions.WriteShortDescription(codeElement.Documentation.Description, writer); conventions.WriteDeprecationAttribute(codeElement, writer); - if (isNullableReferenceType) - { - CSharpConventionService.WriteNullableOpening(writer); - WritePropertyInternal(codeElement, writer, $"{propertyType}?"); - CSharpConventionService.WriteNullableMiddle(writer); - } - - WritePropertyInternal(codeElement, writer, propertyType);// Always write the normal way - - if (isNullableReferenceType) - CSharpConventionService.WriteNullableClosing(writer); + WritePropertyInternal(codeElement, writer, isNullableReferenceType ? $"{propertyType}?" : propertyType); } private void WritePropertyInternal(CodeProperty codeElement, LanguageWriter writer, string propertyType)