!DataSerializationHelper.SerializerSettings.SerializeReadOnlyTypes
See original GitHub issueDescribe the bug
Read-only data cannot be used as dynamic data, because the serializer ignores read-only types. Use-case for read-only data is when the application subject for testing has that kind of data.
Steps To Reproduce
Conceptually in C# Interactive window, showing example of read-only data not being serialized unless SerializeReadOnlyTypes=true
;
#r "System.Runtime.Serialization.Json"
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
readonly struct Foo1 { public Foo1(int a) { A = a; } public int A { get; } }
var ms1 = new MemoryStream();
new DataContractJsonSerializer(typeof(Foo1), new DataContractJsonSerializerSettings()).WriteObject(ms1, new Foo1(42));
[DataContract]
readonly struct Foo2 { public Foo2(int a) { A = a; } [DataMember]public int A { get; } }
var ms2 = new MemoryStream();
new DataContractJsonSerializer(typeof(Foo2), new DataContractJsonSerializerSettings { SerializeReadOnlyTypes = true }).WriteObject(ms2, new Foo2(42));
//Encoding.UTF8.GetString(ms1.ToArray()) => "{}" // So with default value SerializeReadOnlyTypes=false it will never work
//Encoding.UTF8.GetString(ms2.ToArray()) => "{\"A\":42}"
For DataSerializationHelperTests
instead, though I have not managed to build under VS2022 yet;
public void DataSerializerShouldRoundTripReadOnlyData()
{
var source = new ReadOnlyDataWrapper { ReadOnlyData = new ReadOnlyDataType(42) };
var actual = DataSerializationHelper.Deserialize(
DataSerializationHelper.Serialize(new object[] { source }));
Verify(actual.Length == 1);
Verify(actual[0].GetType() == typeof(ReadOnlyDataWrapper));
Verify(((ReadOnlyDataWrapper)actual[0]).ReadOnlyData.Equals(source));
}
public class ReadOnlyDataWrapper
{
public ReadOnlyDataType ReadOnlyData { get; set; }
}
// Should serialize fine if DataSerializationHelper.SerializerSettings.SerializeReadOnlyTypes = true
[DataContract]
public readonly struct ReadOnlyDataType
{
public ReadOnlyDataType(int a)
{
A = a;
}
[DataMember]
public int A { get; }
}
Expected behavior
Dynamic test data with read-only types should not go lost.
Actual behavior
Read-only types in dynamic test data lose their values.
Additional context
Providing PR for your convenience.
Issue Analytics
- State:
- Created 8 months ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
DynamicDataAttribute losts type of the value of structure's ...
DynamicDataAttribute losts type of the value of structure's property when said property has System.Object type #1067. SnarkOrBoojum2 opened this ...
Read more >DataContractSerializerSettings Class (System.Runtime. ...
Specifies data contract serializer settings. ... SerializeReadOnlyTypes. Gets or sets a value that specifies whether to serialize read only types.
Read more >DataContractSerializerSettings.SerializeReadOnlyTypes ...
Gets or sets a value that specifies whether to serialize read only types.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I will have a deeper look after but we already have a couple of issues related to serialization of data during discovery (see #1462). So far the best option I see would be to use
BinaryFormatter
but I fear it will be flagged internally for the security issues it raises. I will sync with runtime team to see what they would suggest for this case.Thanks for the contribution and detailed explanations @aliquid. I will make sure to come back to you asap.