Skip to content

Commit

Permalink
Ensure we put cultures in for null values, even when they are not the…
Browse files Browse the repository at this point in the history
…re. (#486)
  • Loading branch information
KevinJump committed Mar 29, 2023
1 parent fd20aa9 commit 3a94b51
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions uSync.Core/Serialization/Serializers/ContentSerializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;

using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -206,14 +207,15 @@ protected virtual XElement SerializeProperties(TObject item, SyncSerializerOptio
|| options.GetSetting(uSyncConstants.DefaultsKey, false);

var excludedProperties = GetExcludedProperties(options);
var availableCultures = item.AvailableCultures.ToList();

var node = new XElement("Properties");

foreach (var property in item.Properties
.Where(x => !excludedProperties.InvariantContains(x.Alias))
.OrderBy(x => x.Alias))
{
var propertyNode = new XElement(property.Alias);
var elements = new List<XElement>();

// this can cause us false change readings
// but we need to preserve the values if they are blank
Expand Down Expand Up @@ -246,22 +248,35 @@ protected virtual XElement SerializeProperties(TObject item, SyncSerializerOptio
if (validNode)
{
valueNode.Add(new XCData(GetExportValue(GetPropertyValue(value), property.PropertyType, value.Culture, value.Segment)));
propertyNode.Add(valueNode);
elements.Add(valueNode);
}
}

if (property.Values == null || property.Values.Count == 0 && includeDefaults)
{
// add a blank one, for change clarity
// we do it like this because then it doesn't get collapsed in the XML serialization
var emptyValue = new XElement("Value");
emptyValue.Add(new XCData(string.Empty));

propertyNode.Add(emptyValue);
if (property.PropertyType.VariesByCulture())
{
foreach(var culture in availableCultures)
{
elements.Add(new XElement("Value",
new XAttribute("Culture", culture),
new XCData(string.Empty)));
}
}
else
{
// add a blank one, for change clarity
// we do it like this because then it doesn't get collapsed in the XML serialization
elements.Add(new XElement("Value",
new XCData(string.Empty)));
}
}

if (propertyNode.HasElements)
if (elements.Count > 0)
{
// we sort them at the end because we might end up adding a blank culture value last.
var propertyNode = new XElement(property.Alias);
propertyNode.Add(elements.OrderBy(x => x.Attribute("Culture").ValueOrDefault("")));
node.Add(propertyNode);
}
}
Expand Down

0 comments on commit 3a94b51

Please sign in to comment.