Skip to content

Commit

Permalink
Merge pull request #32 from atc-net/feature/extend-WithQueryParameter
Browse files Browse the repository at this point in the history
Handle Enum, DateTimeOffset etc in MessageRequestBuilder
  • Loading branch information
davidkallesen authored Dec 17, 2024
2 parents bd538f2 + 2b62e63 commit 8d33174
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 12 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,13 @@ dotnet_diagnostic.MA0048.severity = error # https://github.com/atc-net
dotnet_diagnostic.CA1014.severity = none # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/CA1014.md
dotnet_diagnostic.CA1068.severity = error # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/CA1068.md
dotnet_diagnostic.CA1305.severity = error
dotnet_diagnostic.CA1308.severity = suggestion # Normalize strings to uppercase
dotnet_diagnostic.CA1510.severity = suggestion # Use ArgumentNullException throw helper
dotnet_diagnostic.CA1511.severity = suggestion # Use ArgumentException throw helper
dotnet_diagnostic.CA1512.severity = suggestion # Use ArgumentOutOfRangeException throw helper
dotnet_diagnostic.CA1513.severity = suggestion # Use ObjectDisposedException throw helper
dotnet_diagnostic.CA1514.severity = error # Avoid redundant length argument
dotnet_diagnostic.CA1515.severity = suggestion # Because an application's API isn't typically referenced from outside the assembly, types can be made internal (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1515)
dotnet_diagnostic.CA1707.severity = error # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/CA1707.md
dotnet_diagnostic.CA1812.severity = none
dotnet_diagnostic.CA1822.severity = suggestion
Expand Down Expand Up @@ -504,7 +506,16 @@ dotnet_diagnostic.CA2259.severity = error # Ensure ThreadStatic is onl
dotnet_diagnostic.CA2260.severity = error # Implement generic math interfaces correctly
dotnet_diagnostic.CA2261.severity = error # Do not use ConfigureAwaitOptions.SuppressThrowing with Task<TResult>
dotnet_diagnostic.IDE0005.severity = warning # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/IDE0005.md
dotnet_diagnostic.IDE0010.severity = suggestion # Populate switch
dotnet_diagnostic.IDE0028.severity = suggestion # Collection initialization can be simplified
dotnet_diagnostic.IDE0021.severity = suggestion # Use expression body for constructor
dotnet_diagnostic.IDE0055.severity = none # Fix formatting
dotnet_diagnostic.IDE0058.severity = none # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/IDE0058.md
dotnet_diagnostic.IDE0061.severity = suggestion # Use expression body for local function
dotnet_diagnostic.IDE0130.severity = suggestion # Namespace does not match folder structure
dotnet_diagnostic.IDE0290.severity = none # Use primary constructor
dotnet_diagnostic.IDE0301.severity = suggestion # Use collection expression for empty
dotnet_diagnostic.IDE0305.severity = suggestion # Collection initialization can be simplified


