Restored backward compat of Release Profile POST api

pull/4742/head
Taloth Saldono 3 years ago
parent 6378e7afef
commit 272f8e6136

@ -9,6 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\NzbDrone.Core\Sonarr.Core.csproj" />
<ProjectReference Include="..\NzbDrone.Test.Common\Sonarr.Test.Common.csproj" />
<ProjectReference Include="..\Sonarr.Api.V3\Sonarr.Api.V3.csproj" />
<ProjectReference Include="..\Sonarr.Http\Sonarr.Http.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Serializer;
using Sonarr.Api.V3.Profiles.Release;
using Sonarr.Http.REST;
namespace NzbDrone.Api.Test.v3.ReleaseProfiles
{
[TestFixture]
public class ReleaseProfilesFixture
{
[Test]
public void should_deserialize_releaseprofile_v3_ignored_null()
{
var resource = Json.Deserialize<ReleaseProfileResource>("{ \"ignored\": null, \"required\": null }");
var model = resource.ToModel();
model.Ignored.Should().BeEquivalentTo();
model.Required.Should().BeEquivalentTo();
}
[Test]
public void should_deserialize_releaseprofile_v3_ignored_string()
{
var resource = Json.Deserialize<ReleaseProfileResource>("{ \"ignored\": \"testa,testb\", \"required\": \"testc,testd\" }");
var model = resource.ToModel();
model.Ignored.Should().BeEquivalentTo("testa", "testb");
model.Required.Should().BeEquivalentTo("testc", "testd");
}
[Test]
public void should_deserialize_releaseprofile_v3_ignored_string_array()
{
var resource = Json.Deserialize<ReleaseProfileResource>("{ \"ignored\": [ \"testa\", \"testb\" ], \"required\": [ \"testc\", \"testd\" ] }");
var model = resource.ToModel();
model.Ignored.Should().BeEquivalentTo("testa", "testb");
model.Required.Should().BeEquivalentTo("testc", "testd");
}
[Test]
public void should_throw_with_bad_releaseprofile_v3_ignored_type()
{
var resource = Json.Deserialize<ReleaseProfileResource>("{ \"ignored\": {} }");
Assert.Throws<BadRequestException>(() => resource.ToModel());
}
}
}

@ -27,7 +27,7 @@ namespace Sonarr.Api.V3.Profiles.Release
SharedValidator.RuleFor(d => d).Custom((restriction, context) =>
{
if (restriction.Ignored.Empty() && restriction.Required.Empty() && restriction.Preferred.Empty())
if (restriction.MapIgnored().Empty() && restriction.MapRequired().Empty() && restriction.Preferred.Empty())
{
context.AddFailure("'Must contain', 'Must not contain' or 'Preferred' is required");
}

@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NzbDrone.Core.Profiles.Releases;
using Sonarr.Http.REST;
@ -9,8 +12,9 @@ namespace Sonarr.Api.V3.Profiles.Release
{
public string Name { get; set; }
public bool Enabled { get; set; }
public List<string> Required { get; set; }
public List<string> Ignored { get; set; }
// Is List<string>, string or JArray, we accept 'string' with POST for backward compatibility
public object Required { get; set; }
public object Ignored { get; set; }
public List<KeyValuePair<string, int>> Preferred { get; set; }
public bool IncludePreferredWhenRenaming { get; set; }
public int IndexerId { get; set; }
@ -18,8 +22,6 @@ namespace Sonarr.Api.V3.Profiles.Release
public ReleaseProfileResource()
{
Required = new List<string>();
Ignored = new List<string>();
Preferred = new List<KeyValuePair<string, int>>();
Tags = new HashSet<int>();
}
@ -36,8 +38,8 @@ namespace Sonarr.Api.V3.Profiles.Release
Id = model.Id,
Name = model.Name,
Enabled = model.Enabled,
Required = model.Required,
Ignored = model.Ignored,
Required = model.Required ?? new List<string>(),
Ignored = model.Ignored ?? new List<string>(),
Preferred = model.Preferred,
IncludePreferredWhenRenaming = model.IncludePreferredWhenRenaming,
IndexerId = model.IndexerId,
@ -54,8 +56,8 @@ namespace Sonarr.Api.V3.Profiles.Release
Id = resource.Id,
Name = resource.Name,
Enabled = resource.Enabled,
Required = resource.Required,
Ignored = resource.Ignored,
Required = resource.MapRequired(),
Ignored = resource.MapIgnored(),
Preferred = resource.Preferred,
IncludePreferredWhenRenaming = resource.IncludePreferredWhenRenaming,
IndexerId = resource.IndexerId,
@ -67,5 +69,33 @@ namespace Sonarr.Api.V3.Profiles.Release
{
return models.Select(ToResource).ToList();
}
public static List<string> MapRequired(this ReleaseProfileResource profile) => ParseArray(profile.Required, "required");
public static List<string> MapIgnored(this ReleaseProfileResource profile) => ParseArray(profile.Ignored, "ignored");
private static List<string> ParseArray(object resource, string title)
{
if (resource == null)
{
return new List<string>();
}
if (resource is List<string> list)
{
return list;
}
if (resource is JArray jarray)
{
return jarray.ToObject<List<string>>();
}
if (resource is string str)
{
return str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
throw new BadRequestException($"Invalid field {title}, should be string or string array");
}
}
}

Loading…
Cancel
Save