Skip to content

Commit

Permalink
Merge pull request #503 from microsoft/fix/parse-node-object-value-nu…
Browse files Browse the repository at this point in the history
…llable

fix: nullability information
  • Loading branch information
baywet authored Jan 7, 2025
2 parents 4062fb2 + 28d922b commit e88a174
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 57 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/abstractions/serialization/IParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public interface IParseNode
/// </summary>
/// <param name="factory">The factory to use to create the model object.</param>
/// <returns>The model object value of the node.</returns>
T? GetObjectValue<T>(ParsableFactory<T> factory) where T : IParsable;
T GetObjectValue<T>(ParsableFactory<T> factory) where T : IParsable;
/// <summary>
/// Callback called before the node is deserialized.
/// </summary>
Expand Down
73 changes: 17 additions & 56 deletions src/serialization/json/JsonParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public IEnumerable<T> GetCollectionOfObjectValues<T>(ParsableFactory<T> factory)
/// Gets the untyped value of the node
/// </summary>
/// <returns>The untyped value of the node.</returns>
private UntypedNode? GetUntypedValue() => GetUntypedValue(_jsonNode);
private UntypedNode GetUntypedValue() => GetUntypedValue(_jsonNode);


/// <summary>
Expand Down Expand Up @@ -350,7 +350,7 @@ private IEnumerable<UntypedNode> GetCollectionOfUntypedValues(JsonElement jsonNo
OnBeforeAssignFieldValues = OnBeforeAssignFieldValues,
OnAfterAssignFieldValues = OnAfterAssignFieldValues
};
yield return currentParseNode.GetUntypedValue()!;
yield return currentParseNode.GetUntypedValue();
}
}
}
Expand Down Expand Up @@ -379,66 +379,27 @@ private IDictionary<string, UntypedNode> 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}")
};

/// <summary>
/// The action to perform before assigning field values.
Expand All @@ -461,7 +422,7 @@ public T GetObjectValue<T>(ParsableFactory<T> 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);
Expand Down

0 comments on commit e88a174

Please sign in to comment.