Fork me on GitHub
#ring-swagger
<
2017-01-23
>
d5p14:01:46

hmm, bit of a strange one, using compojure-api resources in front of my liberator resources , in liberator i’m setting CORS headers for all responses (access-control-allow-origin etc) , these are being returned correctly for all request methods except for OPTIONS , where they are not being returned at all

d5p14:01:19

example of how i’m defining the compojure-api resource:

d5p14:01:28

(defn site-resource [db-spec]
  (api/resource
    {:tags ["sites"]
     :description "Sites"
     :head {:summary "Return HTTP Headers"
            :responses {status/ok nil}}
     :options {:summary "Return available HTTP OPTIONS"
               :responses {status/ok nil}}
     :get {:summary "Return Site records, optionally filtered by URL parameters."
           :parameters {:query-params SiteQueryParams}
           :responses  {status/ok {:schema SiteArrayResponse}}}
     :post {:summary "Create a new Site record."
            :parameters {:body-params Site}
            :responses  {status/created {:schema SiteCreated}}}
     :handler (site db-spec)}))

d5p14:01:54

doesn’t seem like it should be required to specify these headers in my responses since they are being returned fine for everything other than OPTIONS

ikitommi14:01:45

@d5p I’ll have a look

d5p14:01:18

thanks, i’m poking around the source trying to follow the execution path but yeh taking a bit to get my head around it 🙂

d5p14:01:39

it is returning an Allow header for OPTIONS that i’m not generating in my liberator resource as well

ikitommi14:01:07

hmm… the resource isn’t doing anything extra, just coercion and selecting the right handler.

ikitommi14:01:43

if you call the liberator resource directly, it works ok?

d5p14:01:04

and actually just stuck a debug log in , on OPTIONS it doesn’t appear to be calling my liberator handler at all

d5p14:01:31

any middleware further up the chain that would be using the keys in my resource definition to generate the Allow header? as that appears to be what’s happening

ikitommi14:01:11

hmm… sounds like it. compojure-api doesn’t do anything like that, or shoudn’t at least.

d5p14:01:51

i’m just using api-middleware , so been poking through that

ikitommi14:01:52

((api
   (context "/" []
     (resource {:handler (fn [_] {:headers "kikka"})}))) 
  {:request-method :options, :uri "/"})
; => {:headers "kikka"}

ikitommi14:01:46

that’s the minimalstic set: api + context + resource => nothing looking for OPTIONS.

ikitommi14:01:49

do you have some extra mw like ring-defaults?

ikitommi14:01:35

quickly looked at ring code at github, didn’t find anything there either.

d5p14:01:11

nah i removed all other middleware , just have api-middleware , ring-logger and liberator.dev/wrap-trace

ikitommi14:01:27

still doesn’t work?

d5p14:01:21

nope 😕 yeh odd can’t see anything anywhere that would be swallowing this request

ikitommi14:01:04

if you replace your liberator handler with something like (constantly {:headers “kikka”}), does it return it?

ikitommi14:01:30

only place I found any mark of OPTIONS was the Liberator codebase itself, some rules about it: https://github.com/clojure-liberator/liberator/blob/60ca12f12d1a322202de5659b8ab510d9bcc7884/src/liberator/core.clj

ikitommi14:01:50

is it possible that the liberator resource is consuming it?

d5p14:01:54

hmm no when i replace my liberator resource with :handler (constantly {:headers “kikka”}) i get a 500 java.lang.UnsupportedOperationException: nth not supported on this type: Character

ikitommi14:01:16

ah, should be something like (constantly {:headers {”kikka” “kukka”}})

ikitommi14:01:33

was running from repl, so no ring-adapter there

d5p14:01:08

yeah hmm so that works fine

d5p14:01:25

hmm, actually before i misspke , my handler was getting called , i can see my liberator decision graph , it just wasn’t getting to the response handler which works ok when i wasn’t using compojure-api

d5p14:01:42

strange so :as-response which lets me send back through the raw response , is called when i use the liberatr resource directly , for some reason not being passed back now

d5p14:01:51

ha ahh well will continue digging , must be something funky liberator side

d5p14:01:44

is it possible to set custom headers for all compojure-api responses? could always move the logic there

ikitommi14:01:57

sure, just add a middleware somewhere into the request pipeline.

d5p15:01:28

ah yeh sweet

d5p15:01:30

great thanks