Skip to content

Commit

Permalink
Unconditionally generate C# classes with nullable reference types
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Dec 21, 2023
1 parent 5103657 commit e862299
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 36 deletions.
17 changes: 0 additions & 17 deletions src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 NullableHeader => "#nullable enable";
public CodeClassDeclarationWriter(CSharpConventionService conventionService) : base(conventionService) { }
public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWriter writer)
{
Expand All @@ -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 ?
Expand Down
12 changes: 4 additions & 8 deletions src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 1 addition & 11 deletions src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e862299

Please sign in to comment.