Browse Source

Do not mark the .NET 8.0 TFMs as AOT-compatible and use TypeDescriptor.RegisterType<T>()/TypeDescriptor.GetConverterFromRegisteredType() on .NET 9.0+

pull/2294/head
Kévin Chalet 10 months ago
parent
commit
03e1b28910
  1. 9
      Directory.Build.targets
  2. 2
      src/OpenIddict.Client/OpenIddictClientDispatcher.cs
  3. 9
      src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs
  4. 40
      src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs
  5. 40
      src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs
  6. 40
      src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs
  7. 40
      src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs
  8. 9
      src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs
  9. 40
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs
  10. 40
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs
  11. 40
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs
  12. 40
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs
  13. 2
      src/OpenIddict.Server/OpenIddictServerDispatcher.cs
  14. 2
      src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs

9
Directory.Build.targets

@ -16,11 +16,15 @@
</PropertyGroup>
<!--
Note: the trimming and Native AOT analyzers are only supported on .NET.
Note: the trimming and Native AOT analyzers are only supported on .NET. Since the OpenIddict
Entity Framework 6.x and Entity Framework Core stores internally use new trimming/AOT-friendly
TypeDescriptor APIs introduced in .NET 9.0, only the .NET 9.0+ TFMs are marked as AOT-compatible.
-->
<PropertyGroup
Condition=" $(RepoRelativeProjectDir.Contains('src')) And '$(TargetFrameworkIdentifier)' == '.NETCoreApp' ">
Condition=" $(RepoRelativeProjectDir.Contains('src')) And
'$(TargetFrameworkIdentifier)' == '.NETCoreApp' And
$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '9.0')) ">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
@ -112,6 +116,7 @@
<DefineConstants>$(DefineConstants);SUPPORTS_CERTIFICATE_LOADER</DefineConstants>
<DefineConstants>$(DefineConstants);SUPPORTS_JSON_ELEMENT_DEEP_EQUALS</DefineConstants>
<DefineConstants>$(DefineConstants);SUPPORTS_JSON_ELEMENT_PROPERTY_COUNT</DefineConstants>
<DefineConstants>$(DefineConstants);SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION</DefineConstants>
</PropertyGroup>
<PropertyGroup

2
src/OpenIddict.Client/OpenIddictClientDispatcher.cs

@ -121,7 +121,7 @@ public sealed class OpenIddictClientDispatcher : IOpenIddictClientDispatcher
{
for (var index = 0; index < descriptor.FilterTypes.Length; index++)
{
if (!(_provider.GetService(descriptor.FilterTypes[index]) is IOpenIddictClientHandlerFilter<TContext> filter))
if (_provider.GetService(descriptor.FilterTypes[index]) is not IOpenIddictClientHandlerFilter<TContext> filter)
{
throw new InvalidOperationException(SR.FormatID0099(descriptor.FilterTypes[index]));
}

9
src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs

@ -76,6 +76,15 @@ public sealed class OpenIddictEntityFrameworkBuilder
throw new InvalidOperationException(SR.GetResourceString(SR.ID0277));
}
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
// If the specified key type isn't a string (which is special-cased by the stores to avoid having to resolve
// a TypeDescriptor instance) and the platform supports type registration, register the key type to ensure the
// TypeDescriptor associated with that type will be preserved by the IL Linker and can be resolved at runtime.
if (typeof(TKey) != typeof(string))
{
TypeDescriptor.RegisterType<TKey>();
}
#endif
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictApplicationManager>(static provider =>
provider.GetRequiredService<OpenIddictApplicationManager<TApplication>>()));
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictAuthorizationManager>(static provider =>

40
src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs

@ -1174,11 +1174,23 @@ public class OpenIddictEntityFrameworkApplicationStore<
return default;
}
return (TKey?) GetConverter().ConvertFromInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (typeof(TKey) == typeof(string))
{
return (TKey?) (object?) identifier;
}
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
return (TKey?) converter.ConvertFromInvariantString(identifier);
}
}
/// <summary>
@ -1193,10 +1205,22 @@ public class OpenIddictEntityFrameworkApplicationStore<
return null;
}
return GetConverter().ConvertToInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (identifier is string value)
{
return value;
}
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
return converter.ConvertToInvariantString(identifier);
}
}
}

40
src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs

@ -975,11 +975,23 @@ public class OpenIddictEntityFrameworkAuthorizationStore<
return default;
}
return (TKey?) GetConverter().ConvertFromInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (typeof(TKey) == typeof(string))
{
return (TKey?) (object?) identifier;
}
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
return (TKey?) converter.ConvertFromInvariantString(identifier);
}
}
/// <summary>
@ -994,10 +1006,22 @@ public class OpenIddictEntityFrameworkAuthorizationStore<
return null;
}
return GetConverter().ConvertToInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (identifier is string value)
{
return value;
}
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
return converter.ConvertToInvariantString(identifier);
}
}
}

40
src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs

@ -758,11 +758,23 @@ public class OpenIddictEntityFrameworkScopeStore<
return default;
}
return (TKey?) GetConverter().ConvertFromInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (typeof(TKey) == typeof(string))
{
return (TKey?) (object?) identifier;
}
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
return (TKey?) converter.ConvertFromInvariantString(identifier);
}
}
/// <summary>
@ -777,10 +789,22 @@ public class OpenIddictEntityFrameworkScopeStore<
return null;
}
return GetConverter().ConvertToInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (identifier is string value)
{
return value;
}
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
return converter.ConvertToInvariantString(identifier);
}
}
}

40
src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs

