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

Malli -> Swagger transformation fails with custom registries #558

Closed
vharmain opened this issue Jul 21, 2022 · 4 comments · Fixed by metosin/malli#863, #589 or #642
Closed

Malli -> Swagger transformation fails with custom registries #558

vharmain opened this issue Jul 21, 2022 · 4 comments · Fixed by metosin/malli#863, #589 or #642
Assignees
Labels

Comments

@vharmain
Copy link
Member

(def schema-malli [:vector [:map {:closed   true
                                 :registry {::test-attr string?}}
                                 [:testing ::test-attr]]])

Could not resolve reference: Could not resolve pointer: /definitions/:user/test-attr does not exist in document

It seems like malli.json-schema will place registry entries under definitions which works fine when you feed it with the "full schema" but in case of reitit the transformation is done separately for each schema and the definitions end up in wrong places in swagger.

I thought about dereferencing the refs before transformation as a "quick fix" but hesitated.

Originally reported in this Slack thread.

@vharmain vharmain added the bug label Jul 21, 2022
@Invertisment
Copy link

If you dereference then you'll have all different types that won't be connected. It's a bad thing in languages such as C#. IMO It depends on language that your front-end is written in.

@opqdonut
Copy link
Member

this got accidentally closed by merging metosin/malli#863, but it also needs #589

@opqdonut opqdonut reopened this Mar 17, 2023
@handerpeder
Copy link
Contributor

handerpeder commented Aug 13, 2023

This also happens with openapi. json-schema/transform has all the info on the malli side, but the coercer throws it away.
I see #589 only fixes the swagger output. A fix for openapi should probably be included there since the testcases should be similar (identical?).
My current workaround:

diff --git a/modules/reitit-malli/src/reitit/coercion/malli.cljc b/modules/reitit-malli/src/reitit/coercion/malli.cljc
index 6f4dff5b..5adbcbac 100644
--- a/modules/reitit-malli/src/reitit/coercion/malli.cljc
+++ b/modules/reitit-malli/src/reitit/coercion/malli.cljc
@@ -136,10 +136,13 @@
 (defn -get-apidocs-openapi
   [coercion {:keys [parameters responses content-types] :or {content-types ["application/json"]}} options]
   (let [{:keys [body request multipart]} parameters
+        definitions (volatile! {})
         parameters (dissoc parameters :request :body :multipart)
         ->schema-object (fn [schema opts]
-                          (let [current-opts (merge options opts)]
-                            (json-schema/transform schema current-opts)))]
+                          (let [current-opts (merge options opts)
+                                ret (json-schema/transform schema current-opts)]
+                            (vswap! definitions merge (:definitions ret))
+                            ret))]
     (merge
      (when (seq parameters)
        {:parameters
@@ -216,7 +219,10 @@
                        [status (merge (select-keys response [:description])
                                       (when content
                                         {:content content}))])))
-              responses)}))))
+              responses)})
+
+     (when (seq @definitions)
+       {:definitions @definitions}))))

 (defn create
   ([]

@opqdonut
Copy link
Member

opqdonut commented Aug 30, 2023

for the record, I intend to fix the openapi side once we get some currently open PRs in (e.g. the swagger fix from #642)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
4 participants