-
Notifications
You must be signed in to change notification settings - Fork 2
Breaking change: Skip null values for form, header and query parameters #90
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d25e52e
Initial plan
Copilot ab15170
Implement null parameter skipping for QueryParam, FormParam, and Head…
Copilot 5876418
Clean up null parameter tests and finalize implementation
Copilot 6f7a634
Fix weakness in test and missing in copilot implementation
twogood 8c04d6e
Fix DictionaryParameterTests
twogood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Logging; | ||
| using RichardSzalay.MockHttp; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Activout.RestClient.Test; | ||
|
|
||
| public class NullParameterTests(ITestOutputHelper outputHelper) | ||
| { | ||
| private const string BaseUri = "https://example.com/api"; | ||
|
|
||
| private readonly IRestClientFactory _restClientFactory = Services.CreateRestClientFactory(); | ||
| private readonly MockHttpMessageHandler _mockHttp = new MockHttpMessageHandler(); | ||
| private readonly ILoggerFactory _loggerFactory = LoggerFactoryHelpers.CreateLoggerFactory(outputHelper); | ||
|
|
||
| private IRestClientBuilder CreateRestClientBuilder() | ||
| { | ||
| return _restClientFactory.CreateBuilder() | ||
| .With(_loggerFactory.CreateLogger<NullParameterTests>()) | ||
| .With(_mockHttp.ToHttpClient()) | ||
| .BaseUri(BaseUri); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestEmptyStringQueryParam_ShouldAddParameter() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
|
|
||
| // expect: empty string should still be added as parameter | ||
| _mockHttp | ||
| .When("https://example.com/api/test") | ||
| .WithExactQueryString("param=") | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestQueryParam(""); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestEmptyStringFormParam_ShouldAddParameter() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
|
|
||
| // expect: empty string should still be added as form parameter | ||
| _mockHttp | ||
| .When(HttpMethod.Post, "https://example.com/api/test") | ||
| .WithExactFormData([ | ||
| new KeyValuePair<string, string>("param", "") | ||
| ]) | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestFormParam(""); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestEmptyStringHeaderParam_ShouldAddHeader() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
|
|
||
| // expect: empty string should still be added as header | ||
| _mockHttp | ||
| .When("https://example.com/api/test") | ||
| .WithHeaders("X-Custom-Header", "") | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestHeaderParam(""); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestMixedNullAndValidQueryParams_ShouldOnlyAddValidParams() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
|
|
||
| // expect: only the valid parameter should be added, null param should be skipped | ||
| _mockHttp | ||
| .When("https://example.com/api/test") | ||
| .WithExactQueryString("validParam=validValue") | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestMixedQueryParams(null, "validValue"); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestMixedNullAndValidFormParams_ShouldOnlyAddValidParams() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
|
|
||
| // expect: only the valid form parameter should be added, null param should be skipped | ||
| _mockHttp | ||
| .When(HttpMethod.Post, "https://example.com/api/test") | ||
| .WithExactFormData([ | ||
| new KeyValuePair<string, string>("validParam", "validValue") | ||
| ]) | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestMixedFormParams(null, "validValue"); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestMixedNullAndValidHeaderParams_ShouldOnlyAddValidParams() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
|
|
||
| // expect: only the valid header should be added, null param should be skipped | ||
| _mockHttp | ||
| .When("https://example.com/api/test") | ||
| .WithHeaders("X-Valid-Header", "validValue") | ||
| .With(req => !req.Headers.Contains("X-Null-Header")) | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestMixedHeaderParams(null, "validValue"); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestNullValueInQueryDictionary_SkipsNullValues() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
| var queryParams = new Dictionary<string, string> | ||
| { | ||
| ["param1"] = "value1", | ||
| ["param2"] = null, // This should be skipped (already working correctly) | ||
| ["param3"] = "value3" | ||
| }; | ||
|
|
||
| // Dictionary handling already works correctly - only non-null values are added | ||
| _mockHttp | ||
| .When("https://example.com/api/test") | ||
| .WithExactQueryString("param1=value1¶m3=value3") | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestNullDictionaryValues(queryParams); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestNullValueInFormDictionary_SkipsNullValues() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
| var formParams = new Dictionary<string, string> | ||
| { | ||
| ["field1"] = "value1", | ||
| ["field2"] = null, // This should be skipped (already working correctly) | ||
| ["field3"] = "value3" | ||
| }; | ||
|
|
||
| // Dictionary handling already works correctly - only non-null values are added | ||
| _mockHttp | ||
| .When(HttpMethod.Post, "https://example.com/api/test") | ||
| .WithExactFormData([ | ||
| new KeyValuePair<string, string>("field1", "value1"), | ||
| new KeyValuePair<string, string>("field3", "value3") | ||
| ]) | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestNullFormDictionaryValues(formParams); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestNullValueInHeaderDictionary_SkipsNullValues() | ||
| { | ||
| // arrange | ||
| var service = CreateRestClientBuilder().Build<ITestNullService>(); | ||
| var headers = new Dictionary<string, string> | ||
| { | ||
| ["X-Header-1"] = "value1", | ||
| ["X-Header-2"] = null, // This should be skipped (already working correctly) | ||
| ["X-Header-3"] = "value3" | ||
| }; | ||
|
|
||
| // Dictionary handling already works correctly - only non-null values are added | ||
| _mockHttp | ||
| .When("https://example.com/api/test") | ||
| .WithHeaders("X-Header-1", "value1") | ||
| .WithHeaders("X-Header-3", "value3") | ||
| .With(req => !req.Headers.Contains("X-Header-2")) | ||
| .Respond("application/json", "{}"); | ||
|
|
||
| // act | ||
| await service.TestNullHeaderDictionaryValues(headers); | ||
|
|
||
| // assert | ||
| _mockHttp.VerifyNoOutstandingExpectation(); | ||
| } | ||
| } | ||
|
|
||
| public interface ITestNullService | ||
| { | ||
| [Get("test")] | ||
| Task TestQueryParam([QueryParam("param")] string param); | ||
|
|
||
| [Post("test")] | ||
| Task TestFormParam([FormParam("param")] string param); | ||
|
|
||
| [Get("test")] | ||
| Task TestHeaderParam([HeaderParam("X-Custom-Header")] string param); | ||
|
|
||
| [Get("test")] | ||
| Task TestMixedQueryParams([QueryParam("nullParam")] string nullParam, [QueryParam("validParam")] string validParam); | ||
|
|
||
| [Post("test")] | ||
| Task TestMixedFormParams([FormParam("nullParam")] string nullParam, [FormParam("validParam")] string validParam); | ||
|
|
||
| [Get("test")] | ||
| Task TestMixedHeaderParams([HeaderParam("X-Null-Header")] string nullHeader, [HeaderParam("X-Valid-Header")] string validHeader); | ||
|
|
||
| [Get("test")] | ||
| Task TestNullDictionaryValues([QueryParam] Dictionary<string, string> queryParams); | ||
|
|
||
| [Post("test")] | ||
| Task TestNullFormDictionaryValues([FormParam] Dictionary<string, string> formParams); | ||
|
|
||
| [Get("test")] | ||
| Task TestNullHeaderDictionaryValues([HeaderParam] Dictionary<string, string> headers); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.