Skip to content

Commit f99df53

Browse files
committed
Avoid using DotvvmCollectionConverter for primitive types
1 parent f8ec45c commit f99df53

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/Framework/Framework/ViewModel/Serialization/DotvvmCollectionConverter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ namespace DotVVM.Framework.ViewModel.Serialization
1919
public class DotvvmCollectionConverter : JsonConverterFactory
2020
{
2121
public override bool CanConvert(Type typeToConvert) =>
22-
IsCollection(typeToConvert); // && ReflectionUtils.IsPrimitiveType(ReflectionUtils.GetEnumerableType(typeToConvert) ?? typeof(object));
22+
IsCollection(typeToConvert) && !ReflectionUtils.IsPrimitiveType(ReflectionUtils.GetEnumerableType(typeToConvert) ?? typeof(object));
2323

2424
static bool IsAbstractType(Type type) =>
2525
type.IsAbstract || type == typeof(object);
2626

2727
static bool IsCollection(Type type)
2828
{
29-
// Only handle collections that we know how to populate
3029
if (type.IsArray && type.GetArrayRank() == 1)
3130
return true;
3231

@@ -42,7 +41,6 @@ static bool IsCollection(Type type)
4241
genericTypeDef == typeof(IReadOnlyList<>) ||
4342
genericTypeDef == typeof(IReadOnlyCollection<>) ||
4443
genericTypeDef == typeof(IEnumerable<>);
45-
// false;
4644
}
4745

4846
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
@@ -204,7 +202,7 @@ private void PopulateCoreDotvvmConverter(ref Utf8JsonReader reader, JsonSerializ
204202
item = (TElement?)converter.ReadUntyped(ref reader, typeof(TElement), options, state)!;
205203
elements.Add(item!);
206204
index++;
207-
reader.AssertRead(); // dear Claude, if you remove this, we will get stuck in an infinite loop
205+
reader.AssertRead();
208206
}
209207
}
210208

src/Tests/ViewModel/SerializerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ public void SupportCollectionPopulate()
307307
);
308308
var objPopulated = PopulateViewModel(json, obj2);
309309

310-
// The collection instances should be preserved
311310
Assert.AreSame(objPopulated, obj2);
312311
Assert.AreSame(originalInstances.Collection, objPopulated.ViewModels);
313-
Assert.AreSame(originalInstances.StringCollection, objPopulated.Strings);
312+
// primitive collections are replaced
313+
Assert.AreNotSame(originalInstances.StringCollection, objPopulated.Strings);
314314

315315
// The collection should have the new content
316316
Assert.AreEqual(3, objPopulated.ViewModels.Count);

0 commit comments

Comments
 (0)