Newtonsoft.Json → System.Text.Json Migration#9956
Newtonsoft.Json → System.Text.Json Migration#9956NikolaMilosavljevic merged 6 commits intodotnet:mainfrom
Conversation
|
2 integration tests are failing - working on fixes. |
|
Fixed integration tests with cad05c0 Summary of fixes1.
|
...rosoft.TemplateEngine.Orchestrator.RunnableProjects/ValueForms/JsonEncodeValueFormFactory.cs
Show resolved
Hide resolved
After the Newtonsoft.Json → System.Text.Json migration (dotnet#9956), MockTemplateInfo's explicit ITemplateInfo.Tags and CacheParameters implementations throw NotImplementedException. Newtonsoft never hit these during serialization, but System.Text.Json walks all interface properties, causing TemplateDiscoveryMetadata.ToJObject() to crash in the SDK's CacheSearchCoordinatorTests. Return empty dictionaries instead of throwing. Both properties are [Obsolete] and the mock's real data lives in TagsCollection. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
After the Newtonsoft.Json → System.Text.Json migration (dotnet#9956), MockTemplateInfo's explicit ITemplateInfo.Tags and CacheParameters implementations throw NotImplementedException. Newtonsoft never hit these during serialization, but System.Text.Json walks all interface properties, causing TemplateDiscoveryMetadata.ToJObject() to crash in the SDK's CacheSearchCoordinatorTests. Return empty dictionaries instead of throwing. Both properties are [Obsolete] and the mock's real data lives in TagsCollection. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
## Problem After the Newtonsoft.Json → System.Text.Json migration ([#9956](#9956)), `MockTemplateInfo`'s explicit `ITemplateInfo.Tags` and `ITemplateInfo.CacheParameters` implementations throw `NotImplementedException`. Newtonsoft never visited these during serialization, but System.Text.Json walks all interface properties when serializing, causing `TemplateDiscoveryMetadata.ToJObject()` to crash: ``` System.NotImplementedException : The method or operation is not implemented. at MockTemplateInfo.ITemplateInfo.get_Tags() at System.Text.Json.Serialization...JsonPropertyInfo`1.GetMemberAndWriteJson ``` This breaks all `CacheSearchCoordinatorTests` in the SDK CI ([sdk#53613](dotnet/sdk#53613)). ## Fix Return empty dictionaries instead of throwing. Both properties are `[Obsolete]` — the mock's real tag data lives in the `TagsCollection` property.
Summary of changes
1. Core Shared Infrastructure (1 file)
src/Shared/JExtensions.cs) — Most significant change. This shared file (linked into multiple projects) was the central Newtonsoft.Json helper. Completely rewritten to useSystem.Text.Json.Nodes(JsonNode,JsonObject,JsonArray,JsonValue) instead ofJObject/JToken/JArray. Key changes:JsonObject/JsonNode/JsonArrayinstead of Newtonsoft typesParseJsonNode()/ParseJsonObject()helpers withJsonDocumentOptionsconfigured for comment handling and trailing commasToJsonString()for serialization usingJsonSerializerToString(),ToInt32(),Get<T>(), etc.) rewritten for STJ APIsJsonNodeComparerandJsonNodeEqualityComparerutility classes added2. Package References (5 files)
Newtonsoft.Jsonpackage reference; addedSystem.Text.JsonandJsonSchema.Net(for JSON schema validation in tests).Newtonsoft.JsonwithSystem.Text.Json, conditioned withCondition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'"to avoid NU1510 package pruning warnings (System.Text.Json is inbox for .NET 10+, but still needed fornet472/netstandard2.0):src/Microsoft.TemplateEngine.Edge/Microsoft.TemplateEngine.Edge.csprojsrc/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/Microsoft.TemplateEngine.Orchestrator.RunnableProjects.csprojsrc/Microsoft.TemplateEngine.Utils/Microsoft.TemplateEngine.Utils.csprojsrc/Microsoft.TemplateSearch.Common/Microsoft.TemplateSearch.Common.csprojNewtonsoft.Jsonreference.Newtonsoft.JsonwithJsonSchema.Netfor schema validation tests.3. Production Source Files (62 files)
Microsoft.TemplateEngine.Edge (6 files)
JObject/JTokentoJsonObject/JsonNodeMicrosoft.TemplateEngine.Orchestrator.RunnableProjects (32 files)
Microsoft.TemplateSearch.Common (11 files)
Tools/TemplateDiscovery (12 files)
JsonEncodeValueFormFactory.cs
Added
JavaScriptEncoder.UnsafeRelaxedJsonEscapingso"encodes as\"(matching Newtonsoft behavior) instead of STJ's default\u0022.4. Test Files (24 files)
All test files updated to use
System.Text.JsonAPIs instead of Newtonsoft:5. Test Data Fixes (4 files)
Removed duplicate
$schemaproperties from template.json files that Newtonsoft silently tolerated but STJ/JsonSchema.Net rejects:test_templates/TemplateConditionalProcessing/.template.config/template.jsontest_templates/Invalid/InvalidHostData/.template.config/template.jsontest_templates/SourceWithExcludeAndWithout/With/.template.config/template.jsontest_templates/SourceWithExcludeAndWithout/Without/.template.config/template.json6. Behavioral Differences Addressed
"True"/"False""true"/"false""encoding in JSON strings\"\u0022UnsafeRelaxedJsonEscapingArgumentExceptionJsonNode.ToString()formatToJsonString()is compact)ToJsonString()in assertions7. Validation
VMR build