mbraceproject / FsPickler

A fast multi-format message serializer for .NET
http://mbraceproject.github.io/FsPickler/
MIT License
324 stars 52 forks source link

Diagnostics: When Pickler generation fails, it shows the wrong type in the Exception #73

Closed 0x53A closed 8 years ago

0x53A commented 8 years ago

I am currently trying to serialize a Roslyn compilation using FsPickler. I have manually marked CSharpCompilation as Serializable using FsPickler.DeclareSerializable. Serialization still fails with the Exception that it can't serialize 'CSharpCompilation' image

Debugging shows the real non-serializable type, but this information was lost in the topmost exception: image

Please pass the original exception through as an inner exception so it is possible to see for which type it failed without needing to attach a debugger.

eiriktsarpalis commented 8 years ago

Hi,

What is the precise exception that you are getting? I tried the following example:

[<AutoSerializable(false)>]
type Foo = { Foo : string }

type Bar = { Foo : Foo }

type Baz = { Bar : Bar }

FsPickler.GeneratePickler<Baz>()

which gives

Nessos.FsPickler.NonSerializableTypeException: Type 'FSI_0008+Baz' contains non-serializable field of type 'FSI_0008+Bar'.

This seems to contain sufficient debug information, since it points to the correct non-serializable field.

0x53A commented 8 years ago

Hi, sorry for not providing a repro earlier.

This code

open Nessos.FsPickler

[<AutoSerializable(false)>]
type Foo = { Foo : string }

[<AutoSerializable(false)>]
type Bar = { Foo : Foo }

type Baz = { Bar : Bar }

[<EntryPoint>]
let main argv = 
    FsPickler.DeclareSerializable<Bar>()
    FsPickler.GeneratePickler<Baz>() |> ignore
    printfn "%A" argv
    0 // return an integer exit code

fails with image

If I add FsPickler.DeclareSerializable<Foo>(), then it works.

Actual Result: The exception says Bar is not serializable Expected Result: An inner exception which contains a mention of Foo.

0x53A commented 8 years ago

Wow that was fast. Thank you!