Skip to content

Commit

Permalink
Rename CustomJsonSerializerContext types and files to better names
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Sep 27, 2023
1 parent 05d3b41 commit eea0e52
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
namespace Tingle.AspNetCore.Authentication;

[JsonSerializable(typeof(AddressClaim))]
internal partial class CustomJsonSerializerContext : JsonSerializerContext { }
internal partial class AuthenticationJsonSerializerContext : JsonSerializerContext { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Tingle.AspNetCore.Authentication;
using SC = Tingle.AspNetCore.Authentication.AuthenticationJsonSerializerContext;

namespace System.Security.Claims;

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
namespace Tingle.Extensions.Http.Authentication;

[JsonSerializable(typeof(OAuthClientCredentialHandler.OAuthTokenResponse))]
internal partial class CustomJsonSerializerContext : JsonSerializerContext { }
internal partial class AuthenticationJsonSerializerContext : JsonSerializerContext { }
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,23 +19,23 @@ 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);
Assert.Equal(1691507550, response.ExpiresOn);

// 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);
Assert.Equal(1691507550, response.ExpiresOn);

// 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);
Expand Down

0 comments on commit eea0e52

Please sign in to comment.