Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse query string comma-separated values as list #243

Closed
cmeeren opened this issue Mar 9, 2018 · 6 comments
Closed

Parse query string comma-separated values as list #243

cmeeren opened this issue Mar 9, 2018 · 6 comments
Labels
question General question

Comments

@cmeeren
Copy link

cmeeren commented Mar 9, 2018

I'd like to deserialize the query string ?flags=favorite,hidden to the type Query as defined below:

type Flag = Favorite | Hidden
type Query = {Flags: Flag list}

Is this possible using Giraffe? I've only managed to use lists in query strings by repeating the parameter: ?flags=favorite&flags=hidden.

(Please disregard the actual DU related stuff; I've fixed that by using FSharpLu.Json.)

@dustinmoris
Copy link
Member

dustinmoris commented Mar 12, 2018

Hi @cmeeren, the way list items are passed through a query string is by specifying the parameter multiple times like in your second example. See #121 (comment)

In this case ASP.NET Core has already decomposed the query string into a list and Giraffe is simply deserializing the string values into the correct type.

Your client application has to send list items in the correct format I am afraid.

EDIT: You could do your own hack by having a type like this:

type Flag =
    | Favorite
    | Hidden

type Query =
    {
        Flags : string
    }
    member this.FlagList =
        this.Flags.Split ','
        |> Array.map (fun s ->
            match s with
            | "hidden"   -> Hidden
            | "favorite" -> Favorite
            | _ -> failwithf "Could not deserialize string '%s'." s)
        |> Array.toList

@dustinmoris dustinmoris added the question General question label Mar 12, 2018
@cmeeren
Copy link
Author

cmeeren commented Mar 12, 2018

Thanks for the tip. I too was under the impression that specifying the parameter multiple times was the correct way, but our front-end developer suggested using comma-separated values, so I just wanted to check if this was something Giraffe supported. It seems we should indeed stick to the standard way of doing things.

@cmeeren
Copy link
Author

cmeeren commented Mar 12, 2018

On the other hand, the JSON API spec specifies that comma-separated values is mandatory behavior (link to first mention). Do you think it would be possible and desirable for Giraffe to add support for this?

@narve
Copy link

narve commented Dec 6, 2019

On the other hand, the JSON API spec specifies that comma-separated values is mandatory behavior (link to first mention). Do you think it would be possible and desirable for Giraffe to add support for this?

Good point - although I don't like JSON APIs specification, it should be easy to follow it.
Has this been resolved, or an easier workaround found?

@cmeeren
Copy link
Author

cmeeren commented Dec 6, 2019

I am splitting manually.

As a side note, I have also created FSharp.JsonApi, which makes JSON:API much more pleasant to use with F# (and optionally Giraffe). It has (among many other things) built-in helpers for parsing comma-separated query strings. (Don't use the package just for that feature, though.)

@narve
Copy link

narve commented Dec 6, 2019

All right, thanks for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question General question
Projects
None yet
Development

No branches or pull requests

3 participants