diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fa099376b..1f6d7bfe89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Aligns naming of sliced OpenAPI description generated by `plugin add` should be named `-openapi.json|yml` [#4595](https://github.com/microsoft/kiota/issues/4595) - Fixed RPC server to respect the `KIOTA_CONFIG_PREVIEW` flag. +- Added missing nullable directives for CLI generation. - Fixed handling of nested arrays to be handled as `UntypedNode` instances [#4549](https://github.com/microsoft/kiota/issues/4549) - Fixed `InvalidOperationException` thrown when serializing IBacked models with no changes present in the additional data in dotnet [microsoftgraph/msgraph-sdk-dotnet#2471](https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2471). - Fixed `RequestConfiguration` Classes dropped in RequestBuilder methods in python [#4535](https://github.com/microsoft/kiota/issues/4535) diff --git a/src/Kiota.Builder/Writers/CLI/CliCodeMethodWriter.cs b/src/Kiota.Builder/Writers/CLI/CliCodeMethodWriter.cs index b651471c26..ebec472968 100644 --- a/src/Kiota.Builder/Writers/CLI/CliCodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/CLI/CliCodeMethodWriter.cs @@ -341,7 +341,9 @@ private void WriteCommandHandlerBodyOutput(LanguageWriter writer, in CodeMethod if (originalMethod.PagingInformation != null) { + writer.WriteLine(CSharpConventionService.NullableEnableDirective, false); writer.WriteLine($"IOutputFormatter? {formatterVar} = null;"); + writer.WriteLine(CSharpConventionService.NullableRestoreDirective, false); } if (originalMethod.ReturnType is CodeType type && conventions.GetTypeString(type, originalMethod) is { } typeString && !typeString.Equals("Stream", StringComparison.Ordinal)) diff --git a/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs b/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs index 62e01592b3..5a5c1309e8 100644 --- a/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs +++ b/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs @@ -18,17 +18,19 @@ public class CSharpConventionService : CommonLanguageConventionService public const char NullableMarker = '?'; public static string NullableMarkerAsString => "?"; public override string ParseNodeInterfaceName => "IParseNode"; + public const string NullableEnableDirective = "#nullable enable"; + public const string NullableRestoreDirective = "#nullable restore"; 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); + writer.WriteLine(NullableEnableDirective, false); } public static void WriteNullableMiddle(LanguageWriter writer) { ArgumentNullException.ThrowIfNull(writer); - writer.WriteLine($"#nullable restore", false); + writer.WriteLine(NullableRestoreDirective, false); writer.WriteLine("#else", false); } public static void WriteNullableClosing(LanguageWriter writer) diff --git a/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs index c211a7121c..ab5d455c24 100644 --- a/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs @@ -951,7 +951,9 @@ public void WritesExecutableCommandForPagedGetRequestModel() Assert.Contains("var pagingData = new PageLinkData(requestInfo, null, itemName: \"item\", nextLinkName: \"nextLink\");", result); Assert.Contains("var reqAdapter = invocationContext.GetRequestAdapter()", result); Assert.Contains("var pageResponse = await pagingService.GetPagedDataAsync((info, token) => reqAdapter.SendNoContentAsync(info, cancellationToken: token), pagingData, all, cancellationToken);", result); + Assert.Contains("#nullable enable", result); Assert.Contains("IOutputFormatter? formatter = null;", result); + Assert.Contains("#nullable restore", result); Assert.Contains("if (pageResponse?.StatusCode >= 200 && pageResponse?.StatusCode < 300) {", result); Assert.Contains("formatter = outputFormatterFactory.GetFormatter(output);", result); Assert.Contains("response = (response != Stream.Null) ? await outputFilter.FilterOutputAsync(response, query, cancellationToken) : response", result);