Skip to content

Commit

Permalink
Handle additionalProperties properly
Browse files Browse the repository at this point in the history
  • Loading branch information
HughPowell committed Apr 13, 2023
1 parent 85df820 commit 8ac2798
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/spec_tools/swagger/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
(defmethod accept-spec ::default [dispatch spec children options]
(json-schema/accept-spec dispatch spec children options))

(defn- update-if [m k f & args]
(if (contains? m k)
(apply update m k f args)
m))

(defmulti create-or-raise-refs (fn [{:keys [type]} _] type))

(defmethod create-or-raise-refs "object" [swagger options]
Expand All @@ -100,11 +105,14 @@
swagger' (create-or-raise-refs (dissoc swagger :title) options)]
{:$ref (str "#/definitions/" title)
::definitions (merge {title (dissoc swagger' ::definitions)} (::definitions swagger'))})
(let [definitions (apply merge (map ::definitions (vals (:properties swagger))))]
(let [definitions (apply merge
(::definitions (:additionalProperties swagger))
(map ::definitions (vals (:properties swagger))))]
(if definitions
(-> swagger
(assoc ::definitions definitions)
(update :properties update-vals #(dissoc % ::definitions)))
(update-if :properties update-vals #(dissoc % ::definitions))
(update-if :additionalProperties dissoc ::definitions))
swagger))))

(defmethod create-or-raise-refs "array" [swagger _]
Expand Down Expand Up @@ -181,10 +189,6 @@
;; expand the spec
;;

(defn- update-if [m k f & args]
(if (contains? m k)
(apply update m k f args)
m))
(defmulti expand (fn [k _ _ _] k))

(defmethod expand ::responses [_ v acc options]
Expand Down
17 changes: 17 additions & 0 deletions test/cljc/spec_tools/swagger/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,23 @@
{::swagger/responses {200 {:schema (st/create-spec
{:spec ::user
:swagger/title "User"})}}}
{:refs? true}))))

(testing "::responses with refs in additionalProperties"
(is (=
{:responses {200 {:schema {:$ref "#/definitions/Every Test"}, :description ""}},
:definitions {"Every Test" {:type "object",
:additionalProperties {:$ref "#/definitions/spec-tools.swagger.core-test.address"}},
"spec-tools.swagger.core-test.address" {:type "object",
:properties {"street" {:type "string"},
"city" {:enum [:tre :hki],
:type "string",
:x-nullable true}},
:required ["street" "city"]}}}
(swagger/swagger-spec
{::swagger/responses {200 {:schema (st/create-spec
{:spec (s/every-kv ::id ::address)
:swagger/title "Every Test"})}}}
{:refs? true})))))

#?(:clj
Expand Down

0 comments on commit 8ac2798

Please sign in to comment.