From 8ac27982cf1ee51371e46f2ca8305833f427ea91 Mon Sep 17 00:00:00 2001 From: Hugh Powell Date: Thu, 13 Apr 2023 18:43:11 +1000 Subject: [PATCH] Handle additionalProperties properly --- src/spec_tools/swagger/core.cljc | 16 ++++++++++------ test/cljc/spec_tools/swagger/core_test.cljc | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/spec_tools/swagger/core.cljc b/src/spec_tools/swagger/core.cljc index e03fd90..f27ec2b 100644 --- a/src/spec_tools/swagger/core.cljc +++ b/src/spec_tools/swagger/core.cljc @@ -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] @@ -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 _] @@ -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] diff --git a/test/cljc/spec_tools/swagger/core_test.cljc b/test/cljc/spec_tools/swagger/core_test.cljc index b0adb11..5f4bdcd 100644 --- a/test/cljc/spec_tools/swagger/core_test.cljc +++ b/test/cljc/spec_tools/swagger/core_test.cljc @@ -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