Skip to content

Commit

Permalink
fix: swagger: collecting definitions from all parameters
Browse files Browse the repository at this point in the history
fixes #1002

kudos to @mkreis for the excellent bug report!
  • Loading branch information
opqdonut committed May 16, 2024
1 parent 582ade9 commit 11646cb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/malli/swagger.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@
(let [expanded (expand k v acc options)
parameters (:parameters expanded)
responses (:responses expanded)
definitions (if parameters
(-> parameters first :schema :definitions)
(->> responses vals (map :schema)
(map :definitions) (apply merge)))]
definitions (apply merge
(concat
(->> responses vals (map (comp :definitions :schema)))
(->> parameters (map (comp :definitions :schema)))))]
(-> acc (dissoc k) (merge expanded)
(update :definitions merge definitions)
dissoc-non-root-definitions))
Expand Down
36 changes: 36 additions & 0 deletions test/malli/swagger_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,39 @@
:type "string"
:minLength 10}]}
(swagger/swagger-spec {::swagger/parameters {:query #'Query}})))))

(deftest request-parameter-definition-regression-test
;; For issue #1002
(testing "collects :definitions for all parameters"
(let [registry (merge (m/base-schemas) (m/type-schemas) (m/comparator-schemas)
{::req-body [:map-of :keyword :any]})
expected {:definitions {"malli.swagger-test/req-body" {:additionalProperties {}, :type "object"}}
:parameters [{:description ""
:in "body"
:name "body"
:required true
:schema {:$ref "#/definitions/malli.swagger-test~1req-body"}}
{:description ""
:in "header"
:name :h
:required true
:type "string"}
{:description ""
:in "query"
:name :q
:required true
:type "string"}]}
fix #(update % :parameters (partial sort-by :in))]
(is (= expected
(fix
(swagger/swagger-spec {::swagger/parameters
{:body (m/schema ::req-body {:registry registry})
:header [:map [:h :string]]
:query [:map [:q :string]]}}))))
;; bug #1002 was sensitive to the order of the ::swagger/parameters map
(is (= expected
(fix
(swagger/swagger-spec {::swagger/parameters
{:header [:map [:h :string]]
:query [:map [:q :string]]
:body (m/schema ::req-body {:registry registry})}})))))))

0 comments on commit 11646cb

Please sign in to comment.