compojure-api icon indicating copy to clipboard operation
compojure-api copied to clipboard

inconsistency in resource vs. macro based route definition behaviour

Open danieldroit opened this issue 8 years ago • 0 comments

When making:

curl -X POST \
  'http://localhost:7777/foo?rule_name=foo.logic&force_overwrite=true&version=1.1' \
  -H 'accept: application/json' \
  -H 'cache-control: no-cache' \
  -H 'content-type: text/plain' \
  -d 'bla'

the following definition behaves correctly:

(POST "/foo" []
        :middleware [[muuntaja.middleware/wrap-format
                      (muuntaja/create
                        (assoc-in muuntaja/default-options
                                  [:formats "text/plain"] text-plain-format))]]
        :query-params [version :- String, rule_name :- String, force_overwrite :- String]
        :body [b s/Str]
        :return s/Any           
        (ok
          {:ok b})

on other hand this one is failing

(context "/foo" []
        (resource {
                   :post {
                          :middleware [[muuntaja.middleware/wrap-format
                                        (muuntaja/create
                                          (assoc-in muuntaja/default-options
                                                    [:formats "text/plain"] text-plain-format))]]

                          :parameters {:body-params  String
                                       :query-params {:version         s/Str
                                                      :rule_name       s/Str
                                                      :force_overwrite s/Str}}
                          :consumes   ["text/plain"]
                          :responses  {http-status/ok {:schema s/Any}}
                          :handler    (fn [{body :body-params {:keys [version rule_name]} :path-params}]
                                        (ok
                                          {:foo body}
                                          ))}}))

the error is:

{
    "schema": "java.lang.String",
    "errors": "(not (instance? java.lang.String nil))",
    "type": "compojure.api.exception/request-validation",
    "coercion": "schema",
    "value": null,
    "in": [
        "request",
        "body-params"
    ]
}

I use this auxiliary fun:


(def text-plain-format
  {:decoder [(fn make-json-decoder [options]
               (fn [x ^String charset]
                 (if (string? x)
                   x
                   (slurp (InputStreamReader. ^InputStream x charset)))))]

   :encoder [(fn make-json-encoder [options]
               (fn [data ^String charset]
                 (ByteArrayInputStream. (.getBytes data charset))))]})

danieldroit avatar Jul 04 '17 14:07 danieldroit