Fork me on GitHub
#ring-swagger
<
2017-02-17
>
mrchance10:02:11

hi, I'm writing a test for my compojure-api app, and I am a little stumped... I have

(deftest dummy-test
  (let [request (-> (mock/request :post "/foo")
                    (mock/body (json/generate-string foo-request))
                    (mock/header "jwt" valid-jwt))
        response (dummy-app request)
    (is (= 200 (:status response)))))
But the status is 400, with the error: {"errors" "(not (map? nil))"}

mrchance10:02:25

The app works when I run the server and test it through swagger ui

mrchance10:02:12

foo-request is a map, and the request looks correct when I print it

mrchance12:02:30

I solved it, needed a (mock/header "Content-Type" "application/json")

mrchance12:02:42

Not very helpful error message there, but I guess it's hard to do it better

ikitommi13:02:21

@mrchance I'm not sure what could be done with that. Maybe a configurable dev-middleware which one could use to inspect request & response? Open to ideas .

mrchance13:02:09

Yes, not sure how to improve on that either, maybe something like "Content type missing, interpreted as Text/Plain, couldn't parse into map", at least in dev...

daveliepmann13:02:55

I have a POST with query params like {:a [{:b 1} {:b 2}]} and I'd like to define my schema so that the Swagger UI explains the format of each map in the sequence. But it seems that I have to define the query as one schema and the maps or list of maps as another schema, and that second one is referred to in the UI under "Data Type" as simply "Array[AMap]" instead of describing the format of AMap. Am I missing something in either Schema or ring-swagger?

ikitommi14:02:40

@daveliepmann the swagger spec doesn’t understand nested query-params, for body-params (`:body [params MultipleMaps]`) it shows them correctly

ikitommi14:02:18

also, the (s/defschema MultipleMaps {:a [{:b s/Str}]}) works with body-params with ring-swagger.

ikitommi14:02:12

for query-params, it doesn’t work. It’s between a bug and a feature. Should work (as a code) but not (by the spec?).

ikitommi14:02:21

(POST "/multiplemaps" []
  :body  [params MultipleMaps]
  :return {:params MultipleMaps}
  (ok {:params params}))

ikitommi14:02:55

should work

ikitommi14:02:05

if you can work with body-params that is.

daveliepmann14:02:03

@ikitommi Thank you—`body [params...` should be a viable path for me. However I don't quite understand why the schema doesn't work for query-params. Do you mean that it is an invalid query?

ikitommi14:02:50

sorry, not your fault - it’s a ~bug in ring-swagger, it doesn’t seem to emit swagger-spec out of :query-params which have anonymous nested classes. If you need it, please write an issue to github for it.

ikitommi14:02:27

but you solution of using the named inner Schema should work ok. Just renders funnily.

ikitommi14:02:46

and not sure how you can produce such query-params.

daveliepmann14:02:14

I think body-params is the most correct and, as you said, works like a charm, so I'll run with that

mrchance23:02:48

Hi, I am trying to enforce a permissions check on every endpoint. My approach was to write a middleware wrapping all of the compojure api app that returns unauthorized unless there is a ::verified or something in the response, put in there via a middleware provided by a custom metadata handler that also defines the required permissions. The problem is that invalid requests bypass this and the resource returns 401 where it should be 400. Any good ideas how to solve this nicely?

mrchance23:02:25

We have a leiningen template providing basic safe setup, but there is nothing stopping people from forgetting the checks when they add new endpoints

mrchance23:02:48

Custom metadata handlers are awesome btw., I think this is the first time I've seen a macro that was extensible via multimethods 🙂