question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

!DataSerializationHelper.SerializerSettings.SerializeReadOnlyTypes

See original GitHub issue

Describe 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:open
  • Created 8 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Evangelinkcommented, Jan 9, 2023

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.

0reactions
Evangelinkcommented, Jan 10, 2023

Thanks for the contribution and detailed explanations @aliquid. I will make sure to come back to you asap.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found