Skip to content

Commit dac7031

Browse files
committed
fix: Allow SavedCookbooks.json to be deserialized under NativeAOT
Context: https://github.com/unoplatform/uno/issues/20533#issuecomment-3715971106 Context: unoplatform/uno.extensions#3008 Context: unoplatform/uno.extensions#2996 Uno 6.5.x has (very) preliminary support for NativeAOT! Let's try it: sed -i '' 's/"Uno.Sdk": ".*"/"Uno.Sdk": "6.5.0-dev.104"/g' global.json dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop \ -bl Chefs/Chefs.csproj -p:SelfContained=true -p:UseSkiaRendering=true \ -p:PublishAot=true -p:IsAotCompatible=true Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs From the launch screen: * Click **Skip** * Click ** Sign in with Apple** * Click **❤️ Favorites** Result: no favorited recipes are shown. Worse, no useful error messages are written to the Terminal. What's wrong?! Part of what's wrong is unoplatform/uno.extensions#3008: the codepath that *should* be generating an error message writes the error message to a Dependency-Injection -originated `ILogger<BaseMockEndpoint>` instance, which only produces data if `.UseLogging()` is used, which was disabled for reasons nobody remembers. Re-enable `.UseLogging()`, and *now* we see why it fails: fail: Chefs.Client.Mock.BaseMockEndpoint[0] Failed to load SavedCookbooks.json System.InvalidOperationException: Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property. at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_JsonSerializerIsReflectionDisabled() + 0x27 at System.Text.Json.JsonSerializerOptions.ConfigureForJsonSerializer() + 0x2e at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions, Type) + 0x2d at System.Text.Json.JsonSerializer.Deserialize(String, Type, JsonSerializerOptions) + 0x1d at Uno.Extensions.Serialization.SerializerExtensions.FromString[T](ISerializer, String) + 0x39 at Chefs.Client.Mock.BaseMockEndpoint.<LoadData>d__3`1.MoveNext() + 0x144 Sort of. We see that it fails because we're missing `JsonTypeInfo<T>` for *something* that is required, but what? Enter unoplatform/uno.extensions#2996, which adds support for `$(JsonSerializerIsReflectionEnabledByDefault)`=false to Uno.Extensions.Serialization, providing a better error message: fail: Chefs.Client.Mock.BaseMockEndpoint[0] Failed to load SavedCookbooks.json System.InvalidOperationException: Reflection-based serialization has been disabled for this application. Use the IServiceCollection.AddJsonTypeInfoResolvers() or IHostBuilder.UseSerializationResolvers() extension methods to enable JSON deserialization for type `System.Collections.Generic.List`1[[System.Guid, System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]`. With a bit of squinting, we see that the type we need to support is `List<Guid>`. Update `MockEndpointContext` to support serializing `List<Guid>`: [JsonSerializable(typeof(List<Guid>))] partial class MockEndpointContext; Then update `ConfigureSerialization()` so that it's supported: services .AddJsonTypeInfo(MockEndpointContext.Default.ListGuid); This allows `List<Guid>` to be properly deserialized, which in turn allows **❤️ Favorites** to actually show favorited items.
1 parent a49f36b commit dac7031

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎Chefs/App.xaml.host.cs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ private void ConfigureAppBuilder(IApplicationBuilder builder)
4141
.UseEnvironment(Environments.Development)
4242
#endif
4343
// Temporary until uno loggig is fixed in extensions.
44-
//.UseLogging(configure: (context, logBuilder) =>
45-
//{
46-
// // Configure log levels for different categories of logging
47-
// logBuilder.SetMinimumLevel(
48-
// context.HostingEnvironment.IsDevelopment() ? LogLevel.Information : LogLevel.Warning);
49-
//}, enableUnoLogging: true)
44+
.UseLogging(configure: (context, logBuilder) =>
45+
{
46+
// Configure log levels for different categories of logging
47+
logBuilder.SetMinimumLevel(
48+
context.HostingEnvironment.IsDevelopment() ? LogLevel.Information : LogLevel.Warning);
49+
}, enableUnoLogging: true)
5050
.UseConfiguration(configure: configBuilder =>
5151
configBuilder
5252
.EmbeddedSource<App>()
@@ -120,6 +120,7 @@ private void ConfigureSerialization(HostBuilderContext context, IServiceCollecti
120120
.AddJsonTypeInfo(MockEndpointContext.Default.ListReviewData)
121121
.AddJsonTypeInfo(MockEndpointContext.Default.UserData)
122122
.AddJsonTypeInfo(MockEndpointContext.Default.Guid)
123+
.AddJsonTypeInfo(MockEndpointContext.Default.ListGuid)
123124
.AddJsonTypeInfo(MockEndpointContext.Default.ReviewData)
124125
.AddJsonTypeInfo(MockEndpointContext.Default.IEnumerableRecipeData)
125126
.AddJsonTypeInfo(MockEndpointContext.Default.LoginRequest);

‎Chefs/Business/Services/Serialization/MockEndpointContext.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Chefs.Data;
1515
[JsonSerializable(typeof(List<ReviewData>))]
1616
[JsonSerializable(typeof(UserData))]
1717
[JsonSerializable(typeof(Guid))]
18+
[JsonSerializable(typeof(List<Guid>))]
1819
[JsonSerializable(typeof(ReviewData))]
1920
[JsonSerializable(typeof(IEnumerable<RecipeData>))]
2021
[JsonSerializable(typeof(TimeSpanObject))]

0 commit comments

Comments
 (0)