The Free Software Media System - Server Backend & API
Support IReadOnlyList for JsonCommaDelimitedArrayConverter
crobibero42c2ab97e729
diff --git a/MediaBrowser.Common/Json/Converters/JsonCommaDelimitedArrayConverterFactory.cs b/MediaBrowser.Common/Json/Converters/JsonCommaDelimitedArrayConverterFactory.csindex b7b1daf76a..24ed3ea19e 100644--- a/MediaBrowser.Common/Json/Converters/JsonCommaDelimitedArrayConverterFactory.cs+++ b/MediaBrowser.Common/Json/Converters/JsonCommaDelimitedArrayConverterFactory.cs@@ -21,8 +21,8 @@ namespace MediaBrowser.Common.Json.Converters/// <inheritdoc />public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options){- var structType = typeToConvert.GetElementType();+ var structType = typeToConvert.GetElementType() ?? typeToConvert.GenericTypeArguments[0];return (JsonConverter)Activator.CreateInstance(typeof(JsonCommaDelimitedArrayConverter<>).MakeGenericType(structType));}}-}\ No newline at end of file+}diff --git a/tests/Jellyfin.Common.Tests/Json/JsonCommaDelimitedArrayTests.cs b/tests/Jellyfin.Common.Tests/Json/JsonCommaDelimitedArrayTests.csindex 16f8a690e6..0d2bdd1af9 100644--- a/tests/Jellyfin.Common.Tests/Json/JsonCommaDelimitedArrayTests.cs+++ b/tests/Jellyfin.Common.Tests/Json/JsonCommaDelimitedArrayTests.cs@@ -11,82 +11,82 @@ namespace Jellyfin.Common.Tests.Json[Fact]public static void Deserialize_String_Valid_Success(){- var desiredValue = new GenericBodyModel<string>+ var desiredValue = new GenericBodyArrayModel<string>{Value = new[] { "a", "b", "c" }};var options = new JsonSerializerOptions();- var value = JsonSerializer.Deserialize<GenericBodyModel<string>>(@"{ ""Value"": ""a,b,c"" }", options);+ var value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": ""a,b,c"" }", options);Assert.Equal(desiredValue.Value, value?.Value);}[Fact]public static void Deserialize_String_Space_Valid_Success(){- var desiredValue = new GenericBodyModel<string>+ var desiredValue = new GenericBodyArrayModel<string>{Value = new[] { "a", "b", "c" }};var options = new JsonSerializerOptions();- var value = JsonSerializer.Deserialize<GenericBodyModel<string>>(@"{ ""Value"": ""a, b, c"" }", options);+ var value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": ""a, b, c"" }", options);Assert.Equal(desiredValue.Value, value?.Value);}[Fact]public static void Deserialize_GenericCommandType_Valid_Success(){- var desiredValue = new GenericBodyModel<GeneralCommandType>+ var desiredValue = new GenericBodyArrayModel<GeneralCommandType>{Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }};var options = new JsonSerializerOptions();options.Converters.Add(new JsonStringEnumConverter());- var value = JsonSerializer.Deserialize<GenericBodyModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", options);+ var value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", options);Assert.Equal(desiredValue.Value, value?.Value);}[Fact]public static void Deserialize_GenericCommandType_Space_Valid_Success(){- var desiredValue = new GenericBodyModel<GeneralCommandType>+ var desiredValue = new GenericBodyArrayModel<GeneralCommandType>{Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }};var options = new JsonSerializerOptions();options.Converters.Add(new JsonStringEnumConverter());- var value = JsonSerializer.Deserialize<GenericBodyModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", options);+ var value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", options);Assert.Equal(desiredValue.Value, value?.Value);}[Fact]public static void Deserialize_String_Array_Valid_Success(){- var desiredValue = new GenericBodyModel<string>+ var desiredValue = new GenericBodyArrayModel<string>{Value = new[] { "a", "b", "c" }};var options = new JsonSerializerOptions();- var value = JsonSerializer.Deserialize<GenericBodyModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", options);+ var value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", options);Assert.Equal(desiredValue.Value, value?.Value);}[Fact]public static void Deserialize_GenericCommandType_Array_Valid_Success(){- var desiredValue = new GenericBodyModel<GeneralCommandType>+ var desiredValue = new GenericBodyArrayModel<GeneralCommandType>{Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }};var options = new JsonSerializerOptions();options.Converters.Add(new JsonStringEnumConverter());- var value = JsonSerializer.Deserialize<GenericBodyModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", options);+ var value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", options);Assert.Equal(desiredValue.Value, value?.Value);}}-}\ No newline at end of file+}diff --git a/tests/Jellyfin.Common.Tests/Json/JsonCommaDelimitedIReadOnlyListTests.cs b/tests/Jellyfin.Common.Tests/Json/JsonCommaDelimitedIReadOnlyListTests.csnew file mode 100644index 0000000000..34ad9bac79--- /dev/null+++ b/tests/Jellyfin.Common.Tests/Json/JsonCommaDelimitedIReadOnlyListTests.cs@@ -0,0 +1,92 @@+using System.Text.Json;+using System.Text.Json.Serialization;+using Jellyfin.Common.Tests.Models;+using MediaBrowser.Model.Session;+using Xunit;++namespace Jellyfin.Common.Tests.Json+{+ public static class JsonCommaDelimitedIReadOnlyListTests+ {+ [Fact]+ public static void Deserialize_String_Valid_Success()+ {+ var desiredValue = new GenericBodyIReadOnlyListModel<string>+ {+ Value = new[] { "a", "b", "c" }+ };++ var options = new JsonSerializerOptions();+ var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": ""a,b,c"" }", options);+ Assert.Equal(desiredValue.Value, value?.Value);+ }++ [Fact]+ public static void Deserialize_String_Space_Valid_Success()+ {+ var desiredValue = new GenericBodyIReadOnlyListModel<string>+ {+ Value = new[] { "a", "b", "c" }+ };++ var options = new JsonSerializerOptions();+ var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": ""a, b, c"" }", options);+ Assert.Equal(desiredValue.Value, value?.Value);+ }++ [Fact]+ public static void Deserialize_GenericCommandType_Valid_Success()+ {+ var desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>+ {+ Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }+ };++ var options = new JsonSerializerOptions();+ options.Converters.Add(new JsonStringEnumConverter());+ var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", options);+ Assert.Equal(desiredValue.Value, value?.Value);+ }++ [Fact]+ public static void Deserialize_GenericCommandType_Space_Valid_Success()+ {+ var desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>+ {+ Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }+ };++ var options = new JsonSerializerOptions();+ options.Converters.Add(new JsonStringEnumConverter());+ var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", options);+ Assert.Equal(desiredValue.Value, value?.Value);+ }++ [Fact]+ public static void Deserialize_String_Array_Valid_Success()+ {+ var desiredValue = new GenericBodyIReadOnlyListModel<string>+ {+ Value = new[] { "a", "b", "c" }+ };++ var options = new JsonSerializerOptions();+ var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", options);+ Assert.Equal(desiredValue.Value, value?.Value);+ }++ [Fact]+ public static void Deserialize_GenericCommandType_Array_Valid_Success()+ {+ var desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>+ {+ Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }+ };++ var options = new JsonSerializerOptions();+ options.Converters.Add(new JsonStringEnumConverter());+ var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", options);+ Assert.Equal(desiredValue.Value, value?.Value);+ }+ }+}diff --git a/tests/Jellyfin.Common.Tests/Models/GenericBodyModel.cs b/tests/Jellyfin.Common.Tests/Models/GenericBodyArrayModel.cssimilarity index 93%rename from tests/Jellyfin.Common.Tests/Models/GenericBodyModel.csrename to tests/Jellyfin.Common.Tests/Models/GenericBodyArrayModel.csindex 9a6c382fea..276e1bfbe2 100644--- a/tests/Jellyfin.Common.Tests/Models/GenericBodyModel.cs+++ b/tests/Jellyfin.Common.Tests/Models/GenericBodyArrayModel.cs@@ -8,7 +8,7 @@ namespace Jellyfin.Common.Tests.Models/// The generic body model./// </summary>/// <typeparam name="T">The value type.</typeparam>- public class GenericBodyModel<T>+ public class GenericBodyArrayModel<T>{/// <summary>/// Gets or sets the value.@@ -17,4 +17,4 @@ namespace Jellyfin.Common.Tests.Models[JsonConverter(typeof(JsonCommaDelimitedArrayConverterFactory))]public T[] Value { get; set; } = default!;}-}\ No newline at end of file+}diff --git a/tests/Jellyfin.Common.Tests/Models/GenericBodyIReadOnlyListModel.cs b/tests/Jellyfin.Common.Tests/Models/GenericBodyIReadOnlyListModel.csnew file mode 100644index 0000000000..627454b25f--- /dev/null+++ b/tests/Jellyfin.Common.Tests/Models/GenericBodyIReadOnlyListModel.cs@@ -0,0 +1,19 @@+using System.Collections.Generic;+using System.Text.Json.Serialization;+using MediaBrowser.Common.Json.Converters;++namespace Jellyfin.Common.Tests.Models+{+ /// <summary>+ /// The generic body <c>IReadOnlyList</c> model.+ /// </summary>+ /// <typeparam name="T">The value type.</typeparam>+ public class GenericBodyIReadOnlyListModel<T>+ {+ /// <summary>+ /// Gets or sets the value.+ /// </summary>+ [JsonConverter(typeof(JsonCommaDelimitedArrayConverterFactory))]+ public IReadOnlyList<T> Value { get; set; } = default!;+ }+}