From 71c9734d5bc0c2b329e7a7980c798645fd29ed8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Mon, 6 Jul 2026 15:56:54 +0200 Subject: [PATCH] Optimize the creation of OpenIddictMessage instances --- .../Primitives/OpenIddictMessage.cs | 29 ++++++++----------- .../Primitives/OpenIddictMessageTests.cs | 2 +- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs b/src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs index d48dbe52..50c14c0b 100644 --- a/src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs +++ b/src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs @@ -59,12 +59,7 @@ public class OpenIddictMessage // While generally discouraged, JSON objects can contain multiple properties with // the same name. In this case, the last occurrence replaces the previous ones. - if (HasParameter(parameter.Name)) - { - RemoveParameter(parameter.Name); - } - - AddParameter(parameter.Name, parameter.Value); + Parameters[parameter.Name] = parameter.Value; } } @@ -87,12 +82,7 @@ public class OpenIddictMessage // While generally discouraged, JSON objects can contain multiple properties with // the same name. In this case, the last occurrence replaces the previous ones. - if (HasParameter(parameter.Key)) - { - RemoveParameter(parameter.Key); - } - - AddParameter(parameter.Key, parameter.Value); + Parameters[parameter.Key] = parameter.Value; } } @@ -113,7 +103,12 @@ public class OpenIddictMessage continue; } - AddParameter(parameter.Key, parameter.Value); + if (Parameters.ContainsKey(parameter.Key)) + { + throw new ArgumentException(SR.GetResourceString(SR.ID0191), nameof(parameters)); + } + + Parameters.Add(parameter.Key, parameter.Value); } } @@ -138,7 +133,7 @@ public class OpenIddictMessage // not be present more than once but derived specifications like the // token exchange specification deliberately allow specifying multiple // parameters with the same name to represent a multi-valued parameter. - AddParameter(parameter.Key, parameter.Select(parameter => parameter.Value).ToArray() switch + Parameters.Add(parameter.Key, parameter.Select(parameter => parameter.Value).ToArray() switch { [] => default, [string value] => new OpenIddictParameter(value), @@ -168,7 +163,7 @@ public class OpenIddictMessage // not be present more than once but derived specifications like the // token exchange specification deliberately allow specifying multiple // parameters with the same name to represent a multi-valued parameter. - AddParameter(parameter.Key, parameter.Value switch + Parameters.Add(parameter.Key, parameter.Value switch { { IsDefaultOrEmpty: true } => default, [string value] => new OpenIddictParameter(value), @@ -229,7 +224,7 @@ public class OpenIddictMessage // not be present more than once but derived specifications like the // token exchange specification deliberately allow specifying multiple // parameters with the same name to represent a multi-valued parameter. - AddParameter(parameter.Key, parameter.Value switch + Parameters.Add(parameter.Key, parameter.Value switch { [] => default, [string value] => new OpenIddictParameter(value), @@ -260,7 +255,7 @@ public class OpenIddictMessage // not be present more than once but derived specifications like the // token exchange specification deliberately allow specifying multiple // parameters with the same name to represent a multi-valued parameter. - AddParameter(name, parameters.GetValues(name) switch + Parameters.Add(name, parameters.GetValues(name) switch { null or [] => default, [string value] => new OpenIddictParameter(value), diff --git a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs index 05ee45ec..83e5e816 100644 --- a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs +++ b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs @@ -41,7 +41,7 @@ public class OpenIddictMessageTests ]); }); - Assert.Equal("name", exception.ParamName); + Assert.Equal("parameters", exception.ParamName); Assert.StartsWith(SR.GetResourceString(SR.ID0191), exception.Message); }