Browse Source

Update the MongoDB models/stores to use immutable collections

pull/2501/head
Kévin Chalet 2 months ago
parent
commit
ce91303ff5
  1. 14
      src/OpenIddict.MongoDb.Models/OpenIddictMongoDbApplication.cs
  2. 3
      src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs
  3. 6
      src/OpenIddict.MongoDb.Models/OpenIddictMongoDbResource.cs
  4. 8
      src/OpenIddict.MongoDb.Models/OpenIddictMongoDbScope.cs
  5. 95
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs
  6. 16
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs
  7. 48
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs
  8. 64
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs

14
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. /// Gets or sets the localized display names associated with the current application.
/// </summary> /// </summary>
[BsonElement("display_names"), BsonIgnoreIfNull] [BsonElement("display_names"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<string, string>? DisplayNames { get; set; } public virtual ImmutableDictionary<string, string>? DisplayNames { get; set; }
= ImmutableDictionary.Create<string, string>();
/// <summary> /// <summary>
/// Gets or sets the unique identifier associated with the current application. /// 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. /// Gets or sets the permissions associated with the current application.
/// </summary> /// </summary>
[BsonElement("permissions"), BsonIgnoreIfNull] [BsonElement("permissions"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? Permissions { get; set; } = []; public virtual ImmutableArray<string>? Permissions { get; set; }
/// <summary> /// <summary>
/// Gets or sets the post-logout redirect URIs associated with the current application. /// Gets or sets the post-logout redirect URIs associated with the current application.
/// </summary> /// </summary>
[BsonElement("post_logout_redirect_uris"), BsonIgnoreIfNull] [BsonElement("post_logout_redirect_uris"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? PostLogoutRedirectUris { get; set; } = []; public virtual ImmutableArray<string>? PostLogoutRedirectUris { get; set; }
/// <summary> /// <summary>
/// Gets or sets the additional properties associated with the current application. /// 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. /// Gets or sets the redirect URIs associated with the current application.
/// </summary> /// </summary>
[BsonElement("redirect_uris"), BsonIgnoreIfNull] [BsonElement("redirect_uris"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? RedirectUris { get; set; } = []; public virtual ImmutableArray<string>? RedirectUris { get; set; }
/// <summary> /// <summary>
/// Gets or sets the requirements associated with the current application. /// Gets or sets the requirements associated with the current application.
/// </summary> /// </summary>
[BsonElement("requirements"), BsonIgnoreIfNull] [BsonElement("requirements"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? Requirements { get; set; } = []; public virtual ImmutableArray<string>? Requirements { get; set; }
/// <summary> /// <summary>
/// Gets or sets the settings associated with the current application. /// Gets or sets the settings associated with the current application.
/// </summary> /// </summary>
[BsonElement("settings"), BsonIgnoreIfNull] [BsonElement("settings"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<string, string>? Settings { get; set; } public virtual ImmutableDictionary<string, string>? Settings { get; set; }
= ImmutableDictionary.Create<string, string>();
} }

3
src/OpenIddict.MongoDb.Models/OpenIddictMongoDbAuthorization.cs

@ -4,6 +4,7 @@
* the license and the contributors participating to this project. * the license and the contributors participating to this project.
*/ */
using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
namespace OpenIddict.MongoDb.Models; namespace OpenIddict.MongoDb.Models;
@ -48,7 +49,7 @@ public class OpenIddictMongoDbAuthorization
/// Gets or sets the scopes associated with the current authorization. /// Gets or sets the scopes associated with the current authorization.
/// </summary> /// </summary>
[BsonElement("scopes"), BsonIgnoreIfNull] [BsonElement("scopes"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? Scopes { get; set; } = []; public virtual ImmutableArray<string>? Scopes { get; set; }
/// <summary> /// <summary>
/// Gets or sets the status of the current authorization. /// Gets or sets the status of the current authorization.

6
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. /// Gets or sets the localized public descriptions associated with the current resource.
/// </summary> /// </summary>
[BsonElement("descriptions"), BsonIgnoreIfNull] [BsonElement("descriptions"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<string, string>? Descriptions { get; set; } public virtual ImmutableDictionary<string, string>? Descriptions { get; set; }
= ImmutableDictionary.Create<string, string>();
/// <summary> /// <summary>
/// Gets or sets the display name associated with the current resource. /// 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. /// Gets or sets the localized display names associated with the current resource.
/// </summary> /// </summary>
[BsonElement("display_names"), BsonIgnoreIfNull] [BsonElement("display_names"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<string, string>? DisplayNames { get; set; } public virtual ImmutableDictionary<string, string>? DisplayNames { get; set; }
= ImmutableDictionary.Create<string, string>();
/// <summary> /// <summary>
/// Gets or sets the unique identifier associated with the current resource. /// Gets or sets the unique identifier associated with the current resource.

8
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. /// Gets or sets the localized public descriptions associated with the current scope.
/// </summary> /// </summary>
[BsonElement("descriptions"), BsonIgnoreIfNull] [BsonElement("descriptions"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<string, string>? Descriptions { get; set; } public virtual ImmutableDictionary<string, string>? Descriptions { get; set; }
= ImmutableDictionary.Create<string, string>();
/// <summary> /// <summary>
/// Gets or sets the display name associated with the current scope. /// 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. /// Gets or sets the localized display names associated with the current scope.
/// </summary> /// </summary>
[BsonElement("display_names"), BsonIgnoreIfNull] [BsonElement("display_names"), BsonIgnoreIfNull]
public virtual IReadOnlyDictionary<string, string>? DisplayNames { get; set; } public virtual ImmutableDictionary<string, string>? DisplayNames { get; set; }
= ImmutableDictionary.Create<string, string>();
/// <summary> /// <summary>
/// Gets or sets the unique identifier associated with the current scope. /// 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. /// Gets or sets the resources associated with the current scope.
/// </summary> /// </summary>
[BsonElement("resources"), BsonIgnoreIfNull] [BsonElement("resources"), BsonIgnoreIfNull]
public virtual IReadOnlyList<string>? Resources { get; set; } = []; public virtual ImmutableArray<string>? Resources { get; set; }
} }

95
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs

@ -245,14 +245,9 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (application.DisplayNames is not { Count: > 0 }) return new(application.DisplayNames is { Count: > 0 } names
{ ? names.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value)
return new(ImmutableDictionary.Create<CultureInfo, string>()); : []);
}
return new(application.DisplayNames.ToImmutableDictionary(
static pair => CultureInfo.GetCultureInfo(pair.Key),
static pair => pair.Value));
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -282,12 +277,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (application.Permissions is not { Count: > 0 }) return new(application.Permissions is { IsDefaultOrEmpty: false } permissions ? permissions : []);
{
return new([]);
}
return new([.. application.Permissions]);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -296,12 +286,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (application.PostLogoutRedirectUris is not { Count: > 0 }) return new(application.PostLogoutRedirectUris is { IsDefaultOrEmpty: false } uris ? uris : []);
{
return new([]);
}
return new([.. application.PostLogoutRedirectUris]);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -331,12 +316,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (application.RedirectUris is not { Count: > 0 }) return new(application.RedirectUris is { IsDefaultOrEmpty: false } uris ? uris : []);
{
return new([]);
}
return new([.. application.RedirectUris]);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -344,12 +324,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (application.Requirements is not { Count: > 0 }) return new(application.Requirements is { IsDefaultOrEmpty: false } requirements ? requirements : []);
{
return new([]);
}
return new([.. application.Requirements]);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -357,12 +332,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (application.Settings is not { Count: > 0 }) return new(application.Settings is { IsEmpty: false } settings ? settings : []);
{
return new(ImmutableDictionary.Create<string, string>());
}
return new(application.Settings.ToImmutableDictionary());
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -498,16 +468,9 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (names is not { Count: > 0 }) application.DisplayNames = names is { Count: > 0 }
{ ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value)
application.DisplayNames = null; : null;
return ValueTask.CompletedTask;
}
application.DisplayNames = names.ToImmutableDictionary(
static pair => pair.Key.Name,
static pair => pair.Value);
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }
@ -530,14 +493,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (permissions.IsDefaultOrEmpty) application.Permissions = permissions is { IsDefaultOrEmpty: false } ? permissions : null;
{
application.Permissions = null;
return ValueTask.CompletedTask;
}
application.Permissions = permissions.ToImmutableList();
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }
@ -548,14 +504,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (uris.IsDefaultOrEmpty) application.PostLogoutRedirectUris = uris is { IsDefaultOrEmpty: false } ? uris : null;
{
application.PostLogoutRedirectUris = null;
return ValueTask.CompletedTask;
}
application.PostLogoutRedirectUris = uris.ToImmutableList();
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }
@ -602,14 +551,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (uris.IsDefaultOrEmpty) application.RedirectUris = uris is { IsDefaultOrEmpty: false } ? uris : null;
{
application.RedirectUris = null;
return ValueTask.CompletedTask;
}
application.RedirectUris = uris.ToImmutableList();
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }
@ -620,14 +562,7 @@ public class OpenIddictMongoDbApplicationStore<
{ {
ArgumentNullException.ThrowIfNull(application); ArgumentNullException.ThrowIfNull(application);
if (requirements.IsDefaultOrEmpty) application.Requirements = requirements is { IsDefaultOrEmpty: false } ? requirements : null;
{
application.Requirements = null;
return ValueTask.CompletedTask;
}
application.Requirements = requirements.ToImmutableList();
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }

16
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs

@ -279,12 +279,7 @@ public class OpenIddictMongoDbAuthorizationStore<
{ {
ArgumentNullException.ThrowIfNull(authorization); ArgumentNullException.ThrowIfNull(authorization);
if (authorization.Scopes is not { Count: > 0 }) return new(authorization.Scopes is { IsDefaultOrEmpty: false } scopes ? scopes : []);
{
return new([]);
}
return new([.. authorization.Scopes]);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -542,14 +537,7 @@ public class OpenIddictMongoDbAuthorizationStore<
{ {
ArgumentNullException.ThrowIfNull(authorization); ArgumentNullException.ThrowIfNull(authorization);
if (scopes.IsDefaultOrEmpty) authorization.Scopes = scopes is { IsDefaultOrEmpty: false } ? scopes : null;
{
authorization.Scopes = null;
return ValueTask.CompletedTask;
}
authorization.Scopes = scopes.ToImmutableList();
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }

48
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbResourceStore.cs

@ -177,14 +177,9 @@ public class OpenIddictMongoDbResourceStore<
{ {
ArgumentNullException.ThrowIfNull(resource); ArgumentNullException.ThrowIfNull(resource);
if (resource.Descriptions is not { Count: > 0 }) return new(resource.Descriptions is { Count: > 0 } descriptions
{ ? descriptions.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value)
return new(ImmutableDictionary.Create<CultureInfo, string>()); : []);
}
return new(resource.Descriptions.ToImmutableDictionary(
static pair => CultureInfo.GetCultureInfo(pair.Key),
static pair => pair.Value));
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -200,14 +195,9 @@ public class OpenIddictMongoDbResourceStore<
{ {
ArgumentNullException.ThrowIfNull(resource); ArgumentNullException.ThrowIfNull(resource);
if (resource.DisplayNames is not { Count: > 0 }) return new(resource.DisplayNames is { Count: > 0 } names
{ ? names.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value)
return new(ImmutableDictionary.Create<CultureInfo, string>()); : []);
}
return new(resource.DisplayNames.ToImmutableDictionary(
static pair => CultureInfo.GetCultureInfo(pair.Key),
static pair => pair.Value));
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -324,16 +314,9 @@ public class OpenIddictMongoDbResourceStore<
{ {
ArgumentNullException.ThrowIfNull(resource); ArgumentNullException.ThrowIfNull(resource);
if (descriptions is not { Count: > 0 }) resource.Descriptions = descriptions is { Count: > 0 }
{ ? descriptions.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value)
resource.Descriptions = null; : null;
return ValueTask.CompletedTask;
}
resource.Descriptions = descriptions.ToImmutableDictionary(
static pair => pair.Key.Name,
static pair => pair.Value);
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }
@ -344,16 +327,9 @@ public class OpenIddictMongoDbResourceStore<
{ {
ArgumentNullException.ThrowIfNull(resource); ArgumentNullException.ThrowIfNull(resource);
if (names is not { Count: > 0 }) resource.DisplayNames = names is { Count: > 0 }
{ ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value)
resource.DisplayNames = null; : null;
return ValueTask.CompletedTask;
}
resource.DisplayNames = names.ToImmutableDictionary(
static pair => pair.Key.Name,
static pair => pair.Value);
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }

64
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs

@ -196,14 +196,9 @@ public class OpenIddictMongoDbScopeStore<
{ {
ArgumentNullException.ThrowIfNull(scope); ArgumentNullException.ThrowIfNull(scope);
if (scope.Descriptions is not { Count: > 0 }) return new(scope.Descriptions is { Count: > 0 } descriptions
{ ? descriptions.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value)
return new(ImmutableDictionary.Create<CultureInfo, string>()); : []);
}
return new(scope.Descriptions.ToImmutableDictionary(
static pair => CultureInfo.GetCultureInfo(pair.Key),
static pair => pair.Value));
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -219,14 +214,9 @@ public class OpenIddictMongoDbScopeStore<
{ {
ArgumentNullException.ThrowIfNull(scope); ArgumentNullException.ThrowIfNull(scope);
if (scope.DisplayNames is not { Count: > 0 }) return new(scope.DisplayNames is { Count: > 0 } names
{ ? names.ToImmutableDictionary(static pair => CultureInfo.GetCultureInfo(pair.Key), static pair => pair.Value)
return new(ImmutableDictionary.Create<CultureInfo, string>()); : []);
}
return new(scope.DisplayNames.ToImmutableDictionary(
static pair => CultureInfo.GetCultureInfo(pair.Key),
static pair => pair.Value));
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -271,12 +261,7 @@ public class OpenIddictMongoDbScopeStore<
{ {
ArgumentNullException.ThrowIfNull(scope); ArgumentNullException.ThrowIfNull(scope);
if (scope.Resources is not { Count: > 0 }) return new(scope.Resources is { IsDefaultOrEmpty: false } resources ? resources : []);
{
return new([]);
}
return new([.. scope.Resources]);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -356,16 +341,9 @@ public class OpenIddictMongoDbScopeStore<
{ {
ArgumentNullException.ThrowIfNull(scope); ArgumentNullException.ThrowIfNull(scope);
if (descriptions is not { Count: > 0 }) scope.Descriptions = descriptions is { Count: > 0 }
{ ? descriptions.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value)
scope.Descriptions = null; : null;
return ValueTask.CompletedTask;
}
scope.Descriptions = descriptions.ToImmutableDictionary(
static pair => pair.Key.Name,
static pair => pair.Value);
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }
@ -376,16 +354,9 @@ public class OpenIddictMongoDbScopeStore<
{ {
ArgumentNullException.ThrowIfNull(scope); ArgumentNullException.ThrowIfNull(scope);
if (names is not { Count: > 0 }) scope.DisplayNames = names is { Count: > 0 }
{ ? names.ToImmutableDictionary(static pair => pair.Key.Name, static pair => pair.Value)
scope.DisplayNames = null; : null;
return ValueTask.CompletedTask;
}
scope.DisplayNames = names.ToImmutableDictionary(
static pair => pair.Key.Name,
static pair => pair.Value);
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }
@ -451,14 +422,7 @@ public class OpenIddictMongoDbScopeStore<
{ {
ArgumentNullException.ThrowIfNull(scope); ArgumentNullException.ThrowIfNull(scope);
if (resources.IsDefaultOrEmpty) scope.Resources = resources is { IsDefaultOrEmpty: false } ? resources : null;
{
scope.Resources = null;
return ValueTask.CompletedTask;
}
scope.Resources = resources.ToImmutableList();
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }

Loading…
Cancel
Save