Skip to content

Option fields fail to serialize #394

Description

@reinux

Since Fable 5, any return value that includes an Option seems to fail. I think there's disagreement as to how Option should be serialized between the server and client implementations.

In this repro, it produces a different error depending on whether the value is Some or None; in my other project, it fails silently.

When it's None:

Response:

{"thing":null}

Exception {message: 'Cannot convert null to ["Union",null]'}

When it's Some:

Response:

{"thing":"Something"}

Exception {message: "Case Something was not valid for type 'FSharpOptio…', expected one of the cases [ 'None' , 'Some' ]"}

Client.fs:

open Fable.Core
open Fable.Remoting.Client
open Common

Browser.Dom.console.log("Testing")

let client =
  Remoting.createApi()
  |> Remoting.buildProxy<TestApi>

let p =
  promise {
    Browser.Dom.console.log("Starting")
    let! thing = client.getThing() |> Async.StartAsPromise
    Browser.Dom.console.log("Thing", thing)
    return thing
  }

Packages:

    <PackageReference Include="Fable.Promise" Version="3.2.0" />
    <PackageReference Include="Fable.Remoting.Client" Version="8.0.0" />

Server.fs:

open Microsoft.AspNetCore.Builder
open Fable.Remoting.AspNetCore
open Fable.Remoting.Server
open Common

let api =
  Remoting.createApi()
  |> Remoting.fromValue { getThing = fun () -> async { return { thing = None } } }

[<EntryPoint>]
let main (args: string array) =
  let builder = WebApplication.CreateBuilder args
  let app = builder.Build()

  app.UseRemoting api

  app.Run()
  0

Packages:

    <PackageReference Include="Fable.Remoting.AspNetCore" Version="3.1.0" />

Common.fs:

namespace Common

type TestThing =
    { thing: string option }

type TestApi =
    { getThing: unit -> Async<TestThing> }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions