Skip to content

Commit

Permalink
fix #969 Inherited C# auto-property backing field in remoting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jand42 committed Jun 26, 2018
1 parent 1a2a991 commit 7f84c58
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/compiler/WebSharper.Core/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,20 +1085,34 @@ let fieldFlags =
||| System.Reflection.BindingFlags.Public
||| System.Reflection.BindingFlags.NonPublic

let fieldFlagsDeclOnly =
fieldFlags
||| System.Reflection.BindingFlags.DeclaredOnly

exception NoEncodingException of System.Type with
override this.Message =
"No JSON encoding for " + string this.Data0

type FS = System.Runtime.Serialization.FormatterServices

let getObjectFields (t: System.Type) =
t.GetFields fieldFlags
|> Seq.filter (fun f ->
let nS =
f.Attributes &&&
System.Reflection.FieldAttributes.NotSerialized
f.DeclaringType.IsSerializable && int nS = 0)
|> Seq.toArray
// FlattenHierarchy flag is not enough to collect
// backing fields of auto-properties on base classes
let getDecl (t: System.Type) =
if t.IsSerializable then
t.GetFields fieldFlagsDeclOnly
|> Seq.filter (fun f ->
let nS =
f.Attributes &&&
System.Reflection.FieldAttributes.NotSerialized
int nS = 0
)
else Seq.empty
let rec getAll (t: System.Type) =
match t.BaseType with
| null -> Seq.empty // this is a System.Object
| b -> Seq.append (getAll b) (getDecl t)
getAll t |> Array.ofSeq

let unmakeFlatDictionary<'T> (dE: obj -> Encoded) (x: obj) =
EncodedObject [
Expand Down
25 changes: 25 additions & 0 deletions tests/WebSharper.CSharp.Tests/Remoting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class TestClass
public int Y { get; set; }
}

[JavaScript, Serializable]
public class TestClassSub : TestClass
{
}

public static class Server
{
[Remote]
Expand Down Expand Up @@ -102,6 +107,14 @@ public static Task<TestClass> IncrementXY(TestClass o)
return Task.FromResult(o);
}

[Remote]
public static Task<TestClassSub> IncrementXYSub(TestClassSub o)
{
o.X++;
o.Y++;
return Task.FromResult(o);
}

[Remote]
public static Task<TestStruct> IncrementXYStruct(TestStruct o)
{
Expand Down Expand Up @@ -230,6 +243,18 @@ public async Task CustomClass()
Equal(o.Y, 2);
}

[Test]
public async Task CustomSubClass()
{
if (!ShouldRun) { Expect(0); return; }
var o = new TestClassSub();
o.X = 1;
o.Y = 1;
o = await Server.IncrementXYSub(o);
Equal(o.X, 2);
Equal(o.Y, 2);
}

[Test]
public async Task CustomStruct()
{
Expand Down

0 comments on commit 7f84c58

Please sign in to comment.