reitit

dorab 2024-08-18T23:13:04.571539Z

If I want to specify that the response has an empty body, do I just leave off the :content key (as mentioned in OpenAPI spec) or is there some other way in Reitit? In the thread, I have a minimal example that is not working as I expect and I'm wondering what I'm doing wrong.

dorab 2024-08-18T23:18:24.464099Z

The following code does not behave as I expect...

(def test-handler
    (ring/ring-handler
     (ring/router [["/openapi.json"
                    {:get {:no-doc true
                           :openapi {:info {:title "testreitit"
                                            :description "Test Open API spec"
                                            :version "0.0.1"}}
                           :handler (openapi/create-openapi-handler)}}]
                   ["/test/{id}"
                    {:get {:description "id < 5 ==> 400 (null body); otherwise 200 and JSON"
                           :handler (fn [{{{:keys [id]} :path} :parameters}]
                                      (if (< id 5)
                                        {:status 400}
                                        {:status 200
                                         :body {:a 1}}))
                           :parameters {:path [:map [:id int?]]}
                           :responses
                           {:default {:description "default is json"
                                      :content {"application/json" {:schema [:map
                                                                             {:closed false}
                                                                             [:a int?]]}}}
                            400 {:description "400 and empty response"}}}}]]
                  {:validate spec/validate
                   :data {:coercion reitit.coercion.malli/coercion
                          :muuntaja m/instance
                          :middleware [openapi/openapi-feature
                                       swagger/swagger-feature
                                       parameters/parameters-middleware
                                       muuntaja/format-negotiate-middleware
                                       muuntaja/format-response-middleware
                                       exception/exception-middleware
                                       muuntaja/format-request-middleware
                                       coercion/coerce-response-middleware
                                       coercion/coerce-request-middleware
                                       multipart/multipart-middleware]}})
     (ring/routes (swagger-ui/create-swagger-ui-handler
                   {:path "/"
                    :config {:validatorUrl nil
                             :operationsSorter "alpha"
                             :urls [{:name "openapi" :url "/openapi.json"}]
                             :urls.primaryName "openapi"}} )
                  (ring/create-default-handler))))
  (def testx (jetty/run-jetty #'test-handler {:port 7777 :join? false}))
  (.stop ^org.eclipse.jetty.server.Server testx)
I expect that if I call it with /test/2 I should get a 400 response with an empty body. But I get a 500 response with the following body
{
  "value": null,
  "type": "reitit.coercion/response-coercion",
  "coercion": "malli",
  "in": [
    "response",
    "body"
  ],
  "humanized": [
    "invalid type"
  ]
}
It appears that Reitit is expecting something else other than an empty body and so complaining. What am I doing wrong?

dorab 2024-08-19T00:37:47.974689Z

If I change the spec for the 400 code to

400 {:description "400 and empty response"
     :content {:default {:schema nil?}}}
It works as I expected. Is that the recommended way?

opqdonut 2024-08-19T05:49:06.318729Z

I don't think I considered empty responses, so the code expects there to be a content schema. Feel free to open an issue about this. Good to here that :schema nil? works for you though.

dorab 2024-08-19T18:21:10.069839Z

Thanks. Added https://github.com/metosin/reitit/issues/691 on github.