SaturnFramework / Saturn

Opinionated, web development framework for F# which implements the server-side, functional MVC pattern
https://saturnframework.org
MIT License
708 stars 108 forks source link

`Response.accepted` fails to serialize response #197

Closed et1975 closed 4 years ago

et1975 commented 4 years ago

Getting

System.ArgumentException: Could not determine JSON object type for type Microsoft.FSharp.Collections.FSharpList1[Presentation+Link]. at Newtonsoft.Json.Linq.JValue.GetValueType(Nullable1 current, Object value) at Thoth.Json.Net.Encode.autoEncoder@513-14.Invoke(Object v) at Thoth.Json.Giraffe.ThothSerializer.Giraffe-Serialization-Json-IJsonSerializer-SerializeToBytes[T](T o) at Giraffe.ResponseWriters.HttpContext.WriteJsonAsync[T](HttpContext this, T dataObj) in C:\projects\giraffe\src\Giraffe\ResponseWriters.fs:line 137

json function and ctx.WriteJsonAsync has no problem serializing Presentation.Link list type elsewhere. What am I doing wrong?

here's how I'm calling it:

let somePost : HttpHandler =
    fun _ ctx ->
        task {
            return! 
                { id = id
                  links = mkLinks id }
                |> Response.accepted ctx
        }

and the router:

let router = router {
    post "/" somePost
}
et1975 commented 4 years ago

Added unit tests that roundtrip these types using Thoth's IJsonSerializer impl and they pass. Looking for ideas... Oops, closed accidentally!

MangelMaxime commented 4 years ago

Do you know what Response.accepted is calling for serializing the JSON?

Implement seems to be, but I don't know it's being handled by Saturn or Giraffe.

    let accepted (ctx: HttpContext) res =
      Successful.ACCEPTED res Common.halt ctx
et1975 commented 4 years ago

Looking at the stack trace ACCEPTED calls ctx.WriteJsonAsync, but if I call ctx.WriteJsonAsync directly it serializes successfuly!

ChrSteinert commented 4 years ago

Do you also use the SerializeToBytes function in your tests?

MangelMaxime commented 4 years ago

@ChrSteinert Is the question for me?

ChrSteinert commented 4 years ago

Sorry, @MangelMaxime - the question was for @et1975

et1975 commented 4 years ago

Do you also use the SerializeToBytes function in your tests?

@ChrSteinert no.

Krzysztof-Cieslak commented 4 years ago

Hey, I can't reproduce that on current master, this test is passing just fine - https://github.com/SaturnFramework/Saturn/blob/master/tests/Saturn.UnitTests/SimpleTests.fs

et1975 commented 4 years ago

Let's close this, I'm not working anymore on the project where this came up.