Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/Yamlify.SourceGenerator/YamlSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed class YamlSourceGenerator : IIncrementalGenerator
private const string YamlSerializableAttributeGeneric = "Yamlify.Serialization.YamlSerializableAttribute<T>";
private const string YamlDerivedTypeMappingAttributeGeneric = "Yamlify.Serialization.YamlDerivedTypeMappingAttribute<TBase, TDerived>";
private const string YamlSerializerContextBase = "Yamlify.Serialization.YamlSerializerContext";
private const string KeepNullValueAttribute = "Yamlify.Serialization.KeepNullValueAttribute";

public void Initialize(IncrementalGeneratorInitializationContext context)
{
Expand Down Expand Up @@ -826,7 +827,12 @@ private static void GenerateReadMethod(StringBuilder sb, TypeToGenerate type, IR
if (canPreserveDefaults && settableProperties.Contains(prop))
{
var varName = $"_{propName.ToLowerInvariant()}";
if (prop.Type.IsValueType)
if (HasKeepNullValue(prop))
{
// [KeepNullValue] forces explicit nulls from YAML to be honored
sb.AppendLine($" _has{propName} = true;");
}
else if (prop.Type.IsValueType)
{
// Value types are always "set"
sb.AppendLine($" _has{propName} = true;");
Expand Down Expand Up @@ -1842,16 +1848,26 @@ private static void GenerateWriteMethod(StringBuilder sb, TypeToGenerate type, I
{
// Always skip if null, regardless of options.IgnoreNullValues
sb.AppendLine($" if ({string.Join(" && ", conditions)})");
sb.AppendLine(" {");
sb.AppendLine($" writer.WritePropertyName({propertyNameCode});");
GeneratePropertyWrite(sb, propName, prop.Type, allTypes, " ", siblingInfo);
sb.AppendLine(" }");
}
else if (HasKeepNullValue(prop))
{
// [KeepNullValue] forces unconditional write, bypassing options.IgnoreNullValues
sb.AppendLine($" writer.WritePropertyName({propertyNameCode});");
GeneratePropertyWrite(sb, propName, prop.Type, allTypes, "", siblingInfo);
}
Comment thread
Carael marked this conversation as resolved.
Resolved
else
{
// Wrap nullable properties with IgnoreNullValues and IgnoreEmptyObjects checks
sb.AppendLine($" if (!options.IgnoreNullValues || ({string.Join(" && ", conditions)}))");
sb.AppendLine(" {");
sb.AppendLine($" writer.WritePropertyName({propertyNameCode});");
GeneratePropertyWrite(sb, propName, prop.Type, allTypes, " ", siblingInfo);
sb.AppendLine(" }");
}
sb.AppendLine(" {");
sb.AppendLine($" writer.WritePropertyName({propertyNameCode});");
GeneratePropertyWrite(sb, propName, prop.Type, allTypes, " ", siblingInfo);
sb.AppendLine(" }");
}
else
{
Expand Down Expand Up @@ -2682,6 +2698,18 @@ private static int GetPropertyOrder(IPropertySymbol property)
return int.MaxValue;
}

private static bool HasKeepNullValue(IPropertySymbol property)
{
foreach (var attr in property.GetAttributes())
{
if (attr.AttributeClass?.ToDisplayString() == KeepNullValueAttribute)
{
return true;
}
}
return false;
}

/// <summary>
/// Gets the inheritance depth from a derived type to a base type.
/// Higher values mean the type is more specific (further down the inheritance chain).
Expand Down
4 changes: 3 additions & 1 deletion src/Yamlify/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
#nullable enable
Yamlify.Serialization.KeepNullValueAttribute
Yamlify.Serialization.KeepNullValueAttribute.KeepNullValueAttribute() -> void
11 changes: 11 additions & 0 deletions src/Yamlify/Serialization/KeepNullValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Yamlify.Serialization;

/// <summary>
/// When applied to a property, forces null values to be preserved during
/// serialization and deserialization, even if the enclosing context has
/// <see cref="YamlSourceGenerationOptionsAttribute.IgnoreNullValues"/> set to true.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class KeepNullValueAttribute : Attribute
{
}
Loading