fix: Do not write null values to service JSON data

pull/231/head
Robert Dailey 9 months ago
parent 4f5946bc67
commit 0106bc5111

@ -0,0 +1,12 @@
namespace Recyclarr.Cli.Pipelines.QualityProfile.Api;
public static class DtoUtil
{
public static void SetIfNotNull<T>(ref T propertyValue, T? newValue)
{
if (newValue is not null)
{
propertyValue = newValue;
}
}
}

@ -3,17 +3,6 @@ using JetBrains.Annotations;
namespace Recyclarr.Cli.Pipelines.QualityProfile.Api; namespace Recyclarr.Cli.Pipelines.QualityProfile.Api;
public static class DtoUtil
{
public static void SetIfNotNull<T>(ref T propertyValue, T? newValue)
{
if (newValue is not null)
{
propertyValue = newValue;
}
}
}
[UsedImplicitly] [UsedImplicitly]
public record QualityProfileDto public record QualityProfileDto
{ {

@ -9,33 +9,15 @@ public sealed class JsonNoSerializeAttribute : Attribute
public static class JsonSerializationModifiers public static class JsonSerializationModifiers
{ {
private static bool HasAttribute<T>(JsonPropertyInfo? prop, IReadOnlyDictionary<string, IEnumerable<Type>> allAttrs)
where T : Attribute
{
if (prop is null)
{
return false;
}
if (!allAttrs.TryGetValue(prop.Name, out var attrs))
{
return false;
}
return attrs.Any(x => x.IsAssignableTo(typeof(T)));
}
public static void IgnoreNoSerializeAttribute(JsonTypeInfo type) public static void IgnoreNoSerializeAttribute(JsonTypeInfo type)
{ {
var attrs = type.Properties var propertiesToRemove = type.Properties
.Select(x => (Property: x, Attributes: x.PropertyType.GetCustomAttributes(false).Select(y => y.GetType()))) .Where(x => x.AttributeProvider?.IsDefined(typeof(JsonNoSerializeAttribute), false) ?? false)
.Where(x => x.Attributes.Any()) .ToList();
.ToDictionary(x => x.Property.Name, x => x.Attributes);
var props = type.Properties; foreach (var prop in propertiesToRemove)
foreach (var prop in props)
{ {
prop.ShouldSerialize = (_, _) => !HasAttribute<JsonNoSerializeAttribute>(prop, attrs); prop.ShouldSerialize = (_, _) => false;
} }
} }
} }

Loading…
Cancel
Save