@@ -23,7 +23,53 @@ public void WhenNoSerializableTypesIsEmpty ()
23
23
}
24
24
25
25
[ Fact ]
26
- public void DoesntSerializeInstancedInteropInterfaces ( )
26
+ public void SerializesTypesFromInteropMethods ( )
27
+ {
28
+ AddAssembly ( With (
29
+ """
30
+ public record RecordA;
31
+ public record RecordB;
32
+ public record RecordC;
33
+
34
+ public class Class
35
+ {
36
+ [JSInvokable] public static Task<RecordA[]> A (RecordC c) => default;
37
+ [JSFunction] public static RecordB[] B (RecordC[] c) => default;
38
+ }
39
+ """ ) ) ;
40
+ Execute ( ) ;
41
+ Contains ( "[JsonSerializable(typeof(global::RecordA)" ) ;
42
+ Contains ( "[JsonSerializable(typeof(global::RecordB)" ) ;
43
+ Contains ( "[JsonSerializable(typeof(global::RecordC)" ) ;
44
+ Contains ( "[JsonSerializable(typeof(global::RecordA[])" ) ;
45
+ Contains ( "[JsonSerializable(typeof(global::RecordB[])" ) ;
46
+ Contains ( "[JsonSerializable(typeof(global::RecordC[])" ) ;
47
+ }
48
+
49
+ [ Fact ]
50
+ public void SerializesTypesFromInteropInterfaces ( )
51
+ {
52
+ AddAssembly ( With (
53
+ """
54
+ public record RecordA;
55
+ public record RecordB;
56
+ public record RecordC;
57
+ public interface IExported { void Inv (RecordA a); }
58
+ public interface IImported { void Fun (RecordB b); void NotifyEvt(RecordC c); }
59
+
60
+ public class Class
61
+ {
62
+ [JSFunction] public static Task<IImported> GetImported (IExported arg) => default;
63
+ }
64
+ """ ) ) ;
65
+ Execute ( ) ;
66
+ Contains ( "[JsonSerializable(typeof(global::RecordA)" ) ;
67
+ Contains ( "[JsonSerializable(typeof(global::RecordB)" ) ;
68
+ Contains ( "[JsonSerializable(typeof(global::RecordC)" ) ;
69
+ }
70
+
71
+ [ Fact ]
72
+ public void DoesntSerializeInstancedInteropInterfacesThemselves ( )
27
73
{
28
74
AddAssembly ( With (
29
75
"""
@@ -46,23 +92,32 @@ public class Class
46
92
DoesNotContain ( "JsonSerializable" ) ;
47
93
}
48
94
49
- [ Fact ] // .NET's generator indexes types by short names (w/o namespace) and fails on duplicates.
50
- public void AddsOnlyTopLevelTypesAndCrawledDuplicates ( )
95
+ [ Fact ]
96
+ public void SerializesAllTheCrawledSerializableTypes ( )
51
97
{
98
+ // .NET's generator indexes types by short names (w/o namespace) and fails on duplicates, so we have to add everything ourselves.
99
+ // https://github.com/dotnet/runtime/issues/58938#issuecomment-1306731801
52
100
AddAssembly (
53
- With ( "y" , "public struct Struct { public double A { get; set; } }" ) ,
54
- With ( "n" , "public struct Struct { public y.Struct S { get; set; } }" ) ,
101
+ With ( "y" , "public enum Enum { A, B }" ) ,
102
+ With ( "y" , "public record Struct (double A, ReadonlyStruct[]? B);" ) ,
103
+ With ( "y" , "public record ReadonlyStruct (Enum e);" ) ,
104
+ With ( "n" , "public struct Struct { public y.Struct S { get; set; } public ReadonlyStruct[]? A { get; set; } }" ) ,
55
105
With ( "n" , "public readonly struct ReadonlyStruct { public double A { get; init; } }" ) ,
56
106
With ( "n" , "public readonly record struct ReadonlyRecordStruct(double A);" ) ,
57
107
With ( "n" , "public record class RecordClass(double A);" ) ,
58
108
With ( "n" , "public enum Enum { A, B }" ) ,
59
109
With ( "n" , "public class Foo { public Struct S { get; } public ReadonlyStruct Rs { get; } }" ) ,
60
110
WithClass ( "n" , "public class Bar : Foo { public ReadonlyRecordStruct Rrs { get; } public RecordClass Rc { get; } }" ) ,
61
- With ( "n" , "public class Baz { public List<Class.Bar?> Bars { get; } public Enum E { get; } }" ) ,
62
- WithClass ( "n" , "[JSInvokable] public static Task<Baz?> GetBaz () => default;" ) ) ;
111
+ With ( "n" , "public class Baz { public List<Class.Bar?> Bars { get; } }" ) ,
112
+ WithClass ( "n" , "[JSInvokable] public static Task<Baz?> GetBaz (Enum e ) => default;" ) ) ;
63
113
Execute ( ) ;
64
- Assert . Equal ( 2 , Matches ( " JsonSerializable" ) . Count ) ;
65
- Contains ( "[JsonSerializable(typeof(global::n.Baz )" ) ;
114
+ Contains ( "[ JsonSerializable(typeof(global::y.Enum)" ) ;
115
+ Contains ( "[JsonSerializable(typeof(global::n.Enum )" ) ;
66
116
Contains ( "[JsonSerializable(typeof(global::y.Struct)" ) ;
117
+ Contains ( "[JsonSerializable(typeof(global::n.Struct)" ) ;
118
+ Contains ( "[JsonSerializable(typeof(global::n.ReadonlyStruct)" ) ;
119
+ Contains ( "[JsonSerializable(typeof(global::y.ReadonlyStruct)" ) ;
120
+ Contains ( "[JsonSerializable(typeof(global::n.ReadonlyStruct[])" ) ;
121
+ Contains ( "[JsonSerializable(typeof(global::y.ReadonlyStruct[])" ) ;
67
122
}
68
123
}
0 commit comments