diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f9a1111..6e4866de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.7.7] - 2024-02-01 + +### Changed + +- Fixed AOT trimming warnings the URI template parameters resolution. [microsoft/kiota#4065](https://github.com/microsoft/kiota/issues/4065). + ## [1.7.6] - 2024-01-24 ### Changed diff --git a/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs b/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs index e2e1d872..89e701b2 100644 --- a/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs +++ b/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs @@ -529,7 +529,7 @@ public void SetsIntValuesInQueryParameters() UrlTemplate = "http://localhost/me{?items}" }; // Act - requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object []{1,2}}); + requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { 1, 2 } }); // Assert Assert.Equal("http://localhost/me?items=1,2", requestInfo.URI.ToString()); } @@ -544,7 +544,7 @@ public void SetsBooleanValuesInQueryParameters() UrlTemplate = "http://localhost/me{?items}" }; // Act - requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object [] { true, false } }); + requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { true, false } }); // Assert Assert.Equal("http://localhost/me?items=true,false", requestInfo.URI.ToString()); } @@ -552,7 +552,7 @@ public void SetsBooleanValuesInQueryParameters() [Fact] public void SetsDateTimeOffsetValuesInQueryParameters() { - var requestInfo = new RequestInformation + var requestInfo = new RequestInformation { HttpMethod = Method.GET, UrlTemplate = "http://localhost/me{?items}" @@ -615,7 +615,7 @@ public void SetsTimeValuesInQueryParameters() }; // Act - var date1 = new Time(10,0,0); + var date1 = new Time(10, 0, 0); var date2 = new Time(11, 1, 1); requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { date1, date2 } }); @@ -654,15 +654,15 @@ public void DoesNotExpandSecondLayerArrays() }; // Act - requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[]{new int[]{1,2,3,4} } }); + requestInfo.AddQueryParameters(new GetQueryParameters { Items = [new int[] { 1, 2, 3, 4 }] }); // Assert - Assert.Equal("http://localhost/me?items=System.Int32%5B%5D", requestInfo.URI.OriginalString); + Assert.Throws(() => requestInfo.URI.OriginalString); } } - + /// The messages in a mailbox or folder. Read-only. Nullable. internal class GetQueryParameters diff --git a/src/Microsoft.Kiota.Abstractions.csproj b/src/Microsoft.Kiota.Abstractions.csproj index d3eb7c30..5d523df1 100644 --- a/src/Microsoft.Kiota.Abstractions.csproj +++ b/src/Microsoft.Kiota.Abstractions.csproj @@ -14,7 +14,7 @@ https://aka.ms/kiota/docs true true - 1.7.6 + 1.7.7 true false diff --git a/src/MultipartBody.cs b/src/MultipartBody.cs index 7303cb84..5fa5e9e4 100644 --- a/src/MultipartBody.cs +++ b/src/MultipartBody.cs @@ -97,7 +97,7 @@ public void Serialize(ISerializationWriter writer) { throw new InvalidOperationException(nameof(RequestAdapter.SerializationWriterFactory)); } - if(!_parts.Any()) + if(_parts.Count == 0) { throw new InvalidOperationException("No parts to serialize"); } diff --git a/src/RequestHeaders.cs b/src/RequestHeaders.cs index fffcab2f..5329dd1f 100644 --- a/src/RequestHeaders.cs +++ b/src/RequestHeaders.cs @@ -25,7 +25,7 @@ public void Add(string headerName, params string[] headerValues) throw new ArgumentNullException(nameof(headerName)); if(headerValues == null) throw new ArgumentNullException(nameof(headerValues)); - if(!headerValues.Any()) + if(headerValues.Length == 0) return; if(_singleValueHeaders.Contains(headerName)) _headers[headerName] = new HashSet { headerValues[0] }; @@ -79,7 +79,7 @@ public bool Remove(string headerName, string headerValue) if(_headers.TryGetValue(headerName, out var values)) { var result = values.Remove(headerValue); - if(!values.Any()) + if(values.Count == 0) _headers.Remove(headerName); return result; } diff --git a/src/RequestInformation.cs b/src/RequestInformation.cs index d4f5054a..16ddf283 100644 --- a/src/RequestInformation.cs +++ b/src/RequestInformation.cs @@ -5,12 +5,10 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.Serialization; using Microsoft.Kiota.Abstractions.Extensions; using Microsoft.Kiota.Abstractions.Serialization; @@ -114,7 +112,7 @@ public Uri URI }; /// - /// Sanitizes objects in order to appear appropiately in the URL + /// Sanitizes objects in order to appear appropriately in the URL /// /// Object to be sanitized /// Sanitized object @@ -174,12 +172,12 @@ public void AddQueryParameters(T source) } } - private static object ExpandArray(Array collection) + private static object[] ExpandArray(Array collection) { - var passedArray = new string[collection.Length]; + var passedArray = new object[collection.Length]; for(var i = 0; i < collection.Length; i++) { - passedArray[i] = GetSanitizedValue(collection.GetValue(i)!).ToString()!; + passedArray[i] = GetSanitizedValue(collection.GetValue(i)!); } return passedArray; } @@ -190,11 +188,11 @@ private static object ReplaceEnumValueByStringRepresentation(object source) { return enumValueName; } - + return source; } #if NET5_0_OR_GREATER - private static string? GetEnumName<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(T value) where T : Enum + private static string? GetEnumName<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(T value) where T : Enum #else private static string? GetEnumName(T value) where T : Enum #endif @@ -204,7 +202,7 @@ private static object ReplaceEnumValueByStringRepresentation(object source) if(Enum.GetName(type, value) is not { } name) throw new ArgumentException($"Invalid Enum value {value} for enum of type {type}"); - if(type.GetMember(name).FirstOrDefault()?.GetCustomAttribute() is { } attribute) + if(type.GetField(name)?.GetCustomAttribute() is { } attribute) return attribute.Value; return name.ToFirstCharacterLowerCase(); @@ -225,7 +223,7 @@ public void AddHeaders(RequestHeaders headers) /// The Request Body. /// public Stream Content { get; set; } = Stream.Null; - private readonly Dictionary _requestOptions = new Dictionary(StringComparer.OrdinalIgnoreCase); + private readonly Dictionary _requestOptions = new(StringComparer.OrdinalIgnoreCase); /// /// Gets the options for this request. Options are unique by type. If an option of the same type is added twice, the last one wins. /// @@ -246,8 +244,8 @@ public void AddRequestOptions(IEnumerable options) /// Options to remove. public void RemoveRequestOptions(params IRequestOption[] options) { - if(!options?.Any() ?? false) 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!); } @@ -291,7 +289,7 @@ public void SetStreamContent(Stream content, string contentType) Content = content; Headers.TryAdd(ContentTypeHeader, contentType); } - private static ActivitySource _activitySource = new(typeof(RequestInformation).Namespace!); + private static readonly ActivitySource _activitySource = new(typeof(RequestInformation).Namespace!); /// /// Sets the request body from a model with the specified content type. /// diff --git a/src/authentication/AllowedHostsValidator.cs b/src/authentication/AllowedHostsValidator.cs index 6ad4d03e..b4f0f424 100644 --- a/src/authentication/AllowedHostsValidator.cs +++ b/src/authentication/AllowedHostsValidator.cs @@ -50,7 +50,7 @@ public IEnumerable AllowedHosts /// public bool IsUrlHostValid(Uri uri) { - return !_allowedHosts.Any() || _allowedHosts.Contains(uri.Host); + return _allowedHosts.Count == 0 || _allowedHosts.Contains(uri.Host); } private static void ValidateHosts(IEnumerable hostsToValidate) diff --git a/src/serialization/ISerializationWriter.cs b/src/serialization/ISerializationWriter.cs index 54f051d7..602657b3 100644 --- a/src/serialization/ISerializationWriter.cs +++ b/src/serialization/ISerializationWriter.cs @@ -141,7 +141,7 @@ public interface ISerializationWriter : IDisposable /// The key to be used for the written value. May be null. /// The enum value to be written. #if NET5_0_OR_GREATER - void WriteEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string? key, T? value) where T : struct, Enum; + void WriteEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(string? key, T? value) where T : struct, Enum; #else void WriteEnumValue(string? key, T? value) where T : struct, Enum; #endif diff --git a/src/serialization/KiotaSerializer.Deserialization.cs b/src/serialization/KiotaSerializer.Deserialization.cs index 5756b08c..6f265af8 100644 --- a/src/serialization/KiotaSerializer.Deserialization.cs +++ b/src/serialization/KiotaSerializer.Deserialization.cs @@ -31,7 +31,9 @@ private static Stream GetStreamFromString(string source) { var stream = new MemoryStream(); using var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true); - writer.WriteAsync(source).GetAwaiter().GetResult(); // so the asp.net projects don't get an error + + // Some clients enforce async stream processing. + writer.WriteAsync(source).GetAwaiter().GetResult(); writer.Flush(); stream.Position = 0; return stream; diff --git a/src/serialization/ParseNodeFactoryRegistry.cs b/src/serialization/ParseNodeFactoryRegistry.cs index 231b5d2f..ff162de6 100644 --- a/src/serialization/ParseNodeFactoryRegistry.cs +++ b/src/serialization/ParseNodeFactoryRegistry.cs @@ -47,12 +47,12 @@ public IParseNode GetRootParseNode(string contentType, Stream content) _ = content ?? throw new ArgumentNullException(nameof(content)); var vendorSpecificContentType = contentType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); - if(ContentTypeAssociatedFactories.ContainsKey(vendorSpecificContentType)) - return ContentTypeAssociatedFactories[vendorSpecificContentType].GetRootParseNode(vendorSpecificContentType, content); + if(ContentTypeAssociatedFactories.TryGetValue(vendorSpecificContentType, out var vendorFactory)) + return vendorFactory.GetRootParseNode(vendorSpecificContentType, content); var cleanedContentType = contentTypeVendorCleanupRegex.Replace(vendorSpecificContentType, string.Empty); - if(ContentTypeAssociatedFactories.ContainsKey(cleanedContentType)) - return ContentTypeAssociatedFactories[cleanedContentType].GetRootParseNode(cleanedContentType, content); + if(ContentTypeAssociatedFactories.TryGetValue(cleanedContentType, out var factory)) + return factory.GetRootParseNode(cleanedContentType, content); throw new InvalidOperationException($"Content type {cleanedContentType} does not have a factory registered to be parsed"); } diff --git a/src/serialization/ParseNodeHelper.cs b/src/serialization/ParseNodeHelper.cs index e6bb0415..2a22a48f 100644 --- a/src/serialization/ParseNodeHelper.cs +++ b/src/serialization/ParseNodeHelper.cs @@ -23,7 +23,7 @@ public static IDictionary> MergeDeserializersForInter { throw new ArgumentNullException(nameof(targets)); } - if(!targets.Any()) + if(targets.Length == 0) { throw new ArgumentException("At least one target must be provided.", nameof(targets)); } diff --git a/src/serialization/SerializationWriterFactoryRegistry.cs b/src/serialization/SerializationWriterFactoryRegistry.cs index 6ba7bce5..fa53aaca 100644 --- a/src/serialization/SerializationWriterFactoryRegistry.cs +++ b/src/serialization/SerializationWriterFactoryRegistry.cs @@ -42,12 +42,12 @@ public ISerializationWriter GetSerializationWriter(string contentType) throw new ArgumentNullException(nameof(contentType)); var vendorSpecificContentType = contentType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); - if(ContentTypeAssociatedFactories.ContainsKey(vendorSpecificContentType)) - return ContentTypeAssociatedFactories[vendorSpecificContentType].GetSerializationWriter(vendorSpecificContentType); + if(ContentTypeAssociatedFactories.TryGetValue(vendorSpecificContentType, out var vendorFactory)) + return vendorFactory.GetSerializationWriter(vendorSpecificContentType); var cleanedContentType = ParseNodeFactoryRegistry.contentTypeVendorCleanupRegex.Replace(vendorSpecificContentType, string.Empty); - if(ContentTypeAssociatedFactories.ContainsKey(cleanedContentType)) - return ContentTypeAssociatedFactories[cleanedContentType].GetSerializationWriter(cleanedContentType); + if(ContentTypeAssociatedFactories.TryGetValue(cleanedContentType, out var factory)) + return factory.GetSerializationWriter(cleanedContentType); throw new InvalidOperationException($"Content type {cleanedContentType} does not have a factory registered to be parsed"); }