You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ran into a an edge case that was causing some problems. One of our POST endpoints can accept no body when it is called. We set up the individual elements to be completely optional with the idea that if the user does not supply a body the service will execute with default values.
When we implemented swagger we found that the code fails when no body is submitted on the POST, but works when a body (even an empty body is submitted). This is the error we get.
{
"errors": "(not (map? nil))"
}
We were able to work around the problem by putting a piece of small piece of ring middleware in place that would inspect the incoming request for a POST and then check to see if the body of the request was empty. If the body was empty, we replaced the body with an empty json {} string. The code for our middleware function is below
(defn- is-post? [request]
"Determines if the request coming in is a post"
(if (= (:request-method request) :post) true false))
(defn- is-empty-post? [request http-body]
"Determines whether a request is a post and has a blank body"
(and (is-post? request) (blank? http-body)))
(defn- handle-empty-body-for-swagger [handler](fn [request]
%28let [http-body %28if %28nil? %28:body request%29%29 "" %28slurp %28:body request))) ;Suck in the original Request Body. If there is not one then set the body to ""
empty-body (merge request {:body (string-input-stream "{}")}) ;Create an empty body
orig-body (merge request {:body (string-input-stream http-body)})] ;Maintain the original body in case we need it
(if (is-empty-post? request http-body) (handler empty-body) (handler orig-body)))))
The text was updated successfully, but these errors were encountered:
Hi guys,
Ran into a an edge case that was causing some problems. One of our POST endpoints can accept no body when it is called. We set up the individual elements to be completely optional with the idea that if the user does not supply a body the service will execute with default values.
When we implemented swagger we found that the code fails when no body is submitted on the POST, but works when a body (even an empty body is submitted). This is the error we get.
{
"errors": "(not (map? nil))"
}
We were able to work around the problem by putting a piece of small piece of ring middleware in place that would inspect the incoming request for a POST and then check to see if the body of the request was empty. If the body was empty, we replaced the body with an empty json {} string. The code for our middleware function is below
(defn- is-post? [request]
"Determines if the request coming in is a post"
(if (= (:request-method request) :post) true false))
(defn- is-empty-post? [request http-body]
"Determines whether a request is a post and has a blank body"
(and (is-post? request) (blank? http-body)))
(defn- handle-empty-body-for-swagger [handler](fn [request]
%28let [http-body %28if %28nil? %28:body request%29%29 "" %28slurp %28:body request))) ;Suck in the original Request Body. If there is not one then set the body to ""
empty-body (merge request {:body (string-input-stream "{}")}) ;Create an empty body
orig-body (merge request {:body (string-input-stream http-body)})] ;Maintain the original body in case we need it
(if (is-empty-post? request http-body) (handler empty-body) (handler orig-body)))))
The text was updated successfully, but these errors were encountered: