Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change serializer settings to not send empty values, and deserialize empty collections #3381

Closed
nohwnd opened this issue Feb 17, 2022 · 2 comments
Labels
enhancement future Improvements which can be done in future and need team triage

Comments

@nohwnd
Copy link
Member

nohwnd commented Feb 17, 2022

With our current serializer settings there is difference between sending null, and sending empty collection. The serializer will do "the right thing" and give you what you sent. So either a null or empty collection. If no-one touches the property, or does not re-assign it to null we will get empty list on the other side.

IMHO there is almost never a case where you want to know if the collection was null, or empty, and we should change our serializer settings to:

  1. not send empty or null values
  2. always populate collections with empty collection even if we receive null, unless opted out (and I cannot recall any place where we would want to do that)
using ConsoleDump;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

JsonDataSerializer.Instance.DeserializePayload<AAA>(
    JsonDataSerializer.Instance.DeserializeMessage(
        JsonDataSerializer.Instance.SerializePayload("mmm", new AAA())).Dump()).Dump();

JsonDataSerializer.Instance.DeserializePayload<BBB>(
    JsonDataSerializer.Instance.DeserializeMessage(
        JsonDataSerializer.Instance.SerializePayload("mmm", new BBB())).Dump()).Dump();



public class AAA
{
    public IList<string> Names { get; set; } = new List<string>();
}

public class BBB
{
    public IList<string> Names { get; set; }
}

image

Screenshot shows the message on the receiving side before deserialization of the payload (yellow) and after deserialization (green).


In the approach I propose, the payload for both AAA and BBB would be empty ({}) and Names would deserialize as empty collection for both AAA and BBB.

Originally posted by @nohwnd in #3349 (comment)

@nohwnd nohwnd added enhancement future Improvements which can be done in future and need team triage labels Feb 17, 2022
@Evangelink
Copy link
Member

Nullability would help in our code to prevent us from setting null but we cannot control outside of our boundaries (e.g. user or serializer could set null). The only real safety would be to either prevent public setter or to have a backing field and check we don't pass null.

public class CCC
{
    private IList<string> _names = new List<string>();
    public IList<string> Names 
    {
        get => _names;
        set => _names = value ?? new List<string>();
    }
}

@nohwnd
Copy link
Member Author

nohwnd commented Jul 8, 2024

This is a new feature and won't be implemented, we are focusing on adding new features to Testing.Platform instead. https://aka.ms/testingplatform

@nohwnd nohwnd closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement future Improvements which can be done in future and need team triage
Projects
None yet
Development

No branches or pull requests

2 participants