From 45689d19284f31e911a835c5357ffc9f14ff5b86 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 13 Apr 2022 15:07:11 -0400 Subject: [PATCH 1/4] - aligns request options with configuration revamp --- .../QueryParametersBaseTests.cs | 51 ------------------- .../RequestInformationTests.cs | 43 +++++++++++++++- src/QueryParametersBase.cs | 39 -------------- src/RequestInformation.cs | 36 ++++++++++++- 4 files changed, 76 insertions(+), 93 deletions(-) delete mode 100644 Microsoft.Kiota.Abstractions.Tests/QueryParametersBaseTests.cs delete mode 100644 src/QueryParametersBase.cs diff --git a/Microsoft.Kiota.Abstractions.Tests/QueryParametersBaseTests.cs b/Microsoft.Kiota.Abstractions.Tests/QueryParametersBaseTests.cs deleted file mode 100644 index ea5b88e..0000000 --- a/Microsoft.Kiota.Abstractions.Tests/QueryParametersBaseTests.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Linq; -using Xunit; - -namespace Microsoft.Kiota.Abstractions.Tests -{ - public class QueryParametersBaseTests - { - [Fact] - public void SetsSelectQueryParameters() - { - // Arrange as the request builders would - var requestInfo = new RequestInformation - { - HttpMethod = Method.GET, - UrlTemplate = "http://localhost/me{?%24select}" - }; - Action q = x => x.Select = new[] { "id", "displayName" }; - var qParams = new GetQueryParameters(); - q.Invoke(qParams); - - // Act - qParams.AddQueryParameters(requestInfo.QueryParameters); - - // Assert - Assert.True(requestInfo.QueryParameters.ContainsKey("%24select")); - Assert.False(requestInfo.QueryParameters.ContainsKey("select")); - Assert.Equal("%24select",requestInfo.QueryParameters.First().Key); - } - } - - /// The messages in a mailbox or folder. Read-only. Nullable. - internal class GetQueryParameters : QueryParametersBase - { - /// Select properties to be returned\ - [QueryParameter("%24select")] - public string[] Select { get; set; } - /// Include count of items - [QueryParameter("%24count")] - public bool? Count { get; set; } - /// Expand related entities - [QueryParameter("%24filter")] - public string Filter { get; set; } - /// Order items by property values - [QueryParameter("%24orderby")] - public string[] Orderby { get; set; } - /// Search items by search phrases - [QueryParameter("%24search")] - public string Search { get; set; } - } -} diff --git a/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs b/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs index 8d564b0..b23c9e4 100644 --- a/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs +++ b/Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs @@ -39,7 +39,7 @@ public void AddsAndRemovesRequestOptions() var testRequestOption = new Mock().Object; Assert.Empty(testRequest.RequestOptions); // Act - testRequest.AddRequestOptions(testRequestOption); + testRequest.AddRequestOptions(new IRequestOption[] {testRequestOption}); // Assert Assert.NotEmpty(testRequest.RequestOptions); Assert.Equal(testRequestOption, testRequest.RequestOptions.First()); @@ -48,5 +48,46 @@ public void AddsAndRemovesRequestOptions() testRequest.RemoveRequestOptions(testRequestOption); Assert.Empty(testRequest.RequestOptions); } + [Fact] + public void SetsSelectQueryParameters() + { + // Arrange as the request builders would + var requestInfo = new RequestInformation + { + HttpMethod = Method.GET, + UrlTemplate = "http://localhost/me{?%24select}" + }; + Action q = x => x.Select = new[] { "id", "displayName" }; + var qParams = new GetQueryParameters(); + q.Invoke(qParams); + + // Act + requestInfo.AddQueryParameters(qParams); + + // Assert + Assert.True(requestInfo.QueryParameters.ContainsKey("%24select")); + Assert.False(requestInfo.QueryParameters.ContainsKey("select")); + Assert.Equal("%24select",requestInfo.QueryParameters.First().Key); + } + } + + /// The messages in a mailbox or folder. Read-only. Nullable. + internal class GetQueryParameters + { + /// Select properties to be returned\ + [QueryParameter("%24select")] + public string[] Select { get; set; } + /// Include count of items + [QueryParameter("%24count")] + public bool? Count { get; set; } + /// Expand related entities + [QueryParameter("%24filter")] + public string Filter { get; set; } + /// Order items by property values + [QueryParameter("%24orderby")] + public string[] Orderby { get; set; } + /// Search items by search phrases + [QueryParameter("%24search")] + public string Search { get; set; } } } diff --git a/src/QueryParametersBase.cs b/src/QueryParametersBase.cs deleted file mode 100644 index e6b6e2f..0000000 --- a/src/QueryParametersBase.cs +++ /dev/null @@ -1,39 +0,0 @@ -// ------------------------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. -// ------------------------------------------------------------------------------ - -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.Kiota.Abstractions.Extensions; - -namespace Microsoft.Kiota.Abstractions -{ - /// - /// The base implementation of the Query Parameters - /// - public abstract class QueryParametersBase - { - /// - /// Vanity method to add the query parameters to the request query parameters dictionary. - /// - public void AddQueryParameters(IDictionary target) - { - if(target == null) throw new ArgumentNullException(nameof(target)); - foreach(var property in this.GetType() - .GetProperties() - .Select( - x => ( - Name: x.GetCustomAttributes(false) - .OfType() - .FirstOrDefault()?.TemplateName ?? x.Name.ToFirstCharacterLowerCase(), - Value: x.GetValue(this) - ) - ) - .Where(x => x.Value != null && !target.ContainsKey(x.Name))) - { - target.Add(property.Name, property.Value); - } - } - } -} diff --git a/src/RequestInformation.cs b/src/RequestInformation.cs index 2329899..9527b42 100644 --- a/src/RequestInformation.cs +++ b/src/RequestInformation.cs @@ -75,10 +75,42 @@ public Uri URI { /// public IDictionary QueryParameters { get; set; } = new Dictionary(StringComparer.OrdinalIgnoreCase); /// + /// Vanity method to add the query parameters to the request query parameters dictionary. + /// + /// The query parameters to add. + public void AddQueryParameters(object source) + { + if(source == null) return; + foreach(var property in source.GetType() + .GetProperties() + .Select( + x => ( + Name: x.GetCustomAttributes(false) + .OfType() + .FirstOrDefault()?.TemplateName ?? x.Name.ToFirstCharacterLowerCase(), + Value: x.GetValue(source) + ) + ) + .Where(x => x.Value != null && !QueryParameters.ContainsKey(x.Name))) + { + QueryParameters.Add(property.Name, property.Value); + } + } + /// /// The Request Headers. /// public IDictionary Headers { get; set; } = new Dictionary(StringComparer.OrdinalIgnoreCase); /// + /// Adds request headers to the request. + /// + /// The request headers to add. + public void AddHeaders(IDictionary source) + { + if(source == null) return; + foreach(var header in source) + Headers.Add(header.Key, header.Value); + } + /// /// The Request Body. /// public Stream Content { get; set; } @@ -91,9 +123,9 @@ public Uri URI { /// Adds an option to the request. /// /// The option to add. - public void AddRequestOptions(params IRequestOption[] options) + public void AddRequestOptions(IEnumerable options) { - if(!(options?.Any() ?? false)) return; // it's a no-op if there are no options and this avoid having to check in the code gen. + if(options == null) return; foreach(var option in options.Where(x => x != null)) if(!_requestOptions.TryAdd(option.GetType().FullName, option)) _requestOptions[option.GetType().FullName] = option; From 98d82bfce119d69c581b638e7c2c965f0c838592 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 14 Apr 2022 09:08:16 -0400 Subject: [PATCH 2/4] - adds extension method for add or replace --- src/RequestInformation.cs | 7 +++---- src/extensions/IDictionaryExtensions.cs | 26 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/RequestInformation.cs b/src/RequestInformation.cs index 9527b42..b312067 100644 --- a/src/RequestInformation.cs +++ b/src/RequestInformation.cs @@ -93,7 +93,7 @@ public void AddQueryParameters(object source) ) .Where(x => x.Value != null && !QueryParameters.ContainsKey(x.Name))) { - QueryParameters.Add(property.Name, property.Value); + QueryParameters.AddOrReplace(property.Name, property.Value); } } /// @@ -108,7 +108,7 @@ public void AddHeaders(IDictionary source) { if(source == null) return; foreach(var header in source) - Headers.Add(header.Key, header.Value); + Headers.AddOrReplace(header.Key, header.Value); } /// /// The Request Body. @@ -127,8 +127,7 @@ public void AddRequestOptions(IEnumerable options) { if(options == null) return; foreach(var option in options.Where(x => x != null)) - if(!_requestOptions.TryAdd(option.GetType().FullName, option)) - _requestOptions[option.GetType().FullName] = option; + _requestOptions.AddOrReplace(option.GetType().FullName, option); } /// /// Removes given options from the current request. diff --git a/src/extensions/IDictionaryExtensions.cs b/src/extensions/IDictionaryExtensions.cs index ed95f28..c8b51a3 100644 --- a/src/extensions/IDictionaryExtensions.cs +++ b/src/extensions/IDictionaryExtensions.cs @@ -11,7 +11,7 @@ namespace Microsoft.Kiota.Abstractions.Extensions /// /// Extension methods for the /// - public static class IDictionaryExtensions + internal static class IDictionaryExtensions { /// /// Try to add the element to the instance. @@ -28,6 +28,11 @@ public static bool TryAdd(this IDictionary dictionar { throw new ArgumentNullException(nameof(dictionary)); } + + if(key == null) + { + throw new ArgumentNullException(nameof(key)); + } if(!dictionary.ContainsKey(key)) { @@ -37,6 +42,25 @@ public static bool TryAdd(this IDictionary dictionar return false; } + /// + /// Adds or replaces the element to the instance. + /// + /// The type of the key + /// The type of the value + /// The dictionary to add to. + /// The key parameter. + /// The value + /// The previous value if any + public static TValue AddOrReplace(this IDictionary dictionary, TKey key, TValue value) + { + if(!dictionary.TryAdd(key, value)) + { + var oldValue = dictionary[key]; + dictionary[key] = value; + return oldValue; + } + return default(TValue); + } } } From 4355f05e8d7d8ef3e80fe96dd4e35d709826f1ac Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 14 Apr 2022 13:07:30 -0400 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Eastman --- src/extensions/IDictionaryExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/IDictionaryExtensions.cs b/src/extensions/IDictionaryExtensions.cs index c8b51a3..cc7723b 100644 --- a/src/extensions/IDictionaryExtensions.cs +++ b/src/extensions/IDictionaryExtensions.cs @@ -11,7 +11,7 @@ namespace Microsoft.Kiota.Abstractions.Extensions /// /// Extension methods for the /// - internal static class IDictionaryExtensions + public static class IDictionaryExtensions { /// /// Try to add the element to the instance. From cac903b7f31571b7a512b337832829922a7346fc Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Fri, 22 Apr 2022 13:10:01 +0300 Subject: [PATCH 4/4] Updates version and release notes --- CHANGELOG.md | 6 ++++++ src/Microsoft.Kiota.Abstractions.csproj | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1aa34d8..99bf915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.0.0-preview.6] - 2022-04-22 + +### Added + +- Adds support for api surface revamp for query parameters + ## [1.0.0-preview.5] - 2022-04-12 ### Changed diff --git a/src/Microsoft.Kiota.Abstractions.csproj b/src/Microsoft.Kiota.Abstractions.csproj index 7935e36..1c94b29 100644 --- a/src/Microsoft.Kiota.Abstractions.csproj +++ b/src/Microsoft.Kiota.Abstractions.csproj @@ -14,7 +14,7 @@ true true 1.0.0 - preview.5 + preview.6 true false false @@ -23,7 +23,7 @@ - - [Breaking] Change target runtime to netstandard 2.0 + - Adds support for api surface revamp for query parameters true LICENSE