From 6ff187882b33e4117214893f83d195515bcfab42 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 6 May 2024 16:23:19 +0300 Subject: [PATCH] Fixes error in serializing payloads. --- CHANGELOG.md | 3 ++- src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs | 6 +++--- .../Writers/CSharp/CodePropertyWriterTests.cs | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98616c366a..4f0257559b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,8 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Aligns naming of sliced OpenAPI description generated by `plugin add` should be named `-openapi.json|yml` +- 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. +- 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). ## [1.14.0] - 2024-05-02 diff --git a/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs index 4b0baaf35c..4920ea7360 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs @@ -50,11 +50,11 @@ private void WritePropertyInternal(CodeProperty codeElement, LanguageWriter writ case CodePropertyKind.AdditionalData when backingStoreProperty != null: case CodePropertyKind.Custom when backingStoreProperty != null: var backingStoreKey = codeElement.WireName; - var nullableOp = !codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? "?" : string.Empty; - var nullableEx = codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? " ?? throw new InvalidOperationException(\"AdditionalData can not be null\")" : string.Empty; + var nullableOp = !codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? "?" : string.Empty; + var defaultPropertyValue = codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? " ?? new Dictionary()" : string.Empty; writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} {propertyType} {codeElement.Name.ToFirstCharacterUpperCase()} {{"); writer.IncreaseIndent(); - writer.WriteLine($"get {{ return {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Get<{propertyType}>(\"{backingStoreKey}\"){nullableEx}; }}"); + writer.WriteLine($"get {{ return {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Get<{propertyType}>(\"{backingStoreKey}\"){defaultPropertyValue}; }}"); writer.WriteLine($"set {{ {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Set(\"{backingStoreKey}\", value); }}"); writer.DecreaseIndent(); writer.WriteLine("}"); diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs index a1d78fd859..a12cd43520 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs @@ -113,7 +113,7 @@ public void MapsAdditionalDataPropertiesToBackingStore() property.Kind = CodePropertyKind.AdditionalData; writer.Write(property); var result = tw.ToString(); - Assert.Contains("get { return BackingStore.Get(\"propertyName\") ?? throw new InvalidOperationException(\"AdditionalData can not be null\"); }", result); + Assert.Contains("get { return BackingStore.Get(\"propertyName\") ?? new Dictionary(); }", result); Assert.Contains("set { BackingStore.Set(\"propertyName\", value);", result); } [Fact]