Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAPI :requestBody for :form request schema #703

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions modules/reitit-openapi/src/reitit/openapi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@
(defn- openapi-path [path opts]
(-> path (trie/normalize opts) (str/replace #"\{\*" "{")))

(def ^:private form-content-type "application/x-www-form-urlencoded")

(defn -get-apidocs-openapi
[coercion {:keys [request muuntaja parameters responses openapi/request-content-types openapi/response-content-types]} definitions]
(let [{:keys [body multipart]} parameters
parameters (dissoc parameters :request :body :multipart)
(let [{:keys [body form multipart]} parameters
parameters (dissoc parameters :request :body :form :multipart)
->content (fn [data schema]
(merge
{:schema schema}
Expand Down Expand Up @@ -122,6 +124,13 @@
[content-type {:schema schema}])))
request-content-types)}})

(when form
;; :form is similar to any other body, but the content type must be application/x-www-form-urlencoded
{:requestBody {:content {form-content-type {:schema (->schema-object form
{:in :requestBody
:type :schema
:content-type form-content-type})}}}})

(when request
;; :request allows different :requestBody per content-type
{:requestBody
Expand Down
14 changes: 14 additions & 0 deletions test/cljc/reitit/openapi_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,13 @@
:handler (fn [req]
{:status 200
:body (-> req :parameters :request)})}}]
["/form-params"
{:post {:description "ring :form-params coercion with :parameters :form syntax"
:coercion coercion
:parameters {:form (->schema :b)}
:handler (fn [req]
{:status 200
:body (-> req :parameters :request)})}}]
["/openapi.json"
{:get {:handler (openapi/create-openapi-handler)
:openapi {:info {:title "" :version "0.0.1"}}
Expand Down Expand Up @@ -801,6 +808,13 @@
(-> spec
(get-in [:paths "/legacy" :post :responses 200 :content])
keys)))))
(testing ":parameters :form syntax"
(testing "body parameter"
(is (= ["application/x-www-form-urlencoded"]
(-> spec
(get-in [:paths "/form-params" :post :requestBody :content])
keys))
"form parameter schema is put under :requestBody with correct content type")))
(testing "spec is valid"
(is (nil? (validate spec))))))))

Expand Down
Loading