From 892bca1548e7b987bbc5aba2ec37e8faf761acf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sun, 26 Jul 2026 15:49:38 +0200 Subject: [PATCH] Normalize collection empty checks in the Entity Framework/Entity Framework Core/MongoDB stores --- ...enIddictEntityFrameworkApplicationStore.cs | 6 +-- ...IddictEntityFrameworkAuthorizationStore.cs | 2 +- .../OpenIddictEntityFrameworkResourceStore.cs | 6 +-- .../OpenIddictEntityFrameworkScopeStore.cs | 6 +-- .../OpenIddictEntityFrameworkTokenStore.cs | 2 +- ...dictEntityFrameworkCoreApplicationStore.cs | 40 +++---------------- ...ctEntityFrameworkCoreAuthorizationStore.cs | 9 +---- ...nIddictEntityFrameworkCoreResourceStore.cs | 4 +- ...OpenIddictEntityFrameworkCoreScopeStore.cs | 13 ++---- .../OpenIddictMongoDbApplicationStore.cs | 11 +---- .../OpenIddictMongoDbAuthorizationStore.cs | 2 +- .../Stores/OpenIddictMongoDbResourceStore.cs | 2 +- .../Stores/OpenIddictMongoDbScopeStore.cs | 2 +- .../Stores/OpenIddictMongoDbTokenStore.cs | 2 +- .../OpenIddictServerIntegrationTestClient.cs | 6 +-- ...enIddictValidationIntegrationTestClient.cs | 6 +-- 16 files changed, 35 insertions(+), 84 deletions(-) diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs index 10b2166f..dc8d91bc 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs @@ -763,7 +763,7 @@ public class OpenIddictEntityFrameworkApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (names is not { Count: > 0 }) + if (names is not { IsEmpty: false }) { application.DisplayNames = null; @@ -878,7 +878,7 @@ public class OpenIddictEntityFrameworkApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { application.Properties = null; @@ -983,7 +983,7 @@ public class OpenIddictEntityFrameworkApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (settings is not { Count: > 0 }) + if (settings is not { IsEmpty: false }) { application.Settings = null; diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs index 98e4692e..022abac0 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs @@ -747,7 +747,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< { ArgumentNullException.ThrowIfNull(authorization); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { authorization.Properties = null; diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkResourceStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkResourceStore.cs index 2e37889e..783e5249 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkResourceStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkResourceStore.cs @@ -410,7 +410,7 @@ public class OpenIddictEntityFrameworkResourceStore< { ArgumentNullException.ThrowIfNull(resource); - if (descriptions is not { Count: > 0 }) + if (descriptions is not { IsEmpty: false }) { resource.Descriptions = null; @@ -456,7 +456,7 @@ public class OpenIddictEntityFrameworkResourceStore< { ArgumentNullException.ThrowIfNull(resource); - if (names is not { Count: > 0 }) + if (names is not { IsEmpty: false }) { resource.DisplayNames = null; @@ -502,7 +502,7 @@ public class OpenIddictEntityFrameworkResourceStore< { ArgumentNullException.ThrowIfNull(resource); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { resource.Properties = null; diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs index cdfea2dd..da3efa92 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs @@ -480,7 +480,7 @@ public class OpenIddictEntityFrameworkScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (descriptions is not { Count: > 0 }) + if (descriptions is not { IsEmpty: false }) { scope.Descriptions = null; @@ -526,7 +526,7 @@ public class OpenIddictEntityFrameworkScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (names is not { Count: > 0 }) + if (names is not { IsEmpty: false }) { scope.DisplayNames = null; @@ -572,7 +572,7 @@ public class OpenIddictEntityFrameworkScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { scope.Properties = null; diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs index 80d3afcb..79a07412 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs @@ -886,7 +886,7 @@ public class OpenIddictEntityFrameworkTokenStore< { ArgumentNullException.ThrowIfNull(token); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { token.Properties = null; diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs index 94882ef4..45074311 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs @@ -574,7 +574,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< { ArgumentNullException.ThrowIfNull(application); - application.DisplayNames = names is { Count: > 0 } + application.DisplayNames = names is { IsEmpty: false } ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) : null; @@ -596,14 +596,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (permissions.IsDefaultOrEmpty) - { - application.Permissions = null; - - return ValueTask.CompletedTask; - } - - application.Permissions = permissions.ToArray(); + application.Permissions = permissions is { IsDefaultOrEmpty: false } ? [.. permissions] : null; return ValueTask.CompletedTask; } @@ -614,14 +607,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (uris.IsDefaultOrEmpty) - { - application.PostLogoutRedirectUris = null; - - return ValueTask.CompletedTask; - } - - application.PostLogoutRedirectUris = uris.ToArray(); + application.PostLogoutRedirectUris = uris is { IsDefaultOrEmpty: false } ? [.. uris] : null; return ValueTask.CompletedTask; } @@ -643,14 +629,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (uris.IsDefaultOrEmpty) - { - application.RedirectUris = null; - - return ValueTask.CompletedTask; - } - - application.RedirectUris = uris.ToArray(); + application.RedirectUris = uris is { IsDefaultOrEmpty: false } ? [.. uris] : null; return ValueTask.CompletedTask; } @@ -660,14 +639,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (requirements.IsDefaultOrEmpty) - { - application.Requirements = null; - - return ValueTask.CompletedTask; - } - - application.Requirements = requirements.ToArray(); + application.Requirements = requirements is { IsDefaultOrEmpty: false } ? [.. requirements] : null; return ValueTask.CompletedTask; } @@ -678,7 +650,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< { ArgumentNullException.ThrowIfNull(application); - application.Settings = settings; + application.Settings = settings is { IsEmpty: false } ? settings : null; return ValueTask.CompletedTask; } diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs index 23fa9e1e..9a2e1ef8 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs @@ -825,14 +825,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< { ArgumentNullException.ThrowIfNull(authorization); - if (scopes.IsDefaultOrEmpty) - { - authorization.Scopes = null; - - return ValueTask.CompletedTask; - } - - authorization.Scopes = scopes.ToArray(); + authorization.Scopes = scopes is { IsDefaultOrEmpty: false } ? [.. scopes] : null; return ValueTask.CompletedTask; } diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreResourceStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreResourceStore.cs index 701e8683..2ee07702 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreResourceStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreResourceStore.cs @@ -332,7 +332,7 @@ public class OpenIddictEntityFrameworkCoreResourceStore< { ArgumentNullException.ThrowIfNull(resource); - resource.Descriptions = descriptions is { Count: > 0 } + resource.Descriptions = descriptions is { IsEmpty: false } ? descriptions.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) : null; @@ -355,7 +355,7 @@ public class OpenIddictEntityFrameworkCoreResourceStore< { ArgumentNullException.ThrowIfNull(resource); - resource.DisplayNames = names is { Count: > 0 } + resource.DisplayNames = names is { IsEmpty: false } ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) : null; diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs index fcb698f0..68b6e99b 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs @@ -360,7 +360,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< { ArgumentNullException.ThrowIfNull(scope); - scope.Descriptions = descriptions is { Count: > 0 } + scope.Descriptions = descriptions is { IsEmpty: false } ? descriptions.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) : null; @@ -383,7 +383,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< { ArgumentNullException.ThrowIfNull(scope); - scope.DisplayNames = names is { Count: > 0 } + scope.DisplayNames = names is { IsEmpty: false } ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) : null; @@ -416,14 +416,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (resources.IsDefaultOrEmpty) - { - scope.Resources = null; - - return ValueTask.CompletedTask; - } - - scope.Resources = resources.ToArray(); + scope.Resources = resources is { IsDefaultOrEmpty: false } ? [.. resources] : null; return ValueTask.CompletedTask; } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs index a80b3735..bbd2048a 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs @@ -510,7 +510,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { application.Properties = null; @@ -568,14 +568,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (settings is not { Count: > 0 }) - { - application.Settings = null; - - return ValueTask.CompletedTask; - } - - application.Settings = settings; + application.Settings = settings is { IsEmpty: false } ? settings : null; return ValueTask.CompletedTask; } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs index b350b43c..fa02cb24 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs @@ -491,7 +491,7 @@ public class OpenIddictMongoDbAuthorizationStore< { ArgumentNullException.ThrowIfNull(authorization); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { authorization.Properties = null; diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs index e751836f..2f985ac5 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs @@ -360,7 +360,7 @@ public class OpenIddictMongoDbResourceStore< { ArgumentNullException.ThrowIfNull(resource); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { resource.Properties = null; diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs index 30787051..60f34c03 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs @@ -387,7 +387,7 @@ public class OpenIddictMongoDbScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { scope.Properties = null; diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs index 541c18e4..6d719c7e 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs @@ -590,7 +590,7 @@ public class OpenIddictMongoDbTokenStore< { ArgumentNullException.ThrowIfNull(token); - if (properties is not { Count: > 0 }) + if (properties is not { IsEmpty: false }) { token.Properties = null; diff --git a/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs b/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs index 4eac4c33..01dfc3d0 100644 --- a/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs +++ b/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs @@ -363,7 +363,7 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable from parameter in parameters group parameter by parameter.Key into grouping let values = grouping.Select(parameter => parameter.Value) - select KeyValuePair.Create(grouping.Key, new StringValues(values.ToArray()))); + select KeyValuePair.Create(grouping.Key, new StringValues([.. values]))); } else if (string.Equals(message.Content?.Headers?.ContentType?.MediaType, "application/json", StringComparison.OrdinalIgnoreCase)) @@ -406,7 +406,7 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable from parameter in parameters group parameter by parameter.Key into grouping let values = grouping.Select(parameter => parameter.Value) - select KeyValuePair.Create(grouping.Key, new StringValues(values.ToArray()))); + select KeyValuePair.Create(grouping.Key, new StringValues([.. values]))); } else if (string.Equals(message.Content?.Headers?.ContentType?.MediaType, "text/plain", StringComparison.OrdinalIgnoreCase)) @@ -445,7 +445,7 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable from parameter in parameters group parameter by parameter.Key into grouping let values = grouping.Select(parameter => parameter.Value) - select KeyValuePair.Create(grouping.Key, new StringValues(values.ToArray()))); + select KeyValuePair.Create(grouping.Key, new StringValues([.. values]))); } return new OpenIddictResponse(); diff --git a/test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs b/test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs index a286ad59..305b3cda 100644 --- a/test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs +++ b/test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs @@ -363,7 +363,7 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable from parameter in parameters group parameter by parameter.Key into grouping let values = grouping.Select(parameter => parameter.Value) - select KeyValuePair.Create(grouping.Key, new StringValues(values.ToArray()))); + select KeyValuePair.Create(grouping.Key, new StringValues([.. values]))); } else if (string.Equals(message.Content?.Headers?.ContentType?.MediaType, "application/json", StringComparison.OrdinalIgnoreCase)) @@ -406,7 +406,7 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable from parameter in parameters group parameter by parameter.Key into grouping let values = grouping.Select(parameter => parameter.Value) - select KeyValuePair.Create(grouping.Key, new StringValues(values.ToArray()))); + select KeyValuePair.Create(grouping.Key, new StringValues([.. values]))); } else if (string.Equals(message.Content?.Headers?.ContentType?.MediaType, "text/plain", StringComparison.OrdinalIgnoreCase)) @@ -445,7 +445,7 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable from parameter in parameters group parameter by parameter.Key into grouping let values = grouping.Select(parameter => parameter.Value) - select KeyValuePair.Create(grouping.Key, new StringValues(values.ToArray()))); + select KeyValuePair.Create(grouping.Key, new StringValues([.. values]))); } return new OpenIddictResponse();