mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
Merge pull request #703 from cloudpermit/handle_form_coercion_properly_in_openapi
Add OpenAPI :requestBody for :form request schema
This commit is contained in:
commit
86e04414ba
2 changed files with 25 additions and 2 deletions
|
|
@ -76,10 +76,12 @@
|
||||||
(defn- openapi-path [path opts]
|
(defn- openapi-path [path opts]
|
||||||
(-> path (trie/normalize opts) (str/replace #"\{\*" "{")))
|
(-> path (trie/normalize opts) (str/replace #"\{\*" "{")))
|
||||||
|
|
||||||
|
(def ^:private form-content-type "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
(defn -get-apidocs-openapi
|
(defn -get-apidocs-openapi
|
||||||
[coercion {:keys [request muuntaja parameters responses openapi/request-content-types openapi/response-content-types]} definitions]
|
[coercion {:keys [request muuntaja parameters responses openapi/request-content-types openapi/response-content-types]} definitions]
|
||||||
(let [{:keys [body multipart]} parameters
|
(let [{:keys [body form multipart]} parameters
|
||||||
parameters (dissoc parameters :request :body :multipart)
|
parameters (dissoc parameters :request :body :form :multipart)
|
||||||
->content (fn [data schema]
|
->content (fn [data schema]
|
||||||
(merge
|
(merge
|
||||||
{:schema schema}
|
{:schema schema}
|
||||||
|
|
@ -122,6 +124,13 @@
|
||||||
[content-type {:schema schema}])))
|
[content-type {:schema schema}])))
|
||||||
request-content-types)}})
|
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
|
(when request
|
||||||
;; :request allows different :requestBody per content-type
|
;; :request allows different :requestBody per content-type
|
||||||
{:requestBody
|
{:requestBody
|
||||||
|
|
|
||||||
|
|
@ -739,6 +739,13 @@
|
||||||
:handler (fn [req]
|
:handler (fn [req]
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (-> req :parameters :request)})}}]
|
: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"
|
["/openapi.json"
|
||||||
{:get {:handler (openapi/create-openapi-handler)
|
{:get {:handler (openapi/create-openapi-handler)
|
||||||
:openapi {:info {:title "" :version "0.0.1"}}
|
:openapi {:info {:title "" :version "0.0.1"}}
|
||||||
|
|
@ -801,6 +808,13 @@
|
||||||
(-> spec
|
(-> spec
|
||||||
(get-in [:paths "/legacy" :post :responses 200 :content])
|
(get-in [:paths "/legacy" :post :responses 200 :content])
|
||||||
keys)))))
|
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"
|
(testing "spec is valid"
|
||||||
(is (nil? (validate spec))))))))
|
(is (nil? (validate spec))))))))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue