-
-
Notifications
You must be signed in to change notification settings - Fork 414
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
What is the idiomatic way to have default params in APIs? #766
Comments
I has almost working prototype, but it depends on yet unreleased |
See haskell-servant/servant-contrib#4 I won't say it's idiomatic, as I just wrote it; but that's something I could imagine using myself. (Required parameters will complicate the story) |
I think there was an issue somewhere (can't find it now) to add combinator(s) that'd use I think that would be more "idiomatic" than @haskell-servant/maintainers what do you think? |
Would it be difficult to add a combinator that could masquerade as a type Api = "test" :> (QueryParam "value" Int :?> 10) :> Get '[JSON] Value where If there is/were a way to customize what the This would be also very handy when generating docs since defaults are an important part of documentation that is not currently captured. |
I'd shy away from specifying the value at the type level. Suppose the query
param took a Double.
…On Wed, Jun 14, 2017 at 10:09 AM, Elliot Cameron ***@***.***> wrote:
Would it be difficult to add a combinator that could masquerade as a
QueryParam but provide a default?
type Api = "test" :> (QueryParam "value" Int :?> 10) :> Get '[JSON] Value
—
You are receiving this because you are on a team that was mentioned.
Reply to this email directly, view it on GitHub
<#766 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA1bPOseDdyrx6lrv02x63fYoqwt_iHbks5sEBPHgaJpZM4N0D5k>
.
--
Cell: 1.630.740.8204
|
You can always add defaults in your handler. The question is: how do you capture that information in the API specification? I think we have two options, specify the default in the type itself, or use type classes to allow the default to be at the value level. But the type-class approach would be very boilerplate heavy. Perhaps you could combine them to get the best of both worlds. |
@3noch I agree with @dmjio. Unfortunately Besides, I don't like defaults parameter values on the handler side. AFAIK default parameters are mostly useful when you have many parameters and would like to omit few. But then I'd rather pack those parameters in a record data type and have a smart constructor (maybe more than one) for it with some defaults. |
@fizruk I use Servant often to write specifications for other APIs. They don't tend to have the same values as Haskellers and they use defaults liberally. Not being able to easily encode them in the specification is a problem, IMO. It's even more annoying when they're not documented, and servant, currently, has no way to generate docs for defaults. |
@3noch if you're a client of such API then I guess you can treat any |
@fizruk Right, yes. I should have been clearer. I actually hope to use servant specs as the engine for generating docs for such APIs. |
@fizruk the Similar reasoning applies to default values. Sometimes you want them shown at type-level, but you can bake them into an instance as well. The drawback with Side-note: IIRC swagger supports default values? |
The issue here (for me) it's less about mandatory vs optional params and more about easy to call functions. In the example I show above the function If I declare a record to wrap the QueryParams and declare a wrapper function to pass the record instead it's way less cumbersome, but it forces me to declare the records and the wrapper for each function defined in the API. I think this is a common enough case to merit a standard idiomatic solution. I just don't know if there is already one (does not look like it) or if we need something for it. |
This is still highly desired for mapping onto real-world APIs out there |
I am currently defining an API with:
And using this to generate a function to call the endpoint
The issue is that the function signature is quite ugly, I'd like to define a record type and default params to be able to generate the function like this:
Which of course does not work.
How do you normally deal with endpoints that accept a lot of query params? I am sure this is something not uncommon.
The text was updated successfully, but these errors were encountered: