From eea0e5284e62ae86c2d8117bd09c17d2149eb5c2 Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Wed, 27 Sep 2023 12:41:02 +0300 Subject: [PATCH] Rename CustomJsonSerializerContext types and files to better names --- ...erContext.cs => AuthenticationJsonSerializerContext.cs} | 2 +- .../Extensions/ClaimsPrincipalExtensions.cs | 3 ++- ...erContext.cs => AuthenticationJsonSerializerContext.cs} | 2 +- .../OAuthClientCredentialHandler.cs | 3 ++- .../OAuthClientCredentialHandlerTests.cs | 7 ++++--- 5 files changed, 10 insertions(+), 7 deletions(-) rename src/Tingle.AspNetCore.Authentication/{CustomJsonSerializerContext.cs => AuthenticationJsonSerializerContext.cs} (59%) rename src/Tingle.Extensions.Http.Authentication/{CustomJsonSerializerContext.cs => AuthenticationJsonSerializerContext.cs} (65%) diff --git a/src/Tingle.AspNetCore.Authentication/CustomJsonSerializerContext.cs b/src/Tingle.AspNetCore.Authentication/AuthenticationJsonSerializerContext.cs similarity index 59% rename from src/Tingle.AspNetCore.Authentication/CustomJsonSerializerContext.cs rename to src/Tingle.AspNetCore.Authentication/AuthenticationJsonSerializerContext.cs index 1a1f5b7..cf7e846 100644 --- a/src/Tingle.AspNetCore.Authentication/CustomJsonSerializerContext.cs +++ b/src/Tingle.AspNetCore.Authentication/AuthenticationJsonSerializerContext.cs @@ -3,4 +3,4 @@ namespace Tingle.AspNetCore.Authentication; [JsonSerializable(typeof(AddressClaim))] -internal partial class CustomJsonSerializerContext : JsonSerializerContext { } +internal partial class AuthenticationJsonSerializerContext : JsonSerializerContext { } diff --git a/src/Tingle.AspNetCore.Authentication/Extensions/ClaimsPrincipalExtensions.cs b/src/Tingle.AspNetCore.Authentication/Extensions/ClaimsPrincipalExtensions.cs index 834c136..94ff3ce 100644 --- a/src/Tingle.AspNetCore.Authentication/Extensions/ClaimsPrincipalExtensions.cs +++ b/src/Tingle.AspNetCore.Authentication/Extensions/ClaimsPrincipalExtensions.cs @@ -1,4 +1,5 @@ using Tingle.AspNetCore.Authentication; +using SC = Tingle.AspNetCore.Authentication.AuthenticationJsonSerializerContext; namespace System.Security.Claims; @@ -195,6 +196,6 @@ public static class ClaimsPrincipalExtensions if (principal == null) throw new ArgumentNullException(nameof(principal)); var json = principal.GetAddress(); if (string.IsNullOrWhiteSpace(json)) return default; - return Text.Json.JsonSerializer.Deserialize(json, CustomJsonSerializerContext.Default.AddressClaim); + return Text.Json.JsonSerializer.Deserialize(json, SC.Default.AddressClaim); } } diff --git a/src/Tingle.Extensions.Http.Authentication/CustomJsonSerializerContext.cs b/src/Tingle.Extensions.Http.Authentication/AuthenticationJsonSerializerContext.cs similarity index 65% rename from src/Tingle.Extensions.Http.Authentication/CustomJsonSerializerContext.cs rename to src/Tingle.Extensions.Http.Authentication/AuthenticationJsonSerializerContext.cs index 87ac810..55e952a 100644 --- a/src/Tingle.Extensions.Http.Authentication/CustomJsonSerializerContext.cs +++ b/src/Tingle.Extensions.Http.Authentication/AuthenticationJsonSerializerContext.cs @@ -3,4 +3,4 @@ namespace Tingle.Extensions.Http.Authentication; [JsonSerializable(typeof(OAuthClientCredentialHandler.OAuthTokenResponse))] -internal partial class CustomJsonSerializerContext : JsonSerializerContext { } +internal partial class AuthenticationJsonSerializerContext : JsonSerializerContext { } diff --git a/src/Tingle.Extensions.Http.Authentication/OAuthClientCredentialHandler.cs b/src/Tingle.Extensions.Http.Authentication/OAuthClientCredentialHandler.cs index 9875d21..5de661e 100644 --- a/src/Tingle.Extensions.Http.Authentication/OAuthClientCredentialHandler.cs +++ b/src/Tingle.Extensions.Http.Authentication/OAuthClientCredentialHandler.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using System.Text.Json; using System.Text.Json.Serialization; +using SC = Tingle.Extensions.Http.Authentication.AuthenticationJsonSerializerContext; namespace Tingle.Extensions.Http.Authentication; @@ -146,7 +147,7 @@ public OAuthClientCredentialHandler(HttpMessageHandler innerHandler, HttpClient? #endif Logger?.LogTrace("OAuth token response:\n\n{Response}\n\n{Body}", response.ToString(), body); response.EnsureSuccessStatusCode(); // ensure it succeeded - return JsonSerializer.Deserialize(body, CustomJsonSerializerContext.Default.OAuthTokenResponse); + return JsonSerializer.Deserialize(body, SC.Default.OAuthTokenResponse); } internal DateTimeOffset CalculateCacheEntryExpiry(DateTimeOffset expiresOn) diff --git a/tests/Tingle.Extensions.Http.Authentication.Tests/OAuthClientCredentialHandlerTests.cs b/tests/Tingle.Extensions.Http.Authentication.Tests/OAuthClientCredentialHandlerTests.cs index ce03c18..37eebf7 100644 --- a/tests/Tingle.Extensions.Http.Authentication.Tests/OAuthClientCredentialHandlerTests.cs +++ b/tests/Tingle.Extensions.Http.Authentication.Tests/OAuthClientCredentialHandlerTests.cs @@ -3,6 +3,7 @@ using System.Text; using System.Text.Json; using System.Text.Json.Nodes; +using SC = Tingle.Extensions.Http.Authentication.AuthenticationJsonSerializerContext; namespace Tingle.Extensions.Http.Authentication.Tests; @@ -18,7 +19,7 @@ public void OAuthTokenResponse_Deserializes_MultipleTypes() { // works with int var json = new JsonObject { ["access_token"] = "123", ["expires_in"] = 3599, ["expires_on"] = 1691507550, }; - var response = JsonSerializer.Deserialize(json, CustomJsonSerializerContext.Default.OAuthTokenResponse); + var response = JsonSerializer.Deserialize(json, SC.Default.OAuthTokenResponse); Assert.NotNull(response); Assert.Equal("123", response.AccessToken); Assert.Equal(3599, response.ExpiresIn); @@ -26,7 +27,7 @@ public void OAuthTokenResponse_Deserializes_MultipleTypes() // tests works with quoted strings json = new JsonObject { ["access_token"] = "123", ["expires_in"] = "3599", ["expires_on"] = "1691507550", }; - response = JsonSerializer.Deserialize(json, CustomJsonSerializerContext.Default.OAuthTokenResponse); + response = JsonSerializer.Deserialize(json, SC.Default.OAuthTokenResponse); Assert.NotNull(response); Assert.Equal("123", response.AccessToken); Assert.Equal(3599, response.ExpiresIn); @@ -34,7 +35,7 @@ public void OAuthTokenResponse_Deserializes_MultipleTypes() // tests works with quoted strings json = new JsonObject { ["access_token"] = "123", ["expires_in"] = null, ["expires_on"] = null, }; - response = JsonSerializer.Deserialize(json, CustomJsonSerializerContext.Default.OAuthTokenResponse); + response = JsonSerializer.Deserialize(json, SC.Default.OAuthTokenResponse); Assert.NotNull(response); Assert.Equal("123", response.AccessToken); Assert.Null(response.ExpiresIn);