SaturnFramework / Saturn

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

Deeply nested controllers throw an invalid cast exception #115

Closed TWith2Sugars closed 6 years ago

TWith2Sugars commented 6 years ago

If I have a controller setup like so (I extended the Controller.Sample to track this down)

let commentController2 (userId:int)(x:int) = controller {
    index (fun ctx -> (sprintf "Comment Index handler for user %i" userId ) |> Controller.text ctx)
    add (fun ctx -> (sprintf "Comment Add handler for user %i" userId ) |> Controller.text ctx)
    show (fun ctx id -> (sprintf "Show comment %s handler for user %i" id userId ) |> Controller.text ctx)
    edit (fun ctx id -> (sprintf "Edit comment %s handler for user %i" id userId )  |> Controller.text ctx)
}
let commentController userId = controller {
    subController "/sub" (commentController2 userId)
    index (fun ctx -> (sprintf "Comment Index handler for user %i" userId ) |> Controller.text ctx)
    add (fun ctx -> (sprintf "Comment Add handler for user %i" userId ) |> Controller.text ctx)
    show (fun ctx id -> (sprintf "Show comment %O handler for user %i" id userId ) |> Controller.text ctx)
    edit (fun ctx id -> (sprintf "Edit comment %O handler for user %i" id userId )  |> Controller.text ctx)
}
type Response = {
    a: string
    b: string
}
type DifferentResponse = {
    c: int
    d: DateTime
}
let typedController = controller {
    subController "/tc" commentController

    index (fun _ -> task {
        return {a = "hello"; b = "world"}
    })

    add (fun _ -> task {
        return {c = 123; d = DateTime.Now}
    })
}
let topRouter = router {
    forward "/typed" typedController
}

When I attempt to route to a sub controller I get an invalid cast exception here: https://github.com/SaturnFramework/Saturn/blob/master/src/Saturn/Controller.fs#L303

Trying to cast a tuple to a string.

I should also mention this worked on 0.6

NinoFloris commented 6 years ago

@TWith2Sugars thanks for your PR, I should have commented earlier as I have just incorporated it in a bigger fix. I'll rebase mine on top of yours!