Skip to content

Commit

Permalink
Support for primitive body params & responses (also Arrays)
Browse files Browse the repository at this point in the history
* Replaces #56
* Fixes #55
* Fixes metosin/compojure-api#177
  • Loading branch information
ikitommi committed Aug 12, 2016
1 parent 6f95cf4 commit f012c72
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/ring/swagger/swagger2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
(defmulti ^:private extract-parameter (fn [in _ _] in))

(defmethod extract-parameter :body [_ model options]
(if-let [schema (rsc/peek-schema model)]
(let [schema-json (rsjs/->swagger model options)]
(vector {:in "body"
:name (common/title schema)
:description (or (:description (rsjs/json-schema-meta schema)) "")
:required (not (rsjs/maybe? model))
:schema schema-json}))))
(if model
(let [schema (rsc/peek-schema model)
schema-json (rsjs/->swagger model options)]
(vector
{:in "body"
:name (or (common/title schema) "")
:description (or (:description (rsjs/json-schema-meta schema)) "")
:required (not (rsjs/maybe? model))
:schema schema-json}))))

(defmethod extract-parameter :default [in model options]
(if model
Expand Down
54 changes: 54 additions & 0 deletions test/ring/swagger/swagger2_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,57 @@
:responses {:default {:description ""}}}}}})

(validate swagger) => nil))

(fact "primitive vector body parameters & responses"
(let [swagger {:paths {"/api" {:post {:parameters {:body [s/Str]}
:responses {200 {:schema [s/Str]}}}}}}]
(swagger2/swagger-json swagger)
=> (contains
{:definitions {}
:paths {"/api" {:post {:parameters [{:in "body"
:name ""
:description ""
:required true
:schema {:items {:type "string"}
:type "array"}}]
:responses {200 {:description ""
:schema {:items {:type "string"}
:type "array"}}}}}}})

(validate swagger) => nil))

(fact "primitive set body parameters & responses"
(let [swagger {:paths {"/api" {:post {:parameters {:body #{s/Str}}
:responses {200 {:schema #{s/Str}}}}}}}]
(swagger2/swagger-json swagger)
=> (contains
{:definitions {}
:paths {"/api" {:post {:parameters [{:in "body"
:name ""
:description ""
:required true
:schema {:items {:type "string"}
:uniqueItems true
:type "array"}}]
:responses {200 {:description ""
:schema {:items {:type "string"}
:uniqueItems true
:type "array"}}}}}}})

(validate swagger) => nil))

(fact "primitive body parameters & responses"
(let [swagger {:paths {"/api" {:post {:parameters {:body s/Str}
:responses {200 {:schema s/Str}}}}}}]
(swagger2/swagger-json swagger)
=> (contains
{:definitions {}
:paths {"/api" {:post {:parameters [{:in "body"
:name ""
:description ""
:required true
:schema {:type "string"}}]
:responses {200 {:description ""
:schema {:type "string"}}}}}}})

(validate swagger) => nil))

0 comments on commit f012c72

Please sign in to comment.