Skip to content

Commit

Permalink
Merge branch 'main' into feature/175-untyped-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrueastman authored Feb 27, 2024
2 parents cb9b2cd + 972be8b commit 55af16f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added support for untyped nodes. (https://github.com/microsoft/kiota-abstractions-dotnet/issues/175)

## [1.7.11] - 2024-02-26

### Changed

- Updated IParseNode enum methods `DynamicallyAccessedMembersAttribute` to `PublicFields`.
- Fixed AOT compiler warnings from ILC.

## [1.7.10] - 2024-02-26

### Changed
Expand Down
62 changes: 33 additions & 29 deletions src/RequestInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public RequestInformation(Method method, string urlTemplate, IDictionary<string,
public void Configure<T>(Action<RequestConfiguration<T>>? requestConfiguration) where T : class, new()
#endif
{
if(requestConfiguration == null) return;
if (requestConfiguration == null) return;
var requestConfig = new RequestConfiguration<T>();
requestConfiguration(requestConfig);
AddQueryParameters(requestConfig.QueryParameters);
Expand All @@ -69,36 +69,36 @@ public Uri URI
{
set
{
if(value == null)
if (value == null)
throw new ArgumentNullException(nameof(value));
QueryParameters.Clear();
PathParameters.Clear();
_rawUri = value;
}
get
{
if(_rawUri != null)
if (_rawUri != null)
return _rawUri;
else if(PathParameters.TryGetValue(RawUrlKey, out var rawUrl) &&
else if (PathParameters.TryGetValue(RawUrlKey, out var rawUrl) &&
rawUrl is string rawUrlString)
{
URI = new Uri(rawUrlString);
return _rawUri!;
}
else
{
if(UrlTemplate?.IndexOf("{+baseurl}", StringComparison.OrdinalIgnoreCase) >= 0 && !PathParameters.ContainsKey("baseurl"))
if (UrlTemplate?.IndexOf("{+baseurl}", StringComparison.OrdinalIgnoreCase) >= 0 && !PathParameters.ContainsKey("baseurl"))
throw new InvalidOperationException($"{nameof(PathParameters)} must contain a value for \"baseurl\" for the url to be built.");

var substitutions = new Dictionary<string, object>();
foreach(var urlTemplateParameter in PathParameters)
foreach (var urlTemplateParameter in PathParameters)
{
substitutions.Add(urlTemplateParameter.Key, GetSanitizedValues(urlTemplateParameter.Value));
}

foreach(var queryStringParameter in QueryParameters)
foreach (var queryStringParameter in QueryParameters)
{
if(queryStringParameter.Value != null)
if (queryStringParameter.Value != null)
{
substitutions.Add(queryStringParameter.Key, GetSanitizedValues(queryStringParameter.Value));
}
Expand Down Expand Up @@ -157,8 +157,8 @@ public Uri URI
public void AddQueryParameters<T>(T source)
#endif
{
if(source == null) return;
foreach(var property in typeof(T)
if (source == null) return;
foreach (var property in typeof(T)
.GetProperties()
.Select(
x => (
Expand All @@ -179,7 +179,7 @@ public void AddQueryParameters<T>(T source)
private static object[] ExpandArray(Array collection)
{
var passedArray = new object[collection.Length];
for(var i = 0; i < collection.Length; i++)
for (var i = 0; i < collection.Length; i++)
{
passedArray[i] = GetSanitizedValue(collection.GetValue(i)!);
}
Expand All @@ -188,7 +188,7 @@ private static object[] ExpandArray(Array collection)

private static object ReplaceEnumValueByStringRepresentation(object source)
{
if(source is Enum enumValue && GetEnumName(enumValue) is string enumValueName)
if (source is Enum enumValue && GetEnumName(enumValue) is string enumValueName)
{
return enumValueName;
}
Expand All @@ -203,13 +203,17 @@ private static object ReplaceEnumValueByStringRepresentation(object source)
{
var type = value.GetType();

if(Enum.GetName(type, value) is not { } name)
if (Enum.GetName(type, value) is not { } name)
throw new ArgumentException($"Invalid Enum value {value} for enum of type {type}");

if(type.GetField(name)?.GetCustomAttribute<EnumMemberAttribute>() is { } attribute)
return attribute.Value;
#if NET5_0_OR_GREATER
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "Enumerating Enum fields is always trimming/AOT safe - https://github.com/dotnet/runtime/issues/97737")]
#endif
static string? GetEnumMemberValue(Type enumType, string name) =>
enumType.GetField(name, BindingFlags.Static | BindingFlags.Public)?.GetCustomAttribute<EnumMemberAttribute>() is { } attribute ? attribute.Value : null;

return name.ToFirstCharacterLowerCase();
return GetEnumMemberValue(type, name) ?? name.ToFirstCharacterLowerCase();
}
/// <summary>
/// The Request Headers.
Expand All @@ -220,7 +224,7 @@ private static object ReplaceEnumValueByStringRepresentation(object source)
/// </summary>
public void AddHeaders(RequestHeaders headers)
{
if(headers == null) return;
if (headers == null) return;
Headers.AddAll(headers);
}
/// <summary>
Expand All @@ -238,8 +242,8 @@ public void AddHeaders(RequestHeaders headers)
/// <param name="options">The option to add.</param>
public void AddRequestOptions(IEnumerable<IRequestOption> options)
{
if(options == null) return;
foreach(var option in options.Where(x => x != null))
if (options == null) return;
foreach (var option in options.Where(x => x != null))
_requestOptions.AddOrReplace(option.GetType().FullName!, option);
}
/// <summary>
Expand All @@ -248,8 +252,8 @@ public void AddRequestOptions(IEnumerable<IRequestOption> options)
/// <param name="options">Options to remove.</param>
public void RemoveRequestOptions(params IRequestOption[] options)
{
if(options.Length == 0) throw new ArgumentNullException(nameof(options));
foreach(var optionName in options.Where(x => x != null).Select(x => x.GetType().FullName))
if (options.Length == 0) throw new ArgumentNullException(nameof(options));
foreach (var optionName in options.Where(x => x != null).Select(x => x.GetType().FullName))
_requestOptions.Remove(optionName!);
}

Expand All @@ -263,7 +267,7 @@ public void RemoveRequestOptions(params IRequestOption[] options)
/// </summary>
public void SetResponseHandler(IResponseHandler responseHandler)
{
if(responseHandler == null)
if (responseHandler == null)
throw new ArgumentNullException(nameof(responseHandler));

var responseHandlerOption = new ResponseHandlerOption
Expand Down Expand Up @@ -322,7 +326,7 @@ public void SetContentFromParsable<T>(IRequestAdapter requestAdapter, string con
using var activity = _activitySource?.StartActivity(nameof(SetContentFromParsable));
using var writer = GetSerializationWriter(requestAdapter, contentType, item);
SetRequestType(item, activity);
if(item is MultipartBody mpBody)
if (item is MultipartBody mpBody)
{
contentType += "; boundary=" + mpBody.Boundary;
mpBody.RequestAdapter = requestAdapter;
Expand All @@ -333,15 +337,15 @@ public void SetContentFromParsable<T>(IRequestAdapter requestAdapter, string con
}
private static void SetRequestType(object? result, Activity? activity)
{
if(activity == null) return;
if(result == null) return;
if (activity == null) return;
if (result == null) return;
activity.SetTag("com.microsoft.kiota.request.type", result.GetType().FullName);
}
private static ISerializationWriter GetSerializationWriter<T>(IRequestAdapter requestAdapter, string contentType, T item)
{
if(string.IsNullOrEmpty(contentType)) throw new ArgumentNullException(nameof(contentType));
if(requestAdapter == null) throw new ArgumentNullException(nameof(requestAdapter));
if(item == null) throw new InvalidOperationException($"{nameof(item)} cannot be null");
if (string.IsNullOrEmpty(contentType)) throw new ArgumentNullException(nameof(contentType));
if (requestAdapter == null) throw new ArgumentNullException(nameof(requestAdapter));
if (item == null) throw new InvalidOperationException($"{nameof(item)} cannot be null");
return requestAdapter.SerializationWriterFactory.GetSerializationWriter(contentType);
}
/// <summary>
Expand Down Expand Up @@ -372,7 +376,7 @@ public void SetContentFromScalar<T>(IRequestAdapter requestAdapter, string conte
using var activity = _activitySource?.StartActivity(nameof(SetContentFromScalar));
using var writer = GetSerializationWriter(requestAdapter, contentType, item);
SetRequestType(item, activity);
switch(item)
switch (item)
{
case string s:
writer.WriteStringValue(null, s);
Expand Down
4 changes: 2 additions & 2 deletions src/serialization/IParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public interface IParseNode
/// </summary>
/// <returns>The collection of enum values.</returns>
#if NET5_0_OR_GREATER
IEnumerable<T?> GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]T>() where T : struct, Enum;
IEnumerable<T?> GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() where T : struct, Enum;
#else
IEnumerable<T?> GetCollectionOfEnumValues<T>() where T : struct, Enum;
#endif
Expand All @@ -116,7 +116,7 @@ public interface IParseNode
/// </summary>
/// <returns>The enum value of the node.</returns>
#if NET5_0_OR_GREATER
T? GetEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]T>() where T : struct, Enum;
T? GetEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() where T : struct, Enum;
#else
T? GetEnumValue<T>() where T : struct, Enum;
#endif
Expand Down

0 comments on commit 55af16f

Please sign in to comment.