fix: Do not write null values to service JSON data

spectre-console-remove-di-hacks
Robert Dailey 8 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;
public static class DtoUtil
{
public static void SetIfNotNull<T>(ref T propertyValue, T? newValue)
{
if (newValue is not null)
{
propertyValue = newValue;
}
}
}
[UsedImplicitly]
public record QualityProfileDto
{

@ -9,33 +9,15 @@ public sealed class JsonNoSerializeAttribute : Attribute
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)
{
var attrs = type.Properties
.Select(x => (Property: x, Attributes: x.PropertyType.GetCustomAttributes(false).Select(y => y.GetType())))
.Where(x => x.Attributes.Any())
.ToDictionary(x => x.Property.Name, x => x.Attributes);
var propertiesToRemove = type.Properties
.Where(x => x.AttributeProvider?.IsDefined(typeof(JsonNoSerializeAttribute), false) ?? false)
.ToList();
var props = type.Properties;
foreach (var prop in props)
foreach (var prop in propertiesToRemove)
{
prop.ShouldSerialize = (_, _) => !HasAttribute<JsonNoSerializeAttribute>(prop, attrs);
prop.ShouldSerialize = (_, _) => false;
}
}
}

Loading…
Cancel
Save