ring-swagger

ring-swagger & compojure-api
2018-04-16T14:51:43.000001Z

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

2018-04-16T14:53:36.000573Z

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

2018-04-16T14:53:51.000798Z

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

2018-04-16T14:55:04.000517Z

Trying to make this work with clojure.spec .

2018-04-16T14:56:11.000297Z

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