Skip to content

Commit b267e66

Browse files
committed
Fixed HowToStep in ItemList use case; updated Recipe test. Now JSON array deserialization always returns list of interfaces not class instances.
1 parent aef9a5a commit b267e66

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

Source/Schema.NET/ValuesJsonConverter.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ private static Type ToClass(Type type)
410410
private static IList ReadJsonArray(JToken token, Type type, int? count = null)
411411
{
412412
var classType = ToClass(type);
413-
var listType = typeof(List<>).MakeGenericType(classType);
413+
var listType = typeof(List<>).MakeGenericType(type); // always read into list of interfaces
414414
var list = Activator.CreateInstance(listType);
415415
var i = 0;
416416

@@ -425,25 +425,33 @@ private static IList ReadJsonArray(JToken token, Type type, int? count = null)
425425
var typeName = GetTypeNameFromToken(childToken);
426426
if (string.IsNullOrEmpty(typeName))
427427
{
428-
var item = childToken.ToObject(classType);
429-
listType
430-
.GetRuntimeMethod(nameof(List<object>.Add), new[] { classType })
431-
.Invoke(list, new object[] { item });
428+
var child = childToken.ToObject(classType);
429+
var method = listType.GetRuntimeMethod(nameof(List<object>.Add), new[] { classType });
430+
431+
if (method != null)
432+
{
433+
method.Invoke(list, new object[] { child });
434+
435+
i++;
436+
}
432437
}
433438
else
434439
{
435440
var builtType = Type.GetType($"{NamespacePrefix}{typeName}");
436-
if (builtType != null && GetTypeHierarchy(builtType).Any(x => x == classType))
441+
if (builtType != null && type.GetTypeInfo().IsAssignableFrom(builtType.GetTypeInfo()))
437442
{
438443
var child = (Thing)childToken.ToObject(builtType);
439-
listType
440-
.GetRuntimeMethod(nameof(List<object>.Add), new[] { classType })
441-
.Invoke(list, new object[] { child });
444+
var method = listType.GetRuntimeMethod(nameof(List<object>.Add), new[] { classType });
445+
446+
if (method != null)
447+
{
448+
method.Invoke(list, new object[] { child });
449+
450+
i++;
451+
}
442452
}
443453
}
444454

445-
i++;
446-
447455
if (i == count)
448456
{
449457
break;
@@ -460,13 +468,18 @@ private static IEnumerable<Type> GetTypeHierarchy(Type type)
460468
yield break;
461469
}
462470

463-
yield return type;
471+
var tt = type.GetTypeInfo().GetNestedTypes(BindingFlags.Public);
472+
473+
foreach (var t in tt)
474+
{
475+
yield return t;
476+
}
477+
478+
var ii = type.GetInterfaces();
464479

465-
var baseType = type.GetTypeInfo().BaseType;
466-
while (baseType != null)
480+
foreach (var i in ii)
467481
{
468-
yield return baseType;
469-
baseType = baseType.GetTypeInfo().BaseType;
482+
yield return i;
470483
}
471484
}
472485

Tests/Schema.NET.Test/core/RecipeTest.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public class RecipeTest
3838
"Thinly-sliced apples:6 cups",
3939
"White sugar:3/4 cup"
4040
},
41-
RecipeInstructions = "1. Cut and peel apples..."
41+
RecipeInstructions = new List<ICreativeWork>()
42+
{
43+
new HowToStep { Text = "1. Cut and peel apples..." },
44+
new HowToStep { Text = "2. Put in pie shell..." }
45+
}
4246
};
4347

4448
private readonly string json =
@@ -71,7 +75,14 @@ public class RecipeTest
7175
"\"Thinly-sliced apples:6 cups\"," +
7276
"\"White sugar:3/4 cup\"" +
7377
"]," +
74-
"\"recipeInstructions\":\"1. Cut and peel apples...\"," +
78+
"\"recipeInstructions\":[{" +
79+
"\"@type\":\"HowToStep\"," +
80+
"\"text\":\"1. Cut and peel apples...\"" +
81+
"},{" +
82+
"\"@type\":\"HowToStep\"," +
83+
"\"text\":\"2. Put in pie shell...\"" +
84+
"}" +
85+
"]," +
7586
"\"recipeYield\":\"1 9 inch pie (8 servings)\"" +
7687
"}";
7788

0 commit comments

Comments
 (0)