Skip to content

Commit 55af16f

Browse files
authored
Merge branch 'main' into feature/175-untyped-nodes
2 parents cb9b2cd + 972be8b commit 55af16f

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

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

16+
## [1.7.11] - 2024-02-26
17+
18+
### Changed
19+
20+
- Updated IParseNode enum methods `DynamicallyAccessedMembersAttribute` to `PublicFields`.
21+
- Fixed AOT compiler warnings from ILC.
22+
1623
## [1.7.10] - 2024-02-26
1724

1825
### Changed

src/RequestInformation.cs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public RequestInformation(Method method, string urlTemplate, IDictionary<string,
5353
public void Configure<T>(Action<RequestConfiguration<T>>? requestConfiguration) where T : class, new()
5454
#endif
5555
{
56-
if(requestConfiguration == null) return;
56+
if (requestConfiguration == null) return;
5757
var requestConfig = new RequestConfiguration<T>();
5858
requestConfiguration(requestConfig);
5959
AddQueryParameters(requestConfig.QueryParameters);
@@ -69,36 +69,36 @@ public Uri URI
6969
{
7070
set
7171
{
72-
if(value == null)
72+
if (value == null)
7373
throw new ArgumentNullException(nameof(value));
7474
QueryParameters.Clear();
7575
PathParameters.Clear();
7676
_rawUri = value;
7777
}
7878
get
7979
{
80-
if(_rawUri != null)
80+
if (_rawUri != null)
8181
return _rawUri;
82-
else if(PathParameters.TryGetValue(RawUrlKey, out var rawUrl) &&
82+
else if (PathParameters.TryGetValue(RawUrlKey, out var rawUrl) &&
8383
rawUrl is string rawUrlString)
8484
{
8585
URI = new Uri(rawUrlString);
8686
return _rawUri!;
8787
}
8888
else
8989
{
90-
if(UrlTemplate?.IndexOf("{+baseurl}", StringComparison.OrdinalIgnoreCase) >= 0 && !PathParameters.ContainsKey("baseurl"))
90+
if (UrlTemplate?.IndexOf("{+baseurl}", StringComparison.OrdinalIgnoreCase) >= 0 && !PathParameters.ContainsKey("baseurl"))
9191
throw new InvalidOperationException($"{nameof(PathParameters)} must contain a value for \"baseurl\" for the url to be built.");
9292

9393
var substitutions = new Dictionary<string, object>();
94-
foreach(var urlTemplateParameter in PathParameters)
94+
foreach (var urlTemplateParameter in PathParameters)
9595
{
9696
substitutions.Add(urlTemplateParameter.Key, GetSanitizedValues(urlTemplateParameter.Value));
9797
}
9898

99-
foreach(var queryStringParameter in QueryParameters)
99+
foreach (var queryStringParameter in QueryParameters)
100100
{
101-
if(queryStringParameter.Value != null)
101+
if (queryStringParameter.Value != null)
102102
{
103103
substitutions.Add(queryStringParameter.Key, GetSanitizedValues(queryStringParameter.Value));
104104
}
@@ -157,8 +157,8 @@ public Uri URI
157157
public void AddQueryParameters<T>(T source)
158158
#endif
159159
{
160-
if(source == null) return;
161-
foreach(var property in typeof(T)
160+
if (source == null) return;
161+
foreach (var property in typeof(T)
162162
.GetProperties()
163163
.Select(
164164
x => (
@@ -179,7 +179,7 @@ public void AddQueryParameters<T>(T source)
179179
private static object[] ExpandArray(Array collection)
180180
{
181181
var passedArray = new object[collection.Length];
182-
for(var i = 0; i < collection.Length; i++)
182+
for (var i = 0; i < collection.Length; i++)
183183
{
184184
passedArray[i] = GetSanitizedValue(collection.GetValue(i)!);
185185
}
@@ -188,7 +188,7 @@ private static object[] ExpandArray(Array collection)
188188

189189
private static object ReplaceEnumValueByStringRepresentation(object source)
190190
{
191-
if(source is Enum enumValue && GetEnumName(enumValue) is string enumValueName)
191+
if (source is Enum enumValue && GetEnumName(enumValue) is string enumValueName)
192192
{
193193
return enumValueName;
194194
}
@@ -203,13 +203,17 @@ private static object ReplaceEnumValueByStringRepresentation(object source)
203203
{
204204
var type = value.GetType();
205205

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

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

212-
return name.ToFirstCharacterLowerCase();
216+
return GetEnumMemberValue(type, name) ?? name.ToFirstCharacterLowerCase();
213217
}
214218
/// <summary>
215219
/// The Request Headers.
@@ -220,7 +224,7 @@ private static object ReplaceEnumValueByStringRepresentation(object source)
220224
/// </summary>
221225
public void AddHeaders(RequestHeaders headers)
222226
{
223-
if(headers == null) return;
227+
if (headers == null) return;
224228
Headers.AddAll(headers);
225229
}
226230
/// <summary>
@@ -238,8 +242,8 @@ public void AddHeaders(RequestHeaders headers)
238242
/// <param name="options">The option to add.</param>
239243
public void AddRequestOptions(IEnumerable<IRequestOption> options)
240244
{
241-
if(options == null) return;
242-
foreach(var option in options.Where(x => x != null))
245+
if (options == null) return;
246+
foreach (var option in options.Where(x => x != null))
243247
_requestOptions.AddOrReplace(option.GetType().FullName!, option);
244248
}
245249
/// <summary>
@@ -248,8 +252,8 @@ public void AddRequestOptions(IEnumerable<IRequestOption> options)
248252
/// <param name="options">Options to remove.</param>
249253
public void RemoveRequestOptions(params IRequestOption[] options)
250254
{
251-
if(options.Length == 0) throw new ArgumentNullException(nameof(options));
252-
foreach(var optionName in options.Where(x => x != null).Select(x => x.GetType().FullName))
255+
if (options.Length == 0) throw new ArgumentNullException(nameof(options));
256+
foreach (var optionName in options.Where(x => x != null).Select(x => x.GetType().FullName))
253257
_requestOptions.Remove(optionName!);
254258
}
255259

@@ -263,7 +267,7 @@ public void RemoveRequestOptions(params IRequestOption[] options)
263267
/// </summary>
264268
public void SetResponseHandler(IResponseHandler responseHandler)
265269
{
266-
if(responseHandler == null)
270+
if (responseHandler == null)
267271
throw new ArgumentNullException(nameof(responseHandler));
268272

269273
var responseHandlerOption = new ResponseHandlerOption
@@ -322,7 +326,7 @@ public void SetContentFromParsable<T>(IRequestAdapter requestAdapter, string con
322326
using var activity = _activitySource?.StartActivity(nameof(SetContentFromParsable));
323327
using var writer = GetSerializationWriter(requestAdapter, contentType, item);
324328
SetRequestType(item, activity);
325-
if(item is MultipartBody mpBody)
329+
if (item is MultipartBody mpBody)
326330
{
327331
contentType += "; boundary=" + mpBody.Boundary;
328332
mpBody.RequestAdapter = requestAdapter;
@@ -333,15 +337,15 @@ public void SetContentFromParsable<T>(IRequestAdapter requestAdapter, string con
333337
}
334338
private static void SetRequestType(object? result, Activity? activity)
335339
{
336-
if(activity == null) return;
337-
if(result == null) return;
340+
if (activity == null) return;
341+
if (result == null) return;
338342
activity.SetTag("com.microsoft.kiota.request.type", result.GetType().FullName);
339343
}
340344
private static ISerializationWriter GetSerializationWriter<T>(IRequestAdapter requestAdapter, string contentType, T item)
341345
{
342-
if(string.IsNullOrEmpty(contentType)) throw new ArgumentNullException(nameof(contentType));
343-
if(requestAdapter == null) throw new ArgumentNullException(nameof(requestAdapter));
344-
if(item == null) throw new InvalidOperationException($"{nameof(item)} cannot be null");
346+
if (string.IsNullOrEmpty(contentType)) throw new ArgumentNullException(nameof(contentType));
347+
if (requestAdapter == null) throw new ArgumentNullException(nameof(requestAdapter));
348+
if (item == null) throw new InvalidOperationException($"{nameof(item)} cannot be null");
345349
return requestAdapter.SerializationWriterFactory.GetSerializationWriter(contentType);
346350
}
347351
/// <summary>
@@ -372,7 +376,7 @@ public void SetContentFromScalar<T>(IRequestAdapter requestAdapter, string conte
372376
using var activity = _activitySource?.StartActivity(nameof(SetContentFromScalar));
373377
using var writer = GetSerializationWriter(requestAdapter, contentType, item);
374378
SetRequestType(item, activity);
375-
switch(item)
379+
switch (item)
376380
{
377381
case string s:
378382
writer.WriteStringValue(null, s);

src/serialization/IParseNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public interface IParseNode
101101
/// </summary>
102102
/// <returns>The collection of enum values.</returns>
103103
#if NET5_0_OR_GREATER
104-
IEnumerable<T?> GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]T>() where T : struct, Enum;
104+
IEnumerable<T?> GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() where T : struct, Enum;
105105
#else
106106
IEnumerable<T?> GetCollectionOfEnumValues<T>() where T : struct, Enum;
107107
#endif
@@ -116,7 +116,7 @@ public interface IParseNode
116116
/// </summary>
117117
/// <returns>The enum value of the node.</returns>
118118
#if NET5_0_OR_GREATER
119-
T? GetEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]T>() where T : struct, Enum;
119+
T? GetEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() where T : struct, Enum;
120120
#else
121121
T? GetEnumValue<T>() where T : struct, Enum;
122122
#endif

0 commit comments

Comments
 (0)