feat: added new KeepNullValueAttribute, added tests, added logic in source generator - #15
Open
PhantomGian wants to merge 1 commit into
Open
feat: added new KeepNullValueAttribute, added tests, added logic in source generator#15PhantomGian wants to merge 1 commit into
PhantomGian wants to merge 1 commit into
Conversation
Author
|
When approved move from unshipped to shipped |
There was a problem hiding this comment.
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
KeepNullValueAttributeas a new public API to opt specific properties into preservingnull. - Updated
YamlSourceGeneratorto 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:KeepNullValueAttributeinYamlify.Serialization, which, when applied to a property, ensures that null values are preserved during serialization and deserialization, regardless of theIgnoreNullValuesoption. [1] [2]YamlSourceGenerator.cs) to:[KeepNullValue]attribute on properties and adjust serialization logic so that such properties are always written, even if their value is null andIgnoreNullValuesis enabled. [1] [2][KeepNullValue]properties, overriding class defaults as appropriate.Testing and public API:
KeepNullValueAttributeTests.csto 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.These changes provide fine-grained control over null handling in YAML serialization, addressing scenarios where explicit nulls must be preserved for certain properties.