<PackageReference Include="NJsonSchema" Version="9.12.6" />

IgnoreEmptyCollectionsContractResolver

using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System.Collections; using System.Reflection; namespace NJsonSchema.Infrastructure { internal class IgnoreEmptyCollectionsContractResolver : PropertyRenameAndIgnoreSerializerContractResolver { protected override Newtonsoft.Json.Serialization.JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { Newtonsoft.Json.Serialization.JsonProperty property = base.CreateProperty(member, memberSerialization); if ((property.Required == Required.Default || property.Required == Required.DisallowNull) && property.PropertyType != typeof(string) && ReflectionExtensions.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(property.PropertyType.GetTypeInfo())) property.ShouldSerialize = ((object instance) => ((instance != null) ? (property.ValueProvider.GetValue(instance) as IEnumerable) : null)?.GetEnumerator().MoveNext() ?? true); return property; } } }