Skip to content

Commit

Permalink
Add Swagger option to remove default titles
Browse files Browse the repository at this point in the history
All specs created with st/create-spec have a title, either implicitly
(the fully qualified spec name) or explicitly. This results in invalid
Swagger documents being produced when swagger/swagger-spec is called.
  • Loading branch information
HughPowell committed Apr 18, 2023
1 parent 8ac2798 commit 4bbb46c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/spec_tools/swagger/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@
(let [[_ data] (impl/extract-form spec)
swagger-meta (impl/unlift-keys data "swagger")]
(or (:swagger data)
(merge (json-schema/accept-spec dispatch spec children options) swagger-meta))))
(let [json-schema (json-schema/accept-spec dispatch spec children options)]
(cond-> json-schema
(false? (:default-titles? options)) (dissoc :title)
:always (merge swagger-meta))))))

(defmethod accept-spec ::default [dispatch spec children options]
(json-schema/accept-spec dispatch spec children options))
Expand Down Expand Up @@ -140,10 +143,10 @@
Available options:
| Key | Description
|----------|-----------------------------------------------------------
| `:refs?` | Whether refs should be created for objects. Default: false
| Key | Description
|--------------------|-----------------------------------------------------------
| `:refs?` | Whether refs should be created for objects. Default: false
| `:default-titles?` | Whether to automatically include titles. Default: true
"
([spec]
(transform spec nil))
Expand Down Expand Up @@ -257,9 +260,10 @@
Available options:
| Key | Description
|----------|-----------------------------------------------------------
| `:refs?` | Whether refs should be created for objects. Default: false
| Key | Description
|--------------------|-----------------------------------------------------------
| `:refs?` | Whether refs should be created for objects. Default: false
| `:default-titles?` | Whether to automatically include titles. Default: true
"
([x]
(swagger-spec x nil))
Expand Down
14 changes: 13 additions & 1 deletion test/cljc/spec_tools/swagger/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,18 @@
(deftest test-expectations-with-refs
(doseq [[spec swagger-spec] ref-expectations]
(is (= swagger-spec (swagger/transform spec {:refs? true :type :schema})))
(is (= swagger-spec (swagger/transform spec {:refs? true :in :body}))) ) )
(is (= swagger-spec (swagger/transform spec {:refs? true :in :body})))))

(s/def ::default-titled-string (st/create-spec {:spec ::string}))
(s/def ::explicitly-titled-string (st/create-spec {:spec ::string
:swagger/title "String Title"}))
(deftest test-expectations-with-default-titles
(testing "automatically generated title is included by default"
(is ::default-titled-string (keyword (:title (swagger/transform ::default-titled-string)))))
(testing "explicitly exclude default title"
(is (not (contains? (swagger/transform ::default-titled-string {:default-titles? false}) :title))))
(testing "Swagger title is included irrespective"
(is (= "String Title" (:title (swagger/transform ::explicitly-titled-string {:default-titles? false}))))))

(deftest parameter-test
(testing "nilable body is not required"
Expand Down Expand Up @@ -518,6 +529,7 @@
(is (nil? (-> data swagger/swagger-spec v/validate)))
(is (nil? (-> data (swagger/swagger-spec {:refs? true}) v/validate))))))


(deftest backport-swagger-meta-unnamespaced
(is (= (swagger/transform
(st/spec {:spec string?
Expand Down

0 comments on commit 4bbb46c

Please sign in to comment.