I created an API using the following code:
["/environments/:env-name/nodes"
{:swagger {:tags ["Nodes"]}
:parameters {:path {:env-name ::vt-vali/name}}}
[""
{:get {:summary "Retrieve the nodes from this environment"
:parameters {:query {:date ::vt-vali/timestamp}}
:responses {200 {:body map?}}
:handler (fn [{{{:keys [env-name]} :path
{:keys [date]} :query} :parameters}]
(let [result (vt-data/ret-nodes env-name date)]
(if (s/valid? map? result)
{:status 200
:body result}
{:status 500
:body result})))}}]]
This works perfectly. However, I want to make the query parameter optional.
Can anyone help me with this?
I found an answer by searching through the examples in
metosin/reitit
.It is possible to use
clojure.spec.alpha
. Add[clojure.spec.alpha :as s]
to the required dependencies of the namespace and you can use::parameters {:query (s/keys :opt-un [::date])}
See this file for the example in
metosin/reitit
http-swagger example