From 28d922bfd3098ed87939a57ac3b963b8706ad2f9 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Mon, 6 Jan 2025 15:27:54 -0500 Subject: [PATCH] fix: nullability information Signed-off-by: Vincent Biret --- CHANGELOG.md | 2 + src/abstractions/serialization/IParseNode.cs | 2 +- src/serialization/json/JsonParseNode.cs | 73 +++++--------------- 3 files changed, 20 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab3a171..4146a734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Fixed a misalignment in return nullability for IParseNode GetObjectValue. [#429](https://github.com/microsoft/kiota-dotnet/issues/429) + ## [1.16.1] - 2024-12-18 ### Changed diff --git a/src/abstractions/serialization/IParseNode.cs b/src/abstractions/serialization/IParseNode.cs index ef107f67..d93c2231 100644 --- a/src/abstractions/serialization/IParseNode.cs +++ b/src/abstractions/serialization/IParseNode.cs @@ -129,7 +129,7 @@ public interface IParseNode /// /// The factory to use to create the model object. /// The model object value of the node. - T? GetObjectValue(ParsableFactory factory) where T : IParsable; + T GetObjectValue(ParsableFactory factory) where T : IParsable; /// /// Callback called before the node is deserialized. /// diff --git a/src/serialization/json/JsonParseNode.cs b/src/serialization/json/JsonParseNode.cs index f8a8b6c9..857eafd2 100644 --- a/src/serialization/json/JsonParseNode.cs +++ b/src/serialization/json/JsonParseNode.cs @@ -276,7 +276,7 @@ public IEnumerable GetCollectionOfObjectValues(ParsableFactory factory) /// Gets the untyped value of the node /// /// The untyped value of the node. - private UntypedNode? GetUntypedValue() => GetUntypedValue(_jsonNode); + private UntypedNode GetUntypedValue() => GetUntypedValue(_jsonNode); /// @@ -350,7 +350,7 @@ private IEnumerable GetCollectionOfUntypedValues(JsonElement jsonNo OnBeforeAssignFieldValues = OnBeforeAssignFieldValues, OnAfterAssignFieldValues = OnAfterAssignFieldValues }; - yield return currentParseNode.GetUntypedValue()!; + yield return currentParseNode.GetUntypedValue(); } } } @@ -379,66 +379,27 @@ private IDictionary GetPropertiesOfUntypedObject(JsonElemen } else { - properties[objectValue.Name] = GetUntypedValue(property)!; + properties[objectValue.Name] = GetUntypedValue(property); } } } return properties; } - private UntypedNode? GetUntypedValue(JsonElement jsonNode) + private UntypedNode GetUntypedValue(JsonElement jsonNode) => jsonNode.ValueKind switch { - UntypedNode? untypedNode = null; - switch(jsonNode.ValueKind) - { - case JsonValueKind.Number: - if(jsonNode.TryGetInt32(out var intValue)) - { - untypedNode = new UntypedInteger(intValue); - } - else if(jsonNode.TryGetInt64(out var longValue)) - { - untypedNode = new UntypedLong(longValue); - } - else if(jsonNode.TryGetDecimal(out var decimalValue)) - { - untypedNode = new UntypedDecimal(decimalValue); - } - else if(jsonNode.TryGetSingle(out var floatValue)) - { - untypedNode = new UntypedFloat(floatValue); - } - else if(jsonNode.TryGetDouble(out var doubleValue)) - { - untypedNode = new UntypedDouble(doubleValue); - } - else throw new InvalidOperationException("unexpected additional value type during number deserialization"); - break; - case JsonValueKind.String: - var stringValue = jsonNode.GetString(); - untypedNode = new UntypedString(stringValue); - break; - case JsonValueKind.True: - case JsonValueKind.False: - var boolValue = jsonNode.GetBoolean(); - untypedNode = new UntypedBoolean(boolValue); - break; - case JsonValueKind.Array: - var arrayValue = GetCollectionOfUntypedValues(jsonNode); - untypedNode = new UntypedArray(arrayValue); - break; - case JsonValueKind.Object: - var objectValue = GetPropertiesOfUntypedObject(jsonNode); - untypedNode = new UntypedObject(objectValue); - break; - case JsonValueKind.Null: - case JsonValueKind.Undefined: - untypedNode = new UntypedNull(); - break; - } - - return untypedNode; - } + JsonValueKind.Number when jsonNode.TryGetInt32(out var intValue) => new UntypedInteger(intValue), + JsonValueKind.Number when jsonNode.TryGetInt64(out var longValue) => new UntypedLong(longValue), + JsonValueKind.Number when jsonNode.TryGetDecimal(out var decimalValue) => new UntypedDecimal(decimalValue), + JsonValueKind.Number when jsonNode.TryGetSingle(out var floatValue) => new UntypedFloat(floatValue), + JsonValueKind.Number when jsonNode.TryGetDouble(out var doubleValue) => new UntypedDouble(doubleValue), + JsonValueKind.String => new UntypedString(jsonNode.GetString()), + JsonValueKind.True or JsonValueKind.False => new UntypedBoolean(jsonNode.GetBoolean()), + JsonValueKind.Array => new UntypedArray(GetCollectionOfUntypedValues(jsonNode)), + JsonValueKind.Object => new UntypedObject(GetPropertiesOfUntypedObject(jsonNode)), + JsonValueKind.Null or JsonValueKind.Undefined => new UntypedNull(), + _ => throw new InvalidOperationException($"unexpected additional value type during deserialization json kind : {jsonNode.ValueKind}") + }; /// /// The action to perform before assigning field values. @@ -461,7 +422,7 @@ public T GetObjectValue(ParsableFactory factory) where T : IParsable var genericType = typeof(T); if(genericType == typeof(UntypedNode)) { - return (T)(object)GetUntypedValue()!; + return (T)(object)GetUntypedValue(); } var item = factory(this); OnBeforeAssignFieldValues?.Invoke(item);