Skip to content

Swagger throws a error when a POST body is empty #226

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

Closed
carnellj opened this issue Mar 23, 2016 · 1 comment
Closed

Swagger throws a error when a POST body is empty #226

carnellj opened this issue Mar 23, 2016 · 1 comment
Labels

Comments

@carnellj
Copy link

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)))))

@ikitommi
Copy link
Member

no body will continue to be nil producing the normal Schema errors, will be documented properly in #329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants