From 61699eef506e13322e53401dadd23e706aa4bed1 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 3 Nov 2026 15:13:02 +0100 Subject: [PATCH] Added a test which checks that all JSON options of the app resolve through generated contracts only --- .../Serialization/JsonOptionsResolverTests.cs | 49 +++++++++++++++++++ app/Tests/Tests.csproj | 7 +++ 2 files changed, 56 insertions(+) create mode 100644 app/Tests/Serialization/JsonOptionsResolverTests.cs diff --git a/app/Tests/Serialization/JsonOptionsResolverTests.cs b/app/Tests/Serialization/JsonOptionsResolverTests.cs new file mode 100644 index 000000000..5403f894d --- /dev/null +++ b/app/Tests/Serialization/JsonOptionsResolverTests.cs @@ -0,0 +1,49 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +using AIStudio.TrimSmokeTest; + +namespace AIStudio.Tests.Serialization; + +/// +/// Checks that all options of the app get their contracts from source generation, never from reflection. +/// +/// +/// MWAIS0015 refuses options without a TypeInfoResolver while compiling, but cannot tell what the resolver does. +/// Only contexts throw for a type they do not know. Any other resolver, e.g., a DefaultJsonTypeInfoResolver which +/// a helper adds to the chain, builds the missing contract by reflection instead, and in a published build, the +/// code it needs for that may be gone. +/// +[TestFixture] +public sealed class JsonOptionsResolverTests +{ + private static IEnumerable AllOptions() + { + TestHost.PrepareStaticState(); + return AppJsonOptions.FindAll().Select(options => new TestCaseData(options.Value).SetArgDisplayNames(options.Key)); + } + + [TestCaseSource(nameof(AllOptions))] + public void TheOptionsResolveWithContextsOnly(JsonSerializerOptions options) + { + using (Assert.EnterMultipleScope()) + { + Assert.That(options.TypeInfoResolverChain, Is.Not.Empty); + Assert.That(options.TypeInfoResolverChain, Is.All.InstanceOf()); + } + } + + [Test] + public void FieldsAndPropertiesOfTheAppAreFound() + { + TestHost.PrepareStaticState(); + var names = AppJsonOptions.FindAll().Keys.ToList(); + + using (Assert.EnterMultipleScope()) + { + Assert.That(names, Does.Contain("SettingsManager.JSON_OPTIONS")); + Assert.That(names, Does.Contain("VisualBriefingJson.Canonical")); + Assert.That(names, Has.None.Contains("k__BackingField")); + } + } +} \ No newline at end of file diff --git a/app/Tests/Tests.csproj b/app/Tests/Tests.csproj index 5277865b1..ee96038d5 100644 --- a/app/Tests/Tests.csproj +++ b/app/Tests/Tests.csproj @@ -45,6 +45,13 @@ + + + + +