Hi.
I’m trying to spec out an existing API, which uses structured query params like so /something?usage[gas]=123
which I expect to some through as {:usage 123}
eventually.
So far I have failed to convince compojure-api and generate a nice swagger interface for this.
This is an extract of what I have so far:
(ns schemas
(:require
[clojure.spec.alpha :as s]
[spec-tools.core :as st]
[spec-tools.spec :as spec]))
(s/def ::gas spec/int?)
(s/def ::electricity spec/int?)
(s/def ::usage
(st/spec (s/keys :opt-un [::gas ::electricity])))
;;; =---------------------------
(ns api
(:require [compojure.api.sweet :as sweet]
[energy-market.web.schemas :as schemas]
[ring.util.http-response :as http-response]
ring.util.codec))
(sweet/GET "/plan" []
:query-params [{usage :- ::schemas/usage {}}]
(http-response/ok {:usage usage}))
The query params are coming through as {"usage[gas]": 123}
and the swagger UI models the usage
arg as a generic string.
Are there any examples on the net of apis with nested params?
Trying to make this work with clojure.spec
.
Correction: the expected params are {:usage {:gas 123}}