Skip to content

Commit fde3a15

Browse files
author
Michael Catanzariti
committed
fix ObjectMapping.PostInitialize not called during default object mapping creation
fix #51
1 parent 58375d9 commit fde3a15

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Dahomey.Cbor.Serialization;
2+
using Dahomey.Cbor.Serialization.Converters.Mappings;
3+
using System;
4+
using Xunit;
5+
6+
namespace Dahomey.Cbor.Tests.Issues
7+
{
8+
public class Issue0051
9+
{
10+
public class ObjectMappingConventionProvider : IObjectMappingConventionProvider
11+
{
12+
private readonly IObjectMappingConvention _objectMappingConvention;
13+
14+
public ObjectMappingConventionProvider()
15+
{
16+
_objectMappingConvention = new ObjectMappingConvention();
17+
}
18+
19+
public IObjectMappingConvention GetConvention(Type type)
20+
{
21+
return _objectMappingConvention;
22+
}
23+
}
24+
25+
public class ObjectMappingConvention : IObjectMappingConvention
26+
{
27+
private readonly DefaultObjectMappingConvention _defaultObjectMappingConvention = new DefaultObjectMappingConvention();
28+
29+
public void Apply<T>(SerializationRegistry registry, ObjectMapping<T> objectMapping) where T : class
30+
{
31+
_defaultObjectMappingConvention.Apply<T>(registry, objectMapping);
32+
objectMapping.SetOrderBy(m => m.MemberName);
33+
}
34+
}
35+
36+
public class MyObject
37+
{
38+
public int C { get; set; }
39+
public int A { get; set; }
40+
public int B { get; set; }
41+
}
42+
43+
[Fact]
44+
public void TestWrite()
45+
{
46+
CborOptions options = new CborOptions();
47+
options.Registry.ObjectMappingConventionRegistry.RegisterProvider(new ObjectMappingConventionProvider());
48+
49+
var obj = new MyObject
50+
{
51+
C = 1,
52+
A = 2,
53+
B = 3
54+
};
55+
56+
// {"A": 2, "B": 3, "C": 1}
57+
const string hexBuffer = "A3614102614203614301";
58+
59+
Helper.TestWrite(obj, hexBuffer, null, options);
60+
}
61+
}
62+
}

src/Dahomey.Cbor/Serialization/Converters/Mappings/ObjectMappingRegistry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private IObjectMapping CreateDefaultObjectMapping(Type type)
6969
if (objectMapping is IMappingInitialization mappingInitialization)
7070
{
7171
mappingInitialization.Initialize();
72+
mappingInitialization.PostInitialize();
7273
}
7374

7475
return objectMapping;

0 commit comments

Comments
 (0)