# Microsoft - Compiler Errors
Expand Down Expand Up @@ -541,6 +552,7 @@ dotnet_diagnostic.SA1649.severity = error # https://github.com/atc-net
# https://rules.sonarsource.com/csharp
dotnet_diagnostic.S1135.severity = suggestion # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/SonarAnalyzerCSharp/S1135.md
dotnet_diagnostic.S2629.severity = none # Don't use string interpolation in logging message templates.
dotnet_diagnostic.S3358.severity = none # Extract this nested ternary operation into an independent statement.
dotnet_diagnostic.S6602.severity = none # "Find" method should be used instead of the "FirstOrDefault"
dotnet_diagnostic.S6603.severity = none # The collection-specific "TrueForAll" method should be used instead of the "All"
dotnet_diagnostic.S6605.severity = none # Collection-specific "Exists" method should be used instead of the "Any"
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="Asyncify" Version="0.9.7" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.160" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.182" PrivateAssets="All" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.29.0.95321" PrivateAssets="All" />
Expand Down
2 changes: 1 addition & 1 deletion src/Atc.Rest.Client/Atc.Rest.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
Expand Down
23 changes: 19 additions & 4 deletions src/Atc.Rest.Client/Builder/MessageRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public IMessageRequestBuilder WithQueryParameter(
{
var objects = ((IEnumerable)value).Cast<object>().ToArray();
var sb = new StringBuilder();
for (int i = 0; i < objects.Length; i++)
for (var i = 0; i < objects.Length; i++)
{
sb.Append(i == 0
? Uri.EscapeDataString(objects[i].ToString())
Expand All @@ -157,6 +157,23 @@ public IMessageRequestBuilder WithQueryParameter(

queryMapper["#" + name] = sb.ToString();
}
else if (valueType.IsEnum)
{
queryMapper[name] = valueType
.GetTypeInfo()
.DeclaredMembers
.FirstOrDefault(x => x.Name == value.ToString())
?.GetCustomAttribute<EnumMemberAttribute>(inherit: false)
?.Value ?? value.ToString();
}
else if (value is DateTime dt)
{
queryMapper[name] = dt.ToString("o");
}
else if (value is DateTimeOffset dto)
{
queryMapper[name] = dto.ToString("o");
}
else
{
queryMapper[name] = value.ToString();
Expand Down Expand Up @@ -188,9 +205,7 @@ private Uri BuildRequestUri()

private static string BuildQueryKeyEqualValue(
KeyValuePair<string, string> pair)
{
return pair.Key.StartsWith("#", StringComparison.Ordinal)
=> pair.Key.StartsWith("#", StringComparison.Ordinal)
? $"{pair.Key.Replace("#", string.Empty)}={pair.Value}"
: $"{pair.Key}={Uri.EscapeDataString(pair.Value)}";
}
}
2 changes: 2 additions & 0 deletions src/Atc.Rest.Client/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
global using System.Diagnostics.CodeAnalysis;
global using System.Net;
global using System.Net.Http.Headers;
global using System.Reflection;
global using System.Runtime.Serialization;
global using System.Text;
global using System.Text.Json;
global using System.Text.Json.Serialization;
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.139" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.7.112" PrivateAssets="All" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# https://www.meziantou.net/enforcing-asynchronous-code-good-practices-using-a-roslyn-analyzer.htm
dotnet_diagnostic.MA0004.severity = none # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/Meziantou/MA0004.md
dotnet_diagnostic.MA0016.severity = none # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/Meziantou/MA0016.md

dotnet_diagnostic.MA0051.severity = none # Method Length

# Microsoft - Code Analysis
# https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/
Expand Down
7 changes: 4 additions & 3 deletions test/Atc.Rest.Client.Tests/Atc.Rest.Client.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
53 changes: 53 additions & 0 deletions test/Atc.Rest.Client.Tests/Builder/MessageRequestBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,59 @@ public void Should_Replace_Query_Parameters(
.Be($"/api?foo={fooValue}&bar={barValue}");
}

[Theory]
[InlineAutoNSubstituteData("/api")]
public void Should_Replace_Query_Parameters_With_Enum_Member_Value(string template)
{
const OperatorRole operatorRole = OperatorRole.Owner;
var sut = CreateSut(template);

sut.WithQueryParameter("operatorRole", operatorRole);
var message = sut.Build(HttpMethod.Post);

message!
.RequestUri!
.ToString()
.Should()
.Be("/api?operatorRole=owner");
}

[Theory]
[InlineAutoNSubstituteData("/api")]
public void Should_Replace_Query_Parameters_With_DateTime(string template)
{
var from = DateTime.UtcNow;

var sut = CreateSut(template);

sut.WithQueryParameter("from", from);
var message = sut.Build(HttpMethod.Post);

message!
.RequestUri!
.ToString()
.Should()
.Be($"/api?from={Uri.EscapeDataString(from.ToString("o"))}");
}

[Theory]
[InlineAutoNSubstituteData("/api")]
public void Should_Replace_Query_Parameters_With_DateTimeOffset(string template)
{
var from = DateTimeOffset.UtcNow;

var sut = CreateSut(template);

sut.WithQueryParameter("from", from);
var message = sut.Build(HttpMethod.Post);

message!
.RequestUri!
.ToString()
.Should()
.Be($"/api?from={Uri.EscapeDataString(from.ToString("o"))}");
}

[Theory]
[InlineAutoNSubstituteData("/api")]
public void Should_Replace_Query_Parameters_WithNull(
Expand Down
12 changes: 12 additions & 0 deletions test/Atc.Rest.Client.Tests/Builder/OperatorRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Atc.Rest.Client.Tests.Builder;

public enum OperatorRole
{
None = 0,

[EnumMember(Value = "owner")]
Owner = 1,

[EnumMember(Value = "admin")]
Admin = 2,
}
1 change: 1 addition & 0 deletions test/Atc.Rest.Client.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global using System.Net;
global using System.Net.Http.Headers;
global using System.Runtime.Serialization;
global using System.Text.Json;
global using Atc.Rest.Client.Builder;
global using Atc.Rest.Client.Serialization;
2 changes: 1 addition & 1 deletion test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc.Test" Version="1.0.89" />
<PackageReference Include="Atc.Test" Version="1.1.4" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" PrivateAssets="All" />
</ItemGroup>

Expand Down

0 comments on commit 8d33174

Please sign in to comment.