Fork me on GitHub
#ring-swagger
<
2018-04-16
>
velrok14:04:43

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}))

velrok14:04:36

The query params are coming through as {"usage[gas]": 123} and the swagger UI models the usage arg as a generic string.

velrok14:04:51

Are there any examples on the net of apis with nested params?

velrok14:04:04

Trying to make this work with clojure.spec .

velrok14:04:11

Correction: the expected params are {:usage {:gas 123}}