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

Reduce IParseNode Enum methods DynamicallyAccessedMembers scope #202

Merged
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [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
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.7.10</VersionPrefix>
<VersionPrefix>1.7.11</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SignAssembly>false</SignAssembly>
Expand Down
60 changes: 31 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,15 @@ 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;
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
Justification = "Enumerating Enum fields is always trimming/AOT safe - https://github.com/dotnet/runtime/issues/97737")]
hwoodiwiss marked this conversation as resolved.
Show resolved Hide resolved
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 +222,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 +240,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 +250,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 +265,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 +324,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 +335,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 +374,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