refactor: Coercion.-get-model-apidocs, use it for malli openapi

This commit is contained in:
Joel Kaasinen 2023-08-28 08:32:58 +03:00
parent 803ed0933a
commit ee298ec362
2 changed files with 14 additions and 5 deletions

View file

@ -13,6 +13,8 @@
(-get-name [this] "Keyword name for the coercion")
(-get-options [this] "Coercion options")
(-get-apidocs [this specification data] "Returns api documentation")
;; TODO doc options:
(-get-model-apidocs [this specification model options] "Convert model into a format that can be used in api docs")
(-compile-model [this model name] "Compiles a model")
(-open-model [this model] "Returns a new model which allows extra keys in maps")
(-encode-error [this error] "Converts error in to a serializable format")

View file

@ -138,7 +138,7 @@
;; also, this has internally massive amount of duplicate code, could be simplified
;; ... tests too
(defn -get-apidocs-openapi
[->schema-object {:keys [request parameters responses content-types] :or {content-types ["application/json"]}} options]
[coercion {:keys [request parameters responses content-types] :or {content-types ["application/json"]}} options]
(let [{:keys [body multipart]} parameters
parameters (dissoc parameters :request :body :multipart)
->content (fn [data schema]
@ -146,7 +146,7 @@
{:schema schema}
(select-keys data [:description :examples])
(:openapi data)))
->schema-object #(->schema-object %1 (merge options %2))]
->schema-object #(coercion/-get-model-apidocs coercion :openapi %1 (merge options %2))]
(merge
(when (seq parameters)
{:parameters
@ -241,6 +241,13 @@
(reify coercion/Coercion
(-get-name [_] :malli)
(-get-options [_] opts)
(-get-model-apidocs [this specification model options]
(case specification
:openapi (json-schema/transform model options)
(throw
(ex-info
(str "Can't produce Malli apidocs for " specification)
{:type specification, :coercion :malli}))))
(-get-apidocs [this specification {:keys [parameters responses] :as data}]
(case specification
:swagger (merge
@ -261,11 +268,11 @@
(if (:schema $)
(update $ :schema swagger/transform {:type :schema})
$))]))}))
:openapi (-get-apidocs-openapi json-schema/transform data options)
:openapi (-get-apidocs-openapi this data options)
(throw
(ex-info
(str "Can't produce Schema apidocs for " specification)
{:type specification, :coercion :schema}))))
(str "Can't produce Malli apidocs for " specification)
{:type specification, :coercion :malli}))))
(-compile-model [_ model _]
(if (= 1 (count model))
(compile (first model) options)