From 7c18ac300fb8d0bd6f73ab0d8860e6212a025f54 Mon Sep 17 00:00:00 2001 From: Matthew Asplund Date: Mon, 10 Jul 2023 20:59:35 -0500 Subject: [PATCH 01/12] Add IsTrimmable flag and fix json serializer warnings --- src/OidcClient/IIdentityTokenValidator.cs | 1 + .../Infrastructure/LogSerializer.cs | 12 +++++++++--- .../NoValidationIdentityTokenValidator.cs | 11 +++++++---- src/OidcClient/OidcClient.csproj | 14 ++++++++------ src/OidcClient/ResponseProcessor.cs | 3 --- src/OidcClient/SourceGenerationContext.cs | 19 +++++++++++++++++++ 6 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 src/OidcClient/SourceGenerationContext.cs diff --git a/src/OidcClient/IIdentityTokenValidator.cs b/src/OidcClient/IIdentityTokenValidator.cs index dfc7685..de3e48e 100644 --- a/src/OidcClient/IIdentityTokenValidator.cs +++ b/src/OidcClient/IIdentityTokenValidator.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; using IdentityModel.OidcClient.Results; diff --git a/src/OidcClient/Infrastructure/LogSerializer.cs b/src/OidcClient/Infrastructure/LogSerializer.cs index 133983c..5b9dfb2 100644 --- a/src/OidcClient/Infrastructure/LogSerializer.cs +++ b/src/OidcClient/Infrastructure/LogSerializer.cs @@ -1,9 +1,11 @@ // Copyright (c) Brock Allen & Dominick Baier. All rights reserved. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - using System.Text.Json; using System.Text.Json.Serialization; +#if NET5_0_OR_GREATER +using System.Text.Json.Serialization.Metadata; +#endif namespace IdentityModel.OidcClient.Infrastructure { @@ -34,9 +36,13 @@ static LogSerializer() /// /// The object. /// - public static string Serialize(object logObject) + public static string Serialize(T logObject) { +#if NET5_0_OR_GREATER + return Enabled ? JsonSerializer.Serialize(logObject, (JsonTypeInfo)SourceGenerationContext.Default.GetTypeInfo(typeof(T))) : "Logging has been disabled"; +#else return Enabled ? JsonSerializer.Serialize(logObject, JsonOptions) : "Logging has been disabled"; - } +#endif + } } } \ No newline at end of file diff --git a/src/OidcClient/NoValidationIdentityTokenValidator.cs b/src/OidcClient/NoValidationIdentityTokenValidator.cs index 22c550f..7e6bd0a 100644 --- a/src/OidcClient/NoValidationIdentityTokenValidator.cs +++ b/src/OidcClient/NoValidationIdentityTokenValidator.cs @@ -2,6 +2,7 @@ using System.Security.Claims; using System.Text; using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; using IdentityModel.OidcClient.Results; @@ -26,10 +27,13 @@ public Task ValidateAsync(string identityToken, O } var payload = Encoding.UTF8.GetString((Base64Url.Decode(parts[1]))); +#if NET5_0_OR_GREATER + var values = JsonSerializer.Deserialize>( + payload, SourceGenerationContext.Default.DictionaryStringJsonElement); +#else + var values = JsonSerializer.Deserialize>(payload); +#endif - var values = - JsonSerializer.Deserialize>(payload); - var claims = new List(); foreach (var element in values) { @@ -43,7 +47,6 @@ public Task ValidateAsync(string identityToken, O else { claims.Add(new Claim(element.Key, element.Value.ToString())); - } } diff --git a/src/OidcClient/OidcClient.csproj b/src/OidcClient/OidcClient.csproj index d645249..f00a44f 100644 --- a/src/OidcClient/OidcClient.csproj +++ b/src/OidcClient/OidcClient.csproj @@ -4,14 +4,14 @@ IdentityModel.OidcClient IdentityModel.OidcClient IdentityModel.OidcClient - - netstandard2.0 - + + netstandard2.0;net6.0 + OAuth2;OAuth 2.0;OpenID Connect;Security;Identity;IdentityServer RFC8252 compliant and certified OpenID Connect and OAuth 2.0 client library for native applications Dominick Baier;Brock Allen icon.jpg - + Apache-2.0 true @@ -24,12 +24,14 @@ embedded + + true + True - + ../../key.snk true true - diff --git a/src/OidcClient/ResponseProcessor.cs b/src/OidcClient/ResponseProcessor.cs index 9980d45..4cb7fa9 100644 --- a/src/OidcClient/ResponseProcessor.cs +++ b/src/OidcClient/ResponseProcessor.cs @@ -1,15 +1,12 @@ // Copyright (c) Brock Allen & Dominick Baier. All rights reserved. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - using IdentityModel.Client; using IdentityModel.OidcClient.Infrastructure; using IdentityModel.OidcClient.Results; using Microsoft.Extensions.Logging; using System; -using System.Collections.Generic; using System.Net; -using System.Security.Claims; using System.Threading; using System.Threading.Tasks; diff --git a/src/OidcClient/SourceGenerationContext.cs b/src/OidcClient/SourceGenerationContext.cs new file mode 100644 index 0000000..efa3362 --- /dev/null +++ b/src/OidcClient/SourceGenerationContext.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace IdentityModel.OidcClient +{ +#if NET5_0_OR_GREATER + [JsonSourceGenerationOptions( + WriteIndented = false, + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(AuthorizeState))] + [JsonSerializable(typeof(Dictionary))] + [JsonSerializable(typeof(OidcClientOptions))] + internal partial class SourceGenerationContext : JsonSerializerContext + { + } +#endif +} \ No newline at end of file From 794a16d52005207165a44e631af1414b75ca6360 Mon Sep 17 00:00:00 2001 From: Matthew Asplund Date: Fri, 1 Sep 2023 12:27:23 -0500 Subject: [PATCH 02/12] Pull in System.Text.Json to remove conditionals --- src/OidcClient/Infrastructure/LogSerializer.cs | 18 ++++++++---------- .../NoValidationIdentityTokenValidator.cs | 6 +----- src/OidcClient/OidcClient.csproj | 11 ++++++++++- src/OidcClient/SourceGenerationContext.cs | 5 ++--- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/OidcClient/Infrastructure/LogSerializer.cs b/src/OidcClient/Infrastructure/LogSerializer.cs index 5b9dfb2..3edd14d 100644 --- a/src/OidcClient/Infrastructure/LogSerializer.cs +++ b/src/OidcClient/Infrastructure/LogSerializer.cs @@ -3,9 +3,7 @@ using System.Text.Json; using System.Text.Json.Serialization; -#if NET5_0_OR_GREATER using System.Text.Json.Serialization.Metadata; -#endif namespace IdentityModel.OidcClient.Infrastructure { @@ -21,9 +19,11 @@ public static class LogSerializer public static bool Enabled = true; static readonly JsonSerializerOptions JsonOptions = new JsonSerializerOptions() - { - IgnoreNullValues = true, - WriteIndented = true + { +#pragma warning disable SYSLIB0020 // Type or member is obsolete + IgnoreNullValues = true, +#pragma warning restore SYSLIB0020 // Type or member is obsolete + WriteIndented = true }; static LogSerializer() @@ -38,11 +38,9 @@ static LogSerializer() /// public static string Serialize(T logObject) { -#if NET5_0_OR_GREATER - return Enabled ? JsonSerializer.Serialize(logObject, (JsonTypeInfo)SourceGenerationContext.Default.GetTypeInfo(typeof(T))) : "Logging has been disabled"; -#else - return Enabled ? JsonSerializer.Serialize(logObject, JsonOptions) : "Logging has been disabled"; -#endif + return Enabled ? + JsonSerializer.Serialize(logObject, (JsonTypeInfo)SourceGenerationContext.Default.GetTypeInfo(typeof(T))) : + "Logging has been disabled"; } } } \ No newline at end of file diff --git a/src/OidcClient/NoValidationIdentityTokenValidator.cs b/src/OidcClient/NoValidationIdentityTokenValidator.cs index 7e6bd0a..3f053c4 100644 --- a/src/OidcClient/NoValidationIdentityTokenValidator.cs +++ b/src/OidcClient/NoValidationIdentityTokenValidator.cs @@ -27,12 +27,8 @@ public Task ValidateAsync(string identityToken, O } var payload = Encoding.UTF8.GetString((Base64Url.Decode(parts[1]))); -#if NET5_0_OR_GREATER - var values = JsonSerializer.Deserialize>( + var values = JsonSerializer.Deserialize( payload, SourceGenerationContext.Default.DictionaryStringJsonElement); -#else - var values = JsonSerializer.Deserialize>(payload); -#endif var claims = new List(); foreach (var element in values) diff --git a/src/OidcClient/OidcClient.csproj b/src/OidcClient/OidcClient.csproj index f00a44f..206284d 100644 --- a/src/OidcClient/OidcClient.csproj +++ b/src/OidcClient/OidcClient.csproj @@ -6,6 +6,7 @@ IdentityModel.OidcClient netstandard2.0;net6.0 + latest OAuth2;OAuth 2.0;OpenID Connect;Security;Identity;IdentityServer RFC8252 compliant and certified OpenID Connect and OAuth 2.0 client library for native applications @@ -41,9 +42,17 @@ - + + + + + + + + + \ No newline at end of file diff --git a/src/OidcClient/SourceGenerationContext.cs b/src/OidcClient/SourceGenerationContext.cs index efa3362..f42be6d 100644 --- a/src/OidcClient/SourceGenerationContext.cs +++ b/src/OidcClient/SourceGenerationContext.cs @@ -4,16 +4,15 @@ namespace IdentityModel.OidcClient { -#if NET5_0_OR_GREATER [JsonSourceGenerationOptions( WriteIndented = false, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, - GenerationMode = JsonSourceGenerationMode.Metadata)] + GenerationMode = JsonSourceGenerationMode.Metadata, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] [JsonSerializable(typeof(AuthorizeState))] [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(OidcClientOptions))] internal partial class SourceGenerationContext : JsonSerializerContext { } -#endif } \ No newline at end of file From 6319d1b185f52f6a062353b2f7014de5c821d575 Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 12 Jan 2024 14:03:05 -0600 Subject: [PATCH 03/12] Add console program to test trimmable warnings --- test/TrimmableAnalysis/Program.cs | 2 ++ test/TrimmableAnalysis/TrimmableAnalysis.csproj | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 test/TrimmableAnalysis/Program.cs create mode 100644 test/TrimmableAnalysis/TrimmableAnalysis.csproj diff --git a/test/TrimmableAnalysis/Program.cs b/test/TrimmableAnalysis/Program.cs new file mode 100644 index 0000000..3751555 --- /dev/null +++ b/test/TrimmableAnalysis/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/test/TrimmableAnalysis/TrimmableAnalysis.csproj b/test/TrimmableAnalysis/TrimmableAnalysis.csproj new file mode 100644 index 0000000..66dd1f7 --- /dev/null +++ b/test/TrimmableAnalysis/TrimmableAnalysis.csproj @@ -0,0 +1,16 @@ + + + + Exe + net8.0 + enable + true + false + + + + + + + + From 728271fcdf57195ef74d9727e64ffb3f1949585c Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 12 Jan 2024 14:04:56 -0600 Subject: [PATCH 04/12] Update to latest IdentityModel version This is needed to make this assembly trimmable --- src/DPoP/DPoP.csproj | 2 +- src/OidcClient/OidcClient.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DPoP/DPoP.csproj b/src/DPoP/DPoP.csproj index f50f344..a0fe34d 100644 --- a/src/DPoP/DPoP.csproj +++ b/src/DPoP/DPoP.csproj @@ -39,7 +39,7 @@ - + diff --git a/src/OidcClient/OidcClient.csproj b/src/OidcClient/OidcClient.csproj index 0eeb04e..e087b86 100644 --- a/src/OidcClient/OidcClient.csproj +++ b/src/OidcClient/OidcClient.csproj @@ -40,7 +40,7 @@ - + From d5803ab8cffadce4e4f18536daf8649ef31913ba Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 12 Jan 2024 15:08:29 -0600 Subject: [PATCH 05/12] Add trimmable analysis to build --- build/Program.cs | 10 ++++++++-- test/TrimmableAnalysis/README.md | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 test/TrimmableAnalysis/README.md diff --git a/build/Program.cs b/build/Program.cs index 258d37b..bf3cb86 100644 --- a/build/Program.cs +++ b/build/Program.cs @@ -21,6 +21,7 @@ private static class Targets public const string Pack = "pack"; public const string SignBinary = "sign-binary"; public const string SignPackage = "sign-package"; + public const string TrimmableAnalysis = "trimmable-analysis"; } static async Task Main(string[] args) @@ -65,9 +66,14 @@ static async Task Main(string[] args) SignNuGet(); }); - Target("default", DependsOn(Targets.Test, Targets.Pack)); + Target(Targets.TrimmableAnalysis, () => + { + Run("dotnet", "publish test/TrimmableAnalysis -c Release -r win-x64"); + }); + + Target("default", DependsOn(Targets.Test, Targets.Pack, Targets.TrimmableAnalysis)); - Target("sign", DependsOn(Targets.Test, Targets.SignPackage)); + Target("sign", DependsOn(Targets.Test, Targets.SignPackage, Targets.TrimmableAnalysis)); await RunTargetsAndExitAsync(args, ex => ex is SimpleExec.ExitCodeException || ex.Message.EndsWith(envVarMissing)); } diff --git a/test/TrimmableAnalysis/README.md b/test/TrimmableAnalysis/README.md new file mode 100644 index 0000000..5199b0e --- /dev/null +++ b/test/TrimmableAnalysis/README.md @@ -0,0 +1,3 @@ +This project exists to facilitate analysis of trimmable warnings. + +See https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming \ No newline at end of file From baeed9dac51553d7ada31b74a8670c055dbb7a3f Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 12 Jan 2024 15:41:07 -0600 Subject: [PATCH 06/12] Run tests against .net 6, 7, and 8. --- .github/workflows/ci.yml | 7 +++++-- test/DPoPTests/DPoPTests.csproj | 2 +- test/JwtValidationTests/JwtValidationTests.csproj | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f86a62b..16510f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,10 +21,13 @@ jobs: steps: - uses: actions/checkout@af513c7a016048ae468971c52ed77d9562c7c819 - - name: Setup net6 + - name: Setup dotnet uses: actions/setup-dotnet@v1 with: - dotnet-version: '6.0.x' + dotnet-version: | + 6.0.x + 7.0.x + 8.0.x - run: dotnet --info diff --git a/test/DPoPTests/DPoPTests.csproj b/test/DPoPTests/DPoPTests.csproj index 5d1a056..fe35e29 100644 --- a/test/DPoPTests/DPoPTests.csproj +++ b/test/DPoPTests/DPoPTests.csproj @@ -1,7 +1,7 @@  - net6.0 + net6.0;net7.0;net8.0 diff --git a/test/JwtValidationTests/JwtValidationTests.csproj b/test/JwtValidationTests/JwtValidationTests.csproj index 25c74e0..584d208 100644 --- a/test/JwtValidationTests/JwtValidationTests.csproj +++ b/test/JwtValidationTests/JwtValidationTests.csproj @@ -1,7 +1,7 @@ - net6.0 + net6.0;net7.0;net8.0 From f241a2b28c76e499ca1942dfc5274168fc825dff Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 12 Jan 2024 15:44:46 -0600 Subject: [PATCH 07/12] Silly typo --- test/DPoPTests/DPoPTests.csproj | 2 +- test/JwtValidationTests/JwtValidationTests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/DPoPTests/DPoPTests.csproj b/test/DPoPTests/DPoPTests.csproj index fe35e29..ce0c530 100644 --- a/test/DPoPTests/DPoPTests.csproj +++ b/test/DPoPTests/DPoPTests.csproj @@ -1,7 +1,7 @@  - net6.0;net7.0;net8.0 + net6.0;net7.0;net8.0 diff --git a/test/JwtValidationTests/JwtValidationTests.csproj b/test/JwtValidationTests/JwtValidationTests.csproj index 584d208..e7822e7 100644 --- a/test/JwtValidationTests/JwtValidationTests.csproj +++ b/test/JwtValidationTests/JwtValidationTests.csproj @@ -1,7 +1,7 @@ - net6.0;net7.0;net8.0 + net6.0;net7.0;net8.0 From 36555a48088249ce6e6a7571b34c4c782b5d9563 Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 12 Jan 2024 15:54:06 -0600 Subject: [PATCH 08/12] Include trimmable analysis project in sln --- IdentityModel.OidcClient.sln | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/IdentityModel.OidcClient.sln b/IdentityModel.OidcClient.sln index cf52a7e..ac54fb3 100644 --- a/IdentityModel.OidcClient.sln +++ b/IdentityModel.OidcClient.sln @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DPoPTests", "test\DPoPTests EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleClientWithBrowserAndDPoP", "clients\ConsoleClientWithBrowserAndDPoP\ConsoleClientWithBrowserAndDPoP.csproj", "{7DDDA872-49C0-43F0-8B88-2531BF828DDE}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrimmableAnalysis", "test\TrimmableAnalysis\TrimmableAnalysis.csproj", "{672FE4E9-9071-4C59-95FC-F265DF6B2FF5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,6 +65,10 @@ Global {7DDDA872-49C0-43F0-8B88-2531BF828DDE}.Debug|Any CPU.Build.0 = Debug|Any CPU {7DDDA872-49C0-43F0-8B88-2531BF828DDE}.Release|Any CPU.ActiveCfg = Release|Any CPU {7DDDA872-49C0-43F0-8B88-2531BF828DDE}.Release|Any CPU.Build.0 = Release|Any CPU + {672FE4E9-9071-4C59-95FC-F265DF6B2FF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {672FE4E9-9071-4C59-95FC-F265DF6B2FF5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {672FE4E9-9071-4C59-95FC-F265DF6B2FF5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {672FE4E9-9071-4C59-95FC-F265DF6B2FF5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -76,6 +82,7 @@ Global {56EC3A58-33FE-4DCD-9D64-2A985DC58C2C} = {0B7CC363-CF34-4B40-A769-7E928A6B25AF} {0E1807AF-4142-4A3D-925C-BBA019E4E777} = {3DEB81D4-5B40-4D20-AC50-66D1CD6EA24A} {7DDDA872-49C0-43F0-8B88-2531BF828DDE} = {A4154BEB-4B4A-4A48-B75D-B52432304F36} + {672FE4E9-9071-4C59-95FC-F265DF6B2FF5} = {3DEB81D4-5B40-4D20-AC50-66D1CD6EA24A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {66951C2E-691F-408C-9283-F2455F390A9A} From 7629b7cbe6c81308bc6145b55d28082cf18eacb2 Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 9 Feb 2024 22:04:19 -0600 Subject: [PATCH 09/12] Treat trimmable analysis warnings as errors --- test/TrimmableAnalysis/TrimmableAnalysis.csproj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/TrimmableAnalysis/TrimmableAnalysis.csproj b/test/TrimmableAnalysis/TrimmableAnalysis.csproj index 66dd1f7..ea08cd2 100644 --- a/test/TrimmableAnalysis/TrimmableAnalysis.csproj +++ b/test/TrimmableAnalysis/TrimmableAnalysis.csproj @@ -6,11 +6,12 @@ enable true false + true - - + + From 51ef509b436705b31a9dbe51a278b290e8b89dcc Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 9 Feb 2024 22:05:27 -0600 Subject: [PATCH 10/12] xmldoc typo --- src/OidcClient/Infrastructure/LogSerializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OidcClient/Infrastructure/LogSerializer.cs b/src/OidcClient/Infrastructure/LogSerializer.cs index 54444e5..dd93101 100644 --- a/src/OidcClient/Infrastructure/LogSerializer.cs +++ b/src/OidcClient/Infrastructure/LogSerializer.cs @@ -14,7 +14,7 @@ public static class LogSerializer { /// /// Allows log serialization to be disabled, for example, for platforms - /// that don't support serialization of arbitarary objects to JSON. + /// that don't support serialization of arbitrary objects to JSON. /// public static bool Enabled = true; From 3d00419b96d07efb72865b7bc3ea224d7c0c59df Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 9 Feb 2024 22:05:56 -0600 Subject: [PATCH 11/12] Enable trimming warnings only for net6.0 --- src/OidcClient/OidcClient.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OidcClient/OidcClient.csproj b/src/OidcClient/OidcClient.csproj index e087b86..6c4da54 100644 --- a/src/OidcClient/OidcClient.csproj +++ b/src/OidcClient/OidcClient.csproj @@ -26,7 +26,7 @@ embedded - true + true True From 9b6ce96ebddd1d03566720f77be89ef62210ec25 Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Sun, 11 Feb 2024 21:06:47 -0600 Subject: [PATCH 12/12] Add log serializer overloads (prevents a breaking change) Changing the log serializer to support trimming means that we need to add the types that we will serialize into a serialization context. Unfortunately, the serializer is a public type that serializes arbitrary objects today. This is incompatible with trimming. The solution is to mark the existing API as [RequiresExternalCode], which gives trimmed callers of the API a warning, and to add overloads that accept the types that are in the generation context. --- src/OidcClient/IIdentityTokenValidator.cs | 1 - .../Infrastructure/LogSerializer.cs | 22 ++++++++++++++- test/OidcClient.Tests/LogSerializerTests.cs | 28 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test/OidcClient.Tests/LogSerializerTests.cs diff --git a/src/OidcClient/IIdentityTokenValidator.cs b/src/OidcClient/IIdentityTokenValidator.cs index de3e48e..dfc7685 100644 --- a/src/OidcClient/IIdentityTokenValidator.cs +++ b/src/OidcClient/IIdentityTokenValidator.cs @@ -1,4 +1,3 @@ -using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; using IdentityModel.OidcClient.Results; diff --git a/src/OidcClient/Infrastructure/LogSerializer.cs b/src/OidcClient/Infrastructure/LogSerializer.cs index dd93101..d70fb1e 100644 --- a/src/OidcClient/Infrastructure/LogSerializer.cs +++ b/src/OidcClient/Infrastructure/LogSerializer.cs @@ -1,6 +1,10 @@ // Copyright (c) Brock Allen & Dominick Baier. All rights reserved. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. +#if NET6_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; +#endif + using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; @@ -34,7 +38,23 @@ static LogSerializer() /// /// The object. /// - public static string Serialize(T logObject) +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("The log serializer uses reflection in a way that is incompatible with trimming")] +#endif + public static string Serialize(object logObject) + { + return Enabled ? JsonSerializer.Serialize(logObject, JsonOptions) : "Logging has been disabled"; + } + + internal static string Serialize(OidcClientOptions opts) => Serialize(opts); + internal static string Serialize(AuthorizeState state) => Serialize(state); + + /// + /// Serializes the specified object. + /// + /// The object. + /// + private static string Serialize(T logObject) { return Enabled ? JsonSerializer.Serialize(logObject, (JsonTypeInfo)SourceGenerationContext.Default.GetTypeInfo(typeof(T))) : diff --git a/test/OidcClient.Tests/LogSerializerTests.cs b/test/OidcClient.Tests/LogSerializerTests.cs new file mode 100644 index 0000000..646c10a --- /dev/null +++ b/test/OidcClient.Tests/LogSerializerTests.cs @@ -0,0 +1,28 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +using FluentAssertions; +using System; +using Xunit; +using IdentityModel.OidcClient.Infrastructure; + +namespace IdentityModel.OidcClient.Tests +{ + public class LogSerializerTests + { + [Fact] + // This test exists to make sure that the public api for the log + // serializer can serialize types that aren't part of the source + // generation context. There is an internal api that does use the source + // generation context types. We should always use that other serialize + // method internally in order to be trimmable. The overload in this test + // exists to avoid a breaking change. + public void LogSerializer_should_serialize_arbitrary_types() + { + // We instantiate the test class as an example of a class that is + // not (and won't ever be) in the generation context. + var act = () => LogSerializer.Serialize(new LogSerializerTests()); + act.Should().NotThrow(); + } + } +} \ No newline at end of file