Refused JSON contracts built by reflection and checked all of them in the trimmed build (#1152)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-03 18:29:50 +01:00
1 parent f864359290
commit 0b42c0288e
23 files changed
+535 -99

No files matched your search

+6 -4
View File
@@ -10,15 +10,17 @@
<TrimmerRemoveSymbols>true</TrimmerRemoveSymbols> <!-- Remove debug symbols -->
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization> <!-- Remove unsafe binary formatter serialization -->
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding> <!-- Remove unsafe UTF7 encoding -->
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault> <!-- Enable reflection for JSON serialization -->
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault> <!-- Blazor Server needs reflection for JSON of its own, while the options of AI Studio use generated contracts only. Without this line, PublishTrimmed switches reflection off, even for dotnet run -->
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings> <!-- Suppress trim analysis warnings -->
</PropertyGroup>
<!--
System.Text.Json builds an immutable collection, e.g., the ImmutableHashSet of a chat, through a factory method
like ImmutableHashSet.CreateRange, which it finds by reflection at runtime. Nothing calls that method directly,
so the trimmer removes it, and every type which holds such a collection fails to load and to save. Only a
published build shows this: debug builds and the tests are not trimmed. Hence, we keep the whole assembly.
like ImmutableHashSet.CreateRange. The contracts which AI Studio generates for its own options call that method
directly, so the trimmer keeps it, and the trim smoke test passes without the line below. A contract which
System.Text.Json builds by reflection, as it still does for the JSON of Blazor, finds the method at runtime
only, and nothing would keep it then. The whole assembly costs about 70 KB more than its trimmed part, so we
keep it, in case such a contract ever meets an immutable collection.
-->
<ItemGroup>
<TrimmerRootAssembly Include="System.Collections.Immutable" />