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

YAML serialization can use new OmitDefaultsOrEmpty #40

Closed
premun opened this issue Mar 15, 2022 · 4 comments
Closed

YAML serialization can use new OmitDefaultsOrEmpty #40

premun opened this issue Mar 15, 2022 · 4 comments

Comments

@premun
Copy link

premun commented Mar 15, 2022

.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults) //New as of YamlDotNet 8.0.0: https://github.com/aaubry/YamlDotNet/wiki/Serialization.Serializer#configuredefaultvalueshandlingdefaultvalueshandling. This will not show null properties, e.g. "app-name: " will not display when the value is null, as the value is nullable

I was also serializing pipeline YAML and often had the resulting YAML full of empty arrays, things like:

steps: []

This is just an FYI that I added functionality there that also omits empty collections. Not sure if you are hitting this but might be of use to you too.

https://github.com/aaubry/YamlDotNet/releases/tag/v11.2.0
aaubry/YamlDotNet#608

@samsmithnz
Copy link
Owner

Do you have an example? Wouldn't this just skip steps from the output?

@premun
Copy link
Author

premun commented Mar 15, 2022

Yes, it would. But at least in Azure Pipelines YAML you don't want to show some of these when empty.

I don't know how you deal with collections but in Sharpliner, I pre-initialize List<T> because C# 10 can give you an amazingly brief syntax using the implicit Add().

Say you have this:

class SomeClass
{
    List<string> SomeProperty { get; set; } = new();
}

then you can do this:

var x = new SomeClass
{
    SomeProperty = {
        "foo",
        "bar",
    }
}

However, this implicit Add only works if the List<T> property is non-null and initialized. Just calls Add on it.

Now, let's say you have pipeline triggers and want to use this - this means your Trigger property on the pipeline needs to be an initialized list but you don't really want your YAML to be always:

triggers: []

You only want triggers to be serialized when it's not empty.

Now, I don't know GH Actions much so I don't know if you have problems like this. Or you might just have null values for your Lists. Then you can disregard this :)

@premun
Copy link
Author

premun commented Mar 15, 2022

Btw I reached out to you in a more reasonable manner on Teams but I guess you're off already (I didn't mean to ambush you here like this)

@samsmithnz
Copy link
Owner

I'm pretty sure that triggers: [] is valid, just ugly, as YAML is a superset of JSON. I think you can get around this by either not initializing the list, or using an array (with no items).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants