Clojure defresource argument arity error with Liberator

106 views Asked by At

I am writing a clojure function that works fine when I am using defnto define it, but it throws arity error when instead I define it using defresource. I suspect it's something to do with the :as-response key that I am using but I have no clue how to fix it. Any ideas? My code:

(defn function-name []
  :allowed-methods [:get]
  :available-media-types ["application/json"]
  :as-response (my function is here))

Error when using defresource: "Wrong number of args (2) passed to ... " I am passing 0 arguments which works fine using defn.

1

There are 1 answers

1
Phillippe Siclait On BEST ANSWER

The defn that you’ve defined is equivalent to

(defn function-name 
  []
  (do
    :allowed-methods 
    [:get]
    :available-media-types 
    ["application/json"]
    :as-response 
    (...)))

Since the value of the do expression is the last form in the expression — and the keywords and vectors don’t have any side effects — the function definition is equivalent to

(defn function-name 
  []
  (...))

What you have is a 0-parameter arity function. The :as-response and other keywords / vectors don’t do anything.

Looking at the documentation for Liberator, the library expects a 2-parameter arity function when using :as-response. Also according to the documentation on resources you only need to pass a parameter vector to defresource if you’d like a parameterized resource. So your resource should be defined

(defresource function-name
  :allowed-methods [:get]
  :available-media-types ["application/json"]
  :as-response (fn [d ctx] ...))