Browse Source

Optimize the creation of OpenIddictMessage instances

pull/2534/head
Kévin Chalet 3 months ago
parent
commit
71c9734d5b
  1. 29
      src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs
  2. 2
      test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs

29
src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs

@ -59,12 +59,7 @@ public class OpenIddictMessage
// While generally discouraged, JSON objects can contain multiple properties with // While generally discouraged, JSON objects can contain multiple properties with
// the same name. In this case, the last occurrence replaces the previous ones. // the same name. In this case, the last occurrence replaces the previous ones.
if (HasParameter(parameter.Name)) Parameters[parameter.Name] = parameter.Value;
{
RemoveParameter(parameter.Name);
}
AddParameter(parameter.Name, parameter.Value);
} }
} }
@ -87,12 +82,7 @@ public class OpenIddictMessage
// While generally discouraged, JSON objects can contain multiple properties with // While generally discouraged, JSON objects can contain multiple properties with
// the same name. In this case, the last occurrence replaces the previous ones. // the same name. In this case, the last occurrence replaces the previous ones.
if (HasParameter(parameter.Key)) Parameters[parameter.Key] = parameter.Value;
{
RemoveParameter(parameter.Key);
}
AddParameter(parameter.Key, parameter.Value);
} }
} }
@ -113,7 +103,12 @@ public class OpenIddictMessage
continue; 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 // not be present more than once but derived specifications like the
// token exchange specification deliberately allow specifying multiple // token exchange specification deliberately allow specifying multiple
// parameters with the same name to represent a multi-valued parameter. // 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, [] => default,
[string value] => new OpenIddictParameter(value), [string value] => new OpenIddictParameter(value),
@ -168,7 +163,7 @@ public class OpenIddictMessage
// not be present more than once but derived specifications like the // not be present more than once but derived specifications like the
// token exchange specification deliberately allow specifying multiple // token exchange specification deliberately allow specifying multiple
// parameters with the same name to represent a multi-valued parameter. // 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, { IsDefaultOrEmpty: true } => default,
[string value] => new OpenIddictParameter(value), [string value] => new OpenIddictParameter(value),
@ -229,7 +224,7 @@ public class OpenIddictMessage
// not be present more than once but derived specifications like the // not be present more than once but derived specifications like the
// token exchange specification deliberately allow specifying multiple // token exchange specification deliberately allow specifying multiple
// parameters with the same name to represent a multi-valued parameter. // 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, [] => default,
[string value] => new OpenIddictParameter(value), [string value] => new OpenIddictParameter(value),
@ -260,7 +255,7 @@ public class OpenIddictMessage
// not be present more than once but derived specifications like the // not be present more than once but derived specifications like the
// token exchange specification deliberately allow specifying multiple // token exchange specification deliberately allow specifying multiple
// parameters with the same name to represent a multi-valued parameter. // 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, null or [] => default,
[string value] => new OpenIddictParameter(value), [string value] => new OpenIddictParameter(value),

2
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); Assert.StartsWith(SR.GetResourceString(SR.ID0191), exception.Message);
} }

Loading…
Cancel
Save