Skip to content

Commit

Permalink
Extended unit test to handle new case
Browse files Browse the repository at this point in the history
  • Loading branch information
MathoMathiasCamara committed Sep 15, 2023
1 parent 15ddb32 commit 9bee1a3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/Moryx.Resources.Samples/DocumentManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Moryx.AbstractionLayer.Resources;
using Moryx.Serialization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace Moryx.Resources.Demo
{
[ResourceRegistration]
[EntrySerialize]
public class DocumentManager : PublicResource
{

[EntrySerialize]
public string FolderPath { get; set; }
}
}
18 changes: 11 additions & 7 deletions src/Moryx/Serialization/EntryConvert/EntryConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,23 @@ public static Entry EncodeObject(object instance, ICustomSerialization customSer
var filtered = customSerialization.GetProperties(instance.GetType());
foreach (var property in filtered)
{

var propertyType = property.GetValue(instance)?.GetType();
//check if this property is a self reference (Descriptor)
// then skip it to avoid endless loop
var flag = propertyType == instanceType;
if (flag) continue;

var convertedProperty = EncodeProperty(property, customSerialization);

object value;
try
{
value = property.GetValue(instance);

//check if this property is a self reference (Descriptor)
Type propertyType = value?.GetType();
var flag = propertyType == instanceType;
if (flag)
{
convertedProperty.Value = converted.Value;
converted.SubEntries.Add(convertedProperty);
// continue to avoid endless loop
continue;
}
}
catch (Exception ex)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Tests/Moryx.Tests/Serialization/EntrySerializeDummies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public class EntrySerialize_NeverClassNoMember
public string NullMethod2() => "1234";
}

[EntrySerialize]
public class EntrySerialize_AlwaysClassAlwaysMember
{
[EntrySerialize]
public string AlwaysProperty { get; set; } = "123456";

public object Property => this;
internal IExplicitInterface ExplicitInterface { get; }
}

// ReSharper disable once InconsistentNaming
[EntrySerialize(EntrySerializeMode.Never)]
public class EntrySerialize_NeverClassAlwaysMember
Expand Down
15 changes: 15 additions & 0 deletions src/Tests/Moryx.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,21 @@ public void SurviveGetterException()
Assert.NotNull(encoded.SubEntries[0].Value.Current);
}


[Test]
public void ClassWithSerializationAttribute()
{
// Arrange
var dummy = new EntrySerialize_AlwaysClassAlwaysMember();

// Act
var encoded = EntryConvert.EncodeObject(dummy);

// Assert
Assert.NotNull(encoded);
Assert.NotNull(encoded.SubEntries[0].Value.Current);
}

[TestCase(CollectionType.Array, 3, 2)]
[TestCase(CollectionType.Array, 0, 4)]
[TestCase(CollectionType.List, 5, 3)]
Expand Down

0 comments on commit 9bee1a3

Please sign in to comment.