diff --git a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs index ba631049..87f09f94 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs +++ b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs @@ -69,8 +69,7 @@ public class OpenIddictMongoDbApplication /// Gets or sets the localized display names associated with the current application. /// [BsonElement("display_names"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary? DisplayNames { get; set; } - = ImmutableDictionary.Create(); + public virtual ImmutableDictionary? DisplayNames { get; set; } /// /// Gets or sets the unique identifier associated with the current application. @@ -88,13 +87,13 @@ public class OpenIddictMongoDbApplication /// Gets or sets the permissions associated with the current application. /// [BsonElement("permissions"), BsonIgnoreIfNull] - public virtual IReadOnlyList? Permissions { get; set; } = []; + public virtual ImmutableArray? Permissions { get; set; } /// /// Gets or sets the post-logout redirect URIs associated with the current application. /// [BsonElement("post_logout_redirect_uris"), BsonIgnoreIfNull] - public virtual IReadOnlyList? PostLogoutRedirectUris { get; set; } = []; + public virtual ImmutableArray? PostLogoutRedirectUris { get; set; } /// /// Gets or sets the additional properties associated with the current application. @@ -106,18 +105,17 @@ public class OpenIddictMongoDbApplication /// Gets or sets the redirect URIs associated with the current application. /// [BsonElement("redirect_uris"), BsonIgnoreIfNull] - public virtual IReadOnlyList? RedirectUris { get; set; } = []; + public virtual ImmutableArray? RedirectUris { get; set; } /// /// Gets or sets the requirements associated with the current application. /// [BsonElement("requirements"), BsonIgnoreIfNull] - public virtual IReadOnlyList? Requirements { get; set; } = []; + public virtual ImmutableArray? Requirements { get; set; } /// /// Gets or sets the settings associated with the current application. /// [BsonElement("settings"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary? Settings { get; set; } - = ImmutableDictionary.Create(); + public virtual ImmutableDictionary? Settings { get; set; } } diff --git a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs index f9e895ee..df91d6df 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs +++ b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs @@ -4,6 +4,7 @@ * the license and the contributors participating to this project. */ +using System.Collections.Immutable; using System.Diagnostics; namespace OpenIddict.MongoDb.Models; @@ -48,7 +49,7 @@ public class OpenIddictMongoDbAuthorization /// Gets or sets the scopes associated with the current authorization. /// [BsonElement("scopes"), BsonIgnoreIfNull] - public virtual IReadOnlyList? Scopes { get; set; } = []; + public virtual ImmutableArray? Scopes { get; set; } /// /// Gets or sets the status of the current authorization. diff --git a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbResource.cs b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbResource.cs index 73482b2b..76109501 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbResource.cs +++ b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbResource.cs @@ -31,8 +31,7 @@ public class OpenIddictMongoDbResource /// Gets or sets the localized public descriptions associated with the current resource. /// [BsonElement("descriptions"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary? Descriptions { get; set; } - = ImmutableDictionary.Create(); + public virtual ImmutableDictionary? Descriptions { get; set; } /// /// Gets or sets the display name associated with the current resource. @@ -44,8 +43,7 @@ public class OpenIddictMongoDbResource /// Gets or sets the localized display names associated with the current resource. /// [BsonElement("display_names"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary? DisplayNames { get; set; } - = ImmutableDictionary.Create(); + public virtual ImmutableDictionary? DisplayNames { get; set; } /// /// Gets or sets the unique identifier associated with the current resource. diff --git a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs index c7b2cb52..227c9866 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs +++ b/src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs @@ -31,8 +31,7 @@ public class OpenIddictMongoDbScope /// Gets or sets the localized public descriptions associated with the current scope. /// [BsonElement("descriptions"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary? Descriptions { get; set; } - = ImmutableDictionary.Create(); + public virtual ImmutableDictionary? Descriptions { get; set; } /// /// Gets or sets the display name associated with the current scope. @@ -44,8 +43,7 @@ public class OpenIddictMongoDbScope /// Gets or sets the localized display names associated with the current scope. /// [BsonElement("display_names"), BsonIgnoreIfNull] - public virtual IReadOnlyDictionary? DisplayNames { get; set; } - = ImmutableDictionary.Create(); + public virtual ImmutableDictionary? DisplayNames { get; set; } /// /// Gets or sets the unique identifier associated with the current scope. @@ -69,5 +67,5 @@ public class OpenIddictMongoDbScope /// Gets or sets the resources associated with the current scope. /// [BsonElement("resources"), BsonIgnoreIfNull] - public virtual IReadOnlyList? Resources { get; set; } = []; + public virtual ImmutableArray? Resources { get; set; } } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs index e1ede282..a6174f00 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs @@ -245,14 +245,9 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (application.DisplayNames is not { Count: > 0 }) - { - return new(ImmutableDictionary.Create()); - } - - return new(application.DisplayNames.ToImmutableDictionary( - static pair => CultureInfo.GetCultureInfo(pair.Key), - static pair => pair.Value)); + return new(application.DisplayNames is { Count: > 0 } names + ? names.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value) + : []); } /// @@ -282,12 +277,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (application.Permissions is not { Count: > 0 }) - { - return new([]); - } - - return new([.. application.Permissions]); + return new(application.Permissions is { IsDefaultOrEmpty: false } permissions ? permissions : []); } /// @@ -296,12 +286,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (application.PostLogoutRedirectUris is not { Count: > 0 }) - { - return new([]); - } - - return new([.. application.PostLogoutRedirectUris]); + return new(application.PostLogoutRedirectUris is { IsDefaultOrEmpty: false } uris ? uris : []); } /// @@ -331,12 +316,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (application.RedirectUris is not { Count: > 0 }) - { - return new([]); - } - - return new([.. application.RedirectUris]); + return new(application.RedirectUris is { IsDefaultOrEmpty: false } uris ? uris : []); } /// @@ -344,12 +324,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (application.Requirements is not { Count: > 0 }) - { - return new([]); - } - - return new([.. application.Requirements]); + return new(application.Requirements is { IsDefaultOrEmpty: false } requirements ? requirements : []); } /// @@ -357,12 +332,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (application.Settings is not { Count: > 0 }) - { - return new(ImmutableDictionary.Create()); - } - - return new(application.Settings.ToImmutableDictionary()); + return new(application.Settings is { IsEmpty: false } settings ? settings : []); } /// @@ -498,16 +468,9 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (names is not { Count: > 0 }) - { - application.DisplayNames = null; - - return ValueTask.CompletedTask; - } - - application.DisplayNames = names.ToImmutableDictionary( - static pair => pair.Key.Name, - static pair => pair.Value); + application.DisplayNames = names is { Count: > 0 } + ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) + : null; return ValueTask.CompletedTask; } @@ -530,14 +493,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (permissions.IsDefaultOrEmpty) - { - application.Permissions = null; - - return ValueTask.CompletedTask; - } - - application.Permissions = permissions.ToImmutableList(); + application.Permissions = permissions is { IsDefaultOrEmpty: false } ? permissions : null; return ValueTask.CompletedTask; } @@ -548,14 +504,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (uris.IsDefaultOrEmpty) - { - application.PostLogoutRedirectUris = null; - - return ValueTask.CompletedTask; - } - - application.PostLogoutRedirectUris = uris.ToImmutableList(); + application.PostLogoutRedirectUris = uris is { IsDefaultOrEmpty: false } ? uris : null; return ValueTask.CompletedTask; } @@ -602,14 +551,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (uris.IsDefaultOrEmpty) - { - application.RedirectUris = null; - - return ValueTask.CompletedTask; - } - - application.RedirectUris = uris.ToImmutableList(); + application.RedirectUris = uris is { IsDefaultOrEmpty: false } ? uris : null; return ValueTask.CompletedTask; } @@ -620,14 +562,7 @@ public class OpenIddictMongoDbApplicationStore< { ArgumentNullException.ThrowIfNull(application); - if (requirements.IsDefaultOrEmpty) - { - application.Requirements = null; - - return ValueTask.CompletedTask; - } - - application.Requirements = requirements.ToImmutableList(); + application.Requirements = requirements is { IsDefaultOrEmpty: false } ? requirements : null; return ValueTask.CompletedTask; } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs index ca28685f..6adaa4de 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs @@ -279,12 +279,7 @@ public class OpenIddictMongoDbAuthorizationStore< { ArgumentNullException.ThrowIfNull(authorization); - if (authorization.Scopes is not { Count: > 0 }) - { - return new([]); - } - - return new([.. authorization.Scopes]); + return new(authorization.Scopes is { IsDefaultOrEmpty: false } scopes ? scopes : []); } /// @@ -542,14 +537,7 @@ public class OpenIddictMongoDbAuthorizationStore< { ArgumentNullException.ThrowIfNull(authorization); - if (scopes.IsDefaultOrEmpty) - { - authorization.Scopes = null; - - return ValueTask.CompletedTask; - } - - authorization.Scopes = scopes.ToImmutableList(); + authorization.Scopes = scopes is { IsDefaultOrEmpty: false } ? scopes : null; return ValueTask.CompletedTask; } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs index dd1ab8c7..e751836f 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs @@ -177,14 +177,9 @@ public class OpenIddictMongoDbResourceStore< { ArgumentNullException.ThrowIfNull(resource); - if (resource.Descriptions is not { Count: > 0 }) - { - return new(ImmutableDictionary.Create()); - } - - return new(resource.Descriptions.ToImmutableDictionary( - static pair => CultureInfo.GetCultureInfo(pair.Key), - static pair => pair.Value)); + return new(resource.Descriptions is { Count: > 0 } descriptions + ? descriptions.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value) + : []); } /// @@ -200,14 +195,9 @@ public class OpenIddictMongoDbResourceStore< { ArgumentNullException.ThrowIfNull(resource); - if (resource.DisplayNames is not { Count: > 0 }) - { - return new(ImmutableDictionary.Create()); - } - - return new(resource.DisplayNames.ToImmutableDictionary( - static pair => CultureInfo.GetCultureInfo(pair.Key), - static pair => pair.Value)); + return new(resource.DisplayNames is { Count: > 0 } names + ? names.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value) + : []); } /// @@ -324,16 +314,9 @@ public class OpenIddictMongoDbResourceStore< { ArgumentNullException.ThrowIfNull(resource); - if (descriptions is not { Count: > 0 }) - { - resource.Descriptions = null; - - return ValueTask.CompletedTask; - } - - resource.Descriptions = descriptions.ToImmutableDictionary( - static pair => pair.Key.Name, - static pair => pair.Value); + resource.Descriptions = descriptions is { Count: > 0 } + ? descriptions.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) + : null; return ValueTask.CompletedTask; } @@ -344,16 +327,9 @@ public class OpenIddictMongoDbResourceStore< { ArgumentNullException.ThrowIfNull(resource); - if (names is not { Count: > 0 }) - { - resource.DisplayNames = null; - - return ValueTask.CompletedTask; - } - - resource.DisplayNames = names.ToImmutableDictionary( - static pair => pair.Key.Name, - static pair => pair.Value); + resource.DisplayNames = names is { Count: > 0 } + ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) + : null; return ValueTask.CompletedTask; } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs index e6ae3a9f..30787051 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs @@ -196,14 +196,9 @@ public class OpenIddictMongoDbScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (scope.Descriptions is not { Count: > 0 }) - { - return new(ImmutableDictionary.Create()); - } - - return new(scope.Descriptions.ToImmutableDictionary( - static pair => CultureInfo.GetCultureInfo(pair.Key), - static pair => pair.Value)); + return new(scope.Descriptions is { Count: > 0 } descriptions + ? descriptions.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value) + : []); } /// @@ -219,14 +214,9 @@ public class OpenIddictMongoDbScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (scope.DisplayNames is not { Count: > 0 }) - { - return new(ImmutableDictionary.Create()); - } - - return new(scope.DisplayNames.ToImmutableDictionary( - static pair => CultureInfo.GetCultureInfo(pair.Key), - static pair => pair.Value)); + return new(scope.DisplayNames is { Count: > 0 } names + ? names.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value) + : []); } /// @@ -271,12 +261,7 @@ public class OpenIddictMongoDbScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (scope.Resources is not { Count: > 0 }) - { - return new([]); - } - - return new([.. scope.Resources]); + return new(scope.Resources is { IsDefaultOrEmpty: false } resources ? resources : []); } /// @@ -356,16 +341,9 @@ public class OpenIddictMongoDbScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (descriptions is not { Count: > 0 }) - { - scope.Descriptions = null; - - return ValueTask.CompletedTask; - } - - scope.Descriptions = descriptions.ToImmutableDictionary( - static pair => pair.Key.Name, - static pair => pair.Value); + scope.Descriptions = descriptions is { Count: > 0 } + ? descriptions.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) + : null; return ValueTask.CompletedTask; } @@ -376,16 +354,9 @@ public class OpenIddictMongoDbScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (names is not { Count: > 0 }) - { - scope.DisplayNames = null; - - return ValueTask.CompletedTask; - } - - scope.DisplayNames = names.ToImmutableDictionary( - static pair => pair.Key.Name, - static pair => pair.Value); + scope.DisplayNames = names is { Count: > 0 } + ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value) + : null; return ValueTask.CompletedTask; } @@ -451,14 +422,7 @@ public class OpenIddictMongoDbScopeStore< { ArgumentNullException.ThrowIfNull(scope); - if (resources.IsDefaultOrEmpty) - { - scope.Resources = null; - - return ValueTask.CompletedTask; - } - - scope.Resources = resources.ToImmutableList(); + scope.Resources = resources is { IsDefaultOrEmpty: false } ? resources : null; return ValueTask.CompletedTask; }