Skip to content

feat: added new KeepNullValueAttribute, added tests, added logic in source generator - #15

Open
PhantomGian wants to merge 1 commit into
mainfrom
phantom/keep-null-on-props
Open

feat: added new KeepNullValueAttribute, added tests, added logic in source generator#15
PhantomGian wants to merge 1 commit into
mainfrom
phantom/keep-null-on-props

Conversation

@PhantomGian

@PhantomGian PhantomGian commented Jun 30, 2026

Copy link
Copy Markdown

This pull request introduces the [KeepNullValue] attribute, which allows properties to explicitly preserve null values during YAML serialization and deserialization, even when global or context-level settings would otherwise omit them. The changes include the attribute definition, updates to the source generator to honor the attribute, and comprehensive tests to verify its behavior.

Support for [KeepNullValue] attribute:

  • Added the KeepNullValueAttribute in Yamlify.Serialization, which, when applied to a property, ensures that null values are preserved during serialization and deserialization, regardless of the IgnoreNullValues option. [1] [2]
  • Updated the source generator (YamlSourceGenerator.cs) to:
    • Detect the [KeepNullValue] attribute on properties and adjust serialization logic so that such properties are always written, even if their value is null and IgnoreNullValues is enabled. [1] [2]
    • Ensure that, during deserialization, explicit nulls from YAML are honored for [KeepNullValue] properties, overriding class defaults as appropriate.
    • Add a helper method to check for the attribute on properties.

Testing and public API:

  • Added comprehensive unit tests in KeepNullValueAttributeTests.cs to verify serialization, deserialization, and round-trip scenarios involving [KeepNullValue] properties. These tests cover both reference types and nullable value types, and confirm correct behavior with and without the attribute under various settings.
  • Registered the new attribute in the public API file.

These changes provide fine-grained control over null handling in YAML serialization, addressing scenarios where explicit nulls must be preserved for certain properties.

@PhantomGian
PhantomGian requested a review from glucaci June 30, 2026 06:58
@PhantomGian

Copy link
Copy Markdown
Author

When approved move from unshipped to shipped

@glucaci glucaci changed the title Added new KeepNullValueAttribute, added tests, added logic in sourceg… feat: added new KeepNullValueAttribute, added tests, added logic in source generator Jul 1, 2026
@glucaci
glucaci requested a review from Copilot July 1, 2026 07:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new [KeepNullValue] attribute in Yamlify.Serialization and updates the source generator so annotated properties preserve explicit null values during YAML serialization/deserialization even when IgnoreNullValues is enabled. It also adds unit tests and updates the public API tracking file to include the new attribute.

Changes:

  • Added KeepNullValueAttribute as a new public API to opt specific properties into preserving null.
  • Updated YamlSourceGenerator to detect [KeepNullValue] and alter read/write generation accordingly.
  • Added comprehensive unit tests covering serialization, deserialization, and round-trip scenarios.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/Yamlify.Tests/Serialization/KeepNullValueAttributeTests.cs Adds unit tests verifying [KeepNullValue] behavior across serialization, deserialization, and round-trips.
src/Yamlify/Serialization/KeepNullValueAttribute.cs Introduces the new attribute type in the public serialization namespace.
src/Yamlify/PublicAPI.Unshipped.txt Registers the new public API surface for the attribute.
src/Yamlify.SourceGenerator/YamlSourceGenerator.cs Enhances generated read/write logic to honor [KeepNullValue].

Comment on lines +1856 to 1861
else if (HasKeepNullValue(prop))
{
// [KeepNullValue] forces unconditional write, bypassing options.IgnoreNullValues
sb.AppendLine($" writer.WritePropertyName({propertyNameCode});");
GeneratePropertyWrite(sb, propName, prop.Type, allTypes, "", siblingInfo);
}
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

Successfully merging this pull request may close these issues.

2 participants