Describe the bug
When using System.Text.Json for serialization by setting WireMockServerSettings.DefaultJsonSerializer = new SystemTextJsonConverter(), a JSON response body cannot contain JSON reference properties. The same JSON works fine if using NewtonsoftJsonConverter. This behavior is enabled by default in Newtonsoft but requires configuration using built-in .NET serialization. WireMock.NET does not provide a reasonable way to enable this behavior.
Expected behavior:
JSON containing reference properties should be allowed for both Newtonsoft.Json and System.Text.Json serialization.
Test to reproduce
- Start WireMock server using a setting of
WireMockServerSettings.DefaultJsonSerializer = new SystemTextJsonConverter()
- Attempt to load a response mapping that contains a body of
"{"$id":"1","$values":[{"$ref":"1"}]}" - This is a simple example that represents a List<object> that contains a single reference to itself. This will fail.
- Set
WireMockServerSettings.DefaultJsonSerializer = new NewtonsoftJsonSerializer() (or remove serializer assignment all together) and re-run. This will succeed.
Other related info
A similar issue occurs if Newtonsoft is used by WireMock.NET for serialization and built-in .NET is used in code consuming a response body while using BodyAsJson for the response, even when setting JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve in the consumer. WireMock.NET will produce the response since it's using Newtonsoft, but the consumer can fail to read the response due to differences in reference handling between Newtonsoft and built-in .NET serialization. This makes it difficult to mock 3rd party APIs that use .NET's serialization as well as reference properties. A workaround is to stringify the 3rd party API response and use Body instead of BodyAsJson, but this makes WireMock.NET harder to work with.
Relevant .NET doc regarding preserving references: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/preserve-references
Describe the bug
When using
System.Text.Jsonfor serialization by settingWireMockServerSettings.DefaultJsonSerializer = new SystemTextJsonConverter(), a JSON response body cannot contain JSON reference properties. The same JSON works fine if usingNewtonsoftJsonConverter. This behavior is enabled by default in Newtonsoft but requires configuration using built-in .NET serialization. WireMock.NET does not provide a reasonable way to enable this behavior.Expected behavior:
JSON containing reference properties should be allowed for both
Newtonsoft.JsonandSystem.Text.Jsonserialization.Test to reproduce
WireMockServerSettings.DefaultJsonSerializer = new SystemTextJsonConverter()"{"$id":"1","$values":[{"$ref":"1"}]}"- This is a simple example that represents aList<object>that contains a single reference to itself. This will fail.WireMockServerSettings.DefaultJsonSerializer = new NewtonsoftJsonSerializer()(or remove serializer assignment all together) and re-run. This will succeed.Other related info
A similar issue occurs if Newtonsoft is used by WireMock.NET for serialization and built-in .NET is used in code consuming a response body while using
BodyAsJsonfor the response, even when settingJsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preservein the consumer. WireMock.NET will produce the response since it's using Newtonsoft, but the consumer can fail to read the response due to differences in reference handling between Newtonsoft and built-in .NET serialization. This makes it difficult to mock 3rd party APIs that use .NET's serialization as well as reference properties. A workaround is to stringify the 3rd party API response and useBodyinstead ofBodyAsJson, but this makes WireMock.NET harder to work with.Relevant .NET doc regarding preserving references: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/preserve-references