Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix malli swagger defs w/ custom registries #589

Merged
merged 8 commits into from
Sep 1, 2023
65 changes: 18 additions & 47 deletions modules/reitit-malli/src/reitit/coercion/malli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,6 @@
(assoc error :transformed transformed))))
value))))))))

;;
;; swagger
;;

(defmulti extract-parameter (fn [in _ _] in))

(defmethod extract-parameter :body [_ schema options]
(let [swagger-schema (swagger/transform schema (merge options {:in :body, :type :parameter}))]
[{:in "body"
:name (:title swagger-schema "body")
:description (:description swagger-schema "")
:required (not= :maybe (m/type schema))
:schema swagger-schema}]))

(defmethod extract-parameter :default [in schema options]
(let [{:keys [properties required]} (swagger/transform schema (merge options {:in in, :type :parameter}))]
(mapv
(fn [[k {:keys [type] :as schema}]]
(merge
{:in (name in)
:name k
:description (:description schema "")
:type type
:required (contains? (set required) k)}
schema))
properties)))

;;
;; public api
;;
Expand Down Expand Up @@ -234,26 +207,24 @@
(-get-options [_] opts)
(-get-apidocs [this specification {:keys [parameters responses] :as data}]
(case specification
:swagger (merge
(if parameters
{:parameters
(->> (for [[in schema] parameters
parameter (extract-parameter in (compile schema options) options)]
parameter)
(into []))})
(if responses
{:responses
(into
(empty responses)
(for [[status response] responses]
[status (as-> response $
(set/rename-keys $ {:body :schema})
(update $ :description (fnil identity ""))
(if (:schema $)
(-> $
(update :schema compile options)
(update :schema swagger/transform {:type :schema}))
$))]))}))
:swagger (swagger/swagger-spec
(merge
(if parameters
{::swagger/parameters
(into
(empty parameters)
(for [[k v] parameters]
[k (coercion/-compile-model this v nil)]))})
(if responses
{::swagger/responses
(into
(empty responses)
(for [[k response] responses]
[k (as-> response $
(set/rename-keys $ {:body :schema})
(if (:schema $)
(update $ :schema #(coercion/-compile-model this % nil))
$))]))})))
:openapi (-get-apidocs-openapi this data options)
(throw
(ex-info
Expand Down
12 changes: 10 additions & 2 deletions modules/reitit-swagger/src/reitit/swagger.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@
(if-let [endpoint (some->> c (keep transform-endpoint) (seq) (into {}))]
[(swagger-path p (r/options router)) endpoint]))
map-in-order #(->> % (apply concat) (apply array-map))
paths (->> router (r/compiled-routes) (filter accept-route) (map transform-path) map-in-order)]
paths (->> router (r/compiled-routes) (filter accept-route) (map transform-path) map-in-order)
definitions (reduce-kv
(fn [ds _ v]
(let [ks (keys v)]
(merge ds (apply merge
(for [k ks]
(when-let [method-map (get v k)]
(:definitions method-map)))))))
{} paths)]
{:status 200
:body (meta-merge swagger {:paths paths})}))
:body (meta-merge swagger {:paths paths :definitions definitions})}))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update the existing tests and add a new one that uses a registry!

([req res raise]
(try
(res (create-swagger req))
Expand Down
55 changes: 52 additions & 3 deletions test/cljc/reitit/swagger_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[schema.core :as s]
[spec-tools.data-spec :as ds]))
[spec-tools.data-spec :as ds]
[malli.core :as mc]))

(defn- normalize
"Normalize format of swagger spec by converting it to json and back.
Expand All @@ -22,6 +23,23 @@
j/write-value-as-string
(j/read-value j/keyword-keys-object-mapper)))

(def malli-registry
(merge
(mc/base-schemas)
(mc/predicate-schemas)
(mc/type-schemas)
{::req-key [:or :keyword :string]
::req-val [:or map? :string]
::resp-map map?
::resp-string [:string {:min 1}]}))


(def PutReqBody
(mc/schema [:map-of ::req-key ::req-val] {:registry malli-registry}))

(def PutRespBody
(mc/schema [:or ::resp-map ::resp-string] {:registry malli-registry}))

(def app
(ring/ring-handler
(ring/router
Expand Down Expand Up @@ -84,7 +102,13 @@
500 {:description "fail"}}
:handler (fn [{{{:keys [z]} :path
xs :body} :parameters}]
{:status 200, :body {:total (+ (reduce + xs) z)}})}}]]
{:status 200, :body {:total (+ (reduce + xs) z)}})}
:put {:summary "plus put with definitions"
:parameters {:body PutReqBody}
:responses {200 {:body PutRespBody}
500 {:description "fail"}}
:handler (fn [{{body :body} :parameters}]
{:status 200, :body (str "got " body)})}}]]

["/schema" {:coercion schema/coercion}
["/plus/*z"
Expand Down Expand Up @@ -138,6 +162,15 @@
expected {:x-id #{::math}
:swagger "2.0"
:info {:title "my-api"}
:definitions {::req-key {:type "string"
:x-anyOf [{:type "string"}
{:type "string"}]}
::req-val {:type "object"
:x-anyOf [{:type "object"}
{:type "string"}]}
::resp-map {:type "object"},
::resp-string {:type "string"
:minLength 1}}
:paths {"/api/spec/plus/{z}" {:patch {:parameters []
:summary "patch"
:operationId "Patch"
Expand Down Expand Up @@ -247,7 +280,23 @@
400 {:schema {:type "string"}
:description "kosh"}
500 {:description "fail"}}
:summary "plus with body"}}
:summary "plus with body"}
:put {:parameters [{:in "body"
:name "body"
:description ""
:required true
:schema
{:type "object"
:additionalProperties
{:$ref "#/definitions/reitit.swagger-test~1req-val"}}}]
:responses {200
{:schema
{:$ref "#/definitions/reitit.swagger-test~1resp-map"
:x-anyOf [{:$ref "#/definitions/reitit.swagger-test~1resp-map"}
{:$ref "#/definitions/reitit.swagger-test~1resp-string"}]}
:description ""}
500 {:description "fail"}}
:summary "plus put with definitions"}}
"/api/schema/plus/{z}" {:get {:parameters [{:description ""
:format "int32"
:in "query"
Expand Down