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