@ -1139,11 +1139,23 @@ public class OpenIddictEntityFrameworkTokenStore<
return default;
}
return (TKey?) GetConverter().ConvertFromInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (typeof(TKey) == typeof(string))
{
return (TKey?) (object?) identifier;
}
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
return (TKey?) converter.ConvertFromInvariantString(identifier);
}
}
/// <summary>
@ -1158,10 +1170,22 @@ public class OpenIddictEntityFrameworkTokenStore<
return null;
}
return GetConverter().ConvertToInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (identifier is string value)
{
return value;
}
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
return converter.ConvertToInvariantString(identifier);
}
}
}

9
src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs

@ -89,6 +89,15 @@ public sealed class OpenIddictEntityFrameworkCoreBuilder
where TToken : OpenIddictEntityFrameworkCoreToken<TKey, TApplication, TAuthorization>
where TKey : notnull, IEquatable<TKey>
{
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
// If the specified key type isn't a string (which is special-cased by the stores to avoid having to resolve
// a TypeDescriptor instance) and the platform supports type registration, register the key type to ensure the
// TypeDescriptor associated with that type will be preserved by the IL Linker and can be resolved at runtime.
if (typeof(TKey) != typeof(string))
{
TypeDescriptor.RegisterType<TKey>();
}
#endif
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictApplicationManager>(static provider =>
provider.GetRequiredService<OpenIddictApplicationManager<TApplication>>()));
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictAuthorizationManager>(static provider =>

40
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs

@ -1255,11 +1255,23 @@ public class OpenIddictEntityFrameworkCoreApplicationStore<
return default;
}
return (TKey?) GetConverter().ConvertFromInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (typeof(TKey) == typeof(string))
{
return (TKey?) (object?) identifier;
}
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
return (TKey?) converter.ConvertFromInvariantString(identifier);
}
}
/// <summary>
@ -1274,10 +1286,22 @@ public class OpenIddictEntityFrameworkCoreApplicationStore<
return null;
}
return GetConverter().ConvertToInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (identifier is string value)
{
return value;
}
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
return converter.ConvertToInvariantString(identifier);
}
}
}

40
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs

@ -1182,11 +1182,23 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore<
return default;
}
return (TKey?) GetConverter().ConvertFromInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (typeof(TKey) == typeof(string))
{
return (TKey?) (object?) identifier;
}
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
return (TKey?) converter.ConvertFromInvariantString(identifier);
}
}
/// <summary>
@ -1201,10 +1213,22 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore<
return null;
}
return GetConverter().ConvertToInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (identifier is string value)
{
return value;
}
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
return converter.ConvertToInvariantString(identifier);
}
}
}

40
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs

@ -772,11 +772,23 @@ public class OpenIddictEntityFrameworkCoreScopeStore<
return default;
}
return (TKey?) GetConverter().ConvertFromInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (typeof(TKey) == typeof(string))
{
return (TKey?) (object?) identifier;
}
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
return (TKey?) converter.ConvertFromInvariantString(identifier);
}
}
/// <summary>
@ -791,10 +803,22 @@ public class OpenIddictEntityFrameworkCoreScopeStore<
return null;
}
return GetConverter().ConvertToInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (identifier is string value)
{
return value;
}
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
return converter.ConvertToInvariantString(identifier);
}
}
}

40
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs

@ -1342,11 +1342,23 @@ public class OpenIddictEntityFrameworkCoreTokenStore<
return default;
}
return (TKey?) GetConverter().ConvertFromInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (typeof(TKey) == typeof(string))
{
return (TKey?) (object?) identifier;
}
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
return (TKey?) converter.ConvertFromInvariantString(identifier);
}
}
/// <summary>
@ -1361,10 +1373,22 @@ public class OpenIddictEntityFrameworkCoreTokenStore<
return null;
}
return GetConverter().ConvertToInvariantString(identifier);
// Optimization: if the key is a string, directly return it as-is.
if (identifier is string value)
{
return value;
}
else
{
var converter =
#if SUPPORTS_TYPE_DESCRIPTOR_TYPE_REGISTRATION
TypeDescriptor.GetConverterFromRegisteredType(typeof(TKey));
#else
TypeDescriptor.GetConverter(typeof(TKey));
#endif
[UnconditionalSuppressMessage("Trimming", "IL2026",
Justification = "Only primitive types are supported as entity keys.")]
static TypeConverter GetConverter() => TypeDescriptor.GetConverter(typeof(TKey));
return converter.ConvertToInvariantString(identifier);
}
}
}

2
src/OpenIddict.Server/OpenIddictServerDispatcher.cs

@ -121,7 +121,7 @@ public sealed class OpenIddictServerDispatcher : IOpenIddictServerDispatcher
{
for (var index = 0; index < descriptor.FilterTypes.Length; index++)
{
if (!(_provider.GetService(descriptor.FilterTypes[index]) is IOpenIddictServerHandlerFilter<TContext> filter))
if (_provider.GetService(descriptor.FilterTypes[index]) is not IOpenIddictServerHandlerFilter<TContext> filter)
{
throw new InvalidOperationException(SR.FormatID0099(descriptor.FilterTypes[index]));
}

2
src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs

@ -121,7 +121,7 @@ public sealed class OpenIddictValidationDispatcher : IOpenIddictValidationDispat
{
for (var index = 0; index < descriptor.FilterTypes.Length; index++)
{
if (!(_provider.GetService(descriptor.FilterTypes[index]) is IOpenIddictValidationHandlerFilter<TContext> filter))
if (_provider.GetService(descriptor.FilterTypes[index]) is not IOpenIddictValidationHandlerFilter<TContext> filter)
{
throw new InvalidOperationException(SR.FormatID0099(descriptor.FilterTypes[index]));
}

Loading…
Cancel
Save