Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nullability information #503

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading