diff --git a/CHANGELOG.md b/CHANGELOG.md index 870737341b..bbc3658e93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Changed URI template generation to reuse templates when required templates are absent across operations. - Updated reserved name providers for Java and Php so that "object" can be escaped. +- Do not generate CS8603 warnings when enabling backing store in CSharp generation. ## [1.13.0] - 2024-04-04 diff --git a/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs index 3f00038af6..4b0baaf35c 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs @@ -50,10 +50,12 @@ 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; writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} {propertyType} {codeElement.Name.ToFirstCharacterUpperCase()} {{"); writer.IncreaseIndent(); - writer.WriteLine($"get {{ return {backingStoreProperty.Name.ToFirstCharacterUpperCase()}?.Get<{propertyType}>(\"{backingStoreKey}\"); }}"); - writer.WriteLine($"set {{ {backingStoreProperty.Name.ToFirstCharacterUpperCase()}?.Set(\"{backingStoreKey}\", value); }}"); + writer.WriteLine($"get {{ return {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Get<{propertyType}>(\"{backingStoreKey}\"){nullableEx}; }}"); + writer.WriteLine($"set {{ {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Set(\"{backingStoreKey}\", value); }}"); writer.DecreaseIndent(); writer.WriteLine("}"); break; diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs index 27516fbfd2..a1d78fd859 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs @@ -113,8 +113,8 @@ public void MapsAdditionalDataPropertiesToBackingStore() property.Kind = CodePropertyKind.AdditionalData; writer.Write(property); var result = tw.ToString(); - Assert.Contains("get { return BackingStore?.Get(\"propertyName\"); }", result); - Assert.Contains("set { BackingStore?.Set(\"propertyName\", value);", result); + Assert.Contains("get { return BackingStore.Get(\"propertyName\") ?? throw new InvalidOperationException(\"AdditionalData can not be null\"); }", result); + Assert.Contains("set { BackingStore.Set(\"propertyName\", value);", result); } [Fact] public void WritesSerializationAttribute()