metosin / compojure-api

Sweet web apis with Compojure & Swagger
http://metosin.github.io/compojure-api/doc/
Eclipse Public License 1.0
1.12k stars 149 forks source link

Optional query-params #425

Closed tzzh closed 5 years ago

tzzh commented 5 years ago

Library Version(s)

1.1.11

Problem

Is it possible to use optional query-params with the inline syntax ?

                :query-params [????]
                :middleware [wrap-route-metrics]

I can get it to work with resource

(resource
    :get {
       :parameters {:query-params {(s/optional-key :current-status) CurrentStatus}}}

but that then breaks the swagger interface (I believe because of some custom middleware), and that's a frequent use case so I am wondering if there is a way to do it with the inline syntax ? This https://github.com/metosin/compojure-api/issues/154 suggests the hack of setting default values and then having a custom coercer but I was wondering if there was a better way to do it.

tzzh commented 5 years ago

I ended up using empty string default value and then filtering empty strings in the handler

ikitommi commented 5 years ago

You can do:

:query {(s/optional-key :current-status) CurrentStatus}

or

:query-params [{current-status :- CurrentStatus :default-current-status-value}]