From eb76755178297ab0f890c3efb65f043f97d56ab5 Mon Sep 17 00:00:00 2001 From: Per Kops Date: Fri, 28 Jun 2024 09:36:49 +0200 Subject: [PATCH 1/4] chore: update static code analyzers --- Directory.Build.props | 4 ++-- src/Directory.Build.props | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 1ae26a3..4ef8771 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -43,10 +43,10 @@ - + - + \ No newline at end of file diff --git a/src/Directory.Build.props b/src/Directory.Build.props index f516744..bce62e1 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -54,7 +54,7 @@ - + From a539ac22e6951f6504d8691f79b0b56237f0b0b1 Mon Sep 17 00:00:00 2001 From: Per Kops Date: Fri, 28 Jun 2024 09:36:58 +0200 Subject: [PATCH 2/4] chore: update nuget packages --- test/Atc.Rest.Client.Tests/Atc.Rest.Client.Tests.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Atc.Rest.Client.Tests/Atc.Rest.Client.Tests.csproj b/test/Atc.Rest.Client.Tests/Atc.Rest.Client.Tests.csproj index b9fc20f..c88227c 100644 --- a/test/Atc.Rest.Client.Tests/Atc.Rest.Client.Tests.csproj +++ b/test/Atc.Rest.Client.Tests/Atc.Rest.Client.Tests.csproj @@ -7,9 +7,9 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all From 19d934012201e910e8ac8cc9026cdf21f79c26dd Mon Sep 17 00:00:00 2001 From: Per Kops Date: Fri, 28 Jun 2024 09:37:08 +0200 Subject: [PATCH 3/4] chore: update coding-rules --- .editorconfig | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/.editorconfig b/.editorconfig index ec6e506..7a7d7e5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,6 @@ # ATC coding rules - https://github.com/atc-net/atc-coding-rules -# Version: 1.0.0 -# Updated: 25-09-2023 +# Version: 1.0.1 +# Updated: 03-06-2024 # Location: Root # Distribution: DotNet8 # Inspired by: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options @@ -464,10 +464,45 @@ 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.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.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 +dotnet_diagnostic.CA1849.severity = error # Call async methods when in an async method +dotnet_diagnostic.CA1854.severity = suggestion # Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method +dotnet_diagnostic.CA1855.severity = suggestion # Prefer 'Clear' over 'Fill' +dotnet_diagnostic.CA1856.severity = error # Incorrect usage of ConstantExpected attribute +dotnet_diagnostic.CA1857.severity = suggestion # A constant is expected for the parameter +dotnet_diagnostic.CA1858.severity = suggestion # Use 'StartsWith' instead of 'IndexOf' +dotnet_diagnostic.CA1859.severity = suggestion # Use concrete types when possible for improved performance +dotnet_diagnostic.CA1860.severity = suggestion # Avoid using 'Enumerable.Any()' extension method +dotnet_diagnostic.CA1861.severity = suggestion # Avoid constant arrays as arguments +dotnet_diagnostic.CA1862.severity = error # Use the 'StringComparison' method overloads to perform case-insensitive string comparisons +dotnet_diagnostic.CA1863.severity = suggestion # Use 'CompositeFormat' +dotnet_diagnostic.CA1864.severity = suggestion # Prefer the 'IDictionary.TryAdd(TKey, TValue)' method +dotnet_diagnostic.CA1865.severity = suggestion # Use char overload +dotnet_diagnostic.CA1866.severity = suggestion # Use char overload +dotnet_diagnostic.CA1867.severity = suggestion # Use char overload +dotnet_diagnostic.CA1868.severity = suggestion # Unnecessary call to 'Contains(item)' +dotnet_diagnostic.CA1869.severity = suggestion # Cache and reuse 'JsonSerializerOptions' instances +dotnet_diagnostic.CA1870.severity = suggestion # Use a cached 'SearchValues' instance dotnet_diagnostic.CA2007.severity = suggestion # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/CA2007.md +dotnet_diagnostic.CA2017.severity = error # Parameter count mismatch +dotnet_diagnostic.CA2018.severity = error # The count argument to Buffer.BlockCopy should specify the number of bytes to copy +dotnet_diagnostic.CA2019.severity = error # ThreadStatic fields should not use inline initialization +dotnet_diagnostic.CA2021.severity = error # Don't call Enumerable.Cast or Enumerable.OfType with incompatible types +dotnet_diagnostic.CA2250.severity = suggestion # Use ThrowIfCancellationRequested +dotnet_diagnostic.CA2252.severity = suggestion # Opt-in to preview features should be used with caution +dotnet_diagnostic.CA2253.severity = error # Named placeholders should not be numeric values +dotnet_diagnostic.CA2254.severity = suggestion # Template should be a static expression +dotnet_diagnostic.CA2255.severity = suggestion # The ModuleInitializer attribute should not be used in libraries +dotnet_diagnostic.CA2259.severity = error # Ensure ThreadStatic is only used with static fields +dotnet_diagnostic.CA2260.severity = error # Implement generic math interfaces correctly +dotnet_diagnostic.CA2261.severity = error # Do not use ConfigureAwaitOptions.SuppressThrowing with Task dotnet_diagnostic.IDE0005.severity = warning # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/IDE0005.md dotnet_diagnostic.IDE0058.severity = none # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/IDE0058.md From a824dc4e505ba17a99830a1fc72dec944e52f9c3 Mon Sep 17 00:00:00 2001 From: Per Kops Date: Fri, 28 Jun 2024 09:37:51 +0200 Subject: [PATCH 4/4] fix: ensure we can properly read response content with incorrectly defined serializer - e.g. for Unauthorized response --- .../Builder/MessageResponseBuilder.cs | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Atc.Rest.Client/Builder/MessageResponseBuilder.cs b/src/Atc.Rest.Client/Builder/MessageResponseBuilder.cs index 499f20d..116ad3c 100644 --- a/src/Atc.Rest.Client/Builder/MessageResponseBuilder.cs +++ b/src/Atc.Rest.Client/Builder/MessageResponseBuilder.cs @@ -44,6 +44,7 @@ public IMessageResponseBuilder AddSuccessResponse( HttpStatusCode statusCode) => AddTypedResponse(statusCode, isSuccess: true); + [SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "OK.")] public async Task BuildResponseAsync( Func factory, CancellationToken cancellationToken) @@ -57,13 +58,28 @@ public async Task BuildResponseAsync( { var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); - return factory( - new EndpointResponse( - IsSuccessStatus(response), - response.StatusCode, - content, - GetSerializer(response.StatusCode)?.Invoke(content), - GetHeaders(response))); + object? contentResponse = content; + var contentSerializerDelegate = GetSerializer(response.StatusCode); + if (contentSerializerDelegate is not null) + { + try + { + contentResponse = contentSerializerDelegate.Invoke(content); + } + catch + { + // Swallow + } + } + + var endpointResponse = new EndpointResponse( + IsSuccessStatus(response), + response.StatusCode, + content, + contentResponse, + GetHeaders(response)); + + return factory(endpointResponse); } var contentObject = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);