Skip to content

Commit

Permalink
Merge pull request #703 from cloudpermit/handle_form_coercion_properl…
Browse files Browse the repository at this point in the history
…y_in_openapi

Add OpenAPI :requestBody for :form request schema
  • Loading branch information
opqdonut authored Nov 1, 2024
2 parents f6b8054 + 702e7b8 commit 86e0441
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit 86e0441

Please sign in to comment.