ring-swagger & compojure-api
slipset 2017-11-15T09:55:42.000437Z

A little bit on error messages:

slipset 2017-11-15T09:56:13.000269Z

I was playing around with specs on path-params, and I thought the :- thingy was schema-only, so I made my thing like this

slipset 2017-11-15T09:56:25.000166Z

(context "/product/:id" []
          :coercion :spec
          :path-params [id ::id])

slipset 2017-11-15T09:57:13.000082Z

The error I get is either

slipset 2017-11-15T09:57:15.000122Z

CompilerException java.lang.ClassCastException: clojure.lang.Keyword cannot be cast to clojure.lang.IObj, compiling:(/Users/erik/Documents/telenordigital.com/di-data-inventory/src/data_inventory/handler.clj:225:9) 

slipset 2017-11-15T09:57:48.000290Z

when I only evaluate this form, or

slipset 2017-11-15T09:57:58.000208Z

CompilerException java.lang.ClassCastException, compiling:(/Users/erik/Documents/telenordigital.com/di-data-inventory/src/data_inventory/handler.clj:225:9)

slipset 2017-11-15T09:58:04.000376Z

if I evaluate the whole api.

slipset 2017-11-15T09:58:55.000399Z

I appreciate the fact that it might be hard getting good/descriptive error messages here, but I thought I’d post this as a bit of inspiration.

slipset 2017-11-15T09:59:07.000207Z

As always, thanks for the great work!

slipset 2017-11-15T13:17:57.000286Z

Also, when using :spec coercion, swagger fails with

slipset 2017-11-15T13:17:59.000376Z

500 : {"type":"unknown-exception","class":"java.lang.Exception"} <http://localhost:3000/swagger.json>

slipset 2017-11-15T13:18:09.000139Z

when at least one spec is missing.

slipset 2017-11-15T13:18:14.000181Z

so given something like

slipset 2017-11-15T13:18:41.000155Z

(s/def ::foo (s/keys :req-un [::bar]))

slipset 2017-11-15T13:18:59.000102Z

where ::bar is not defined, swagger shows nothing at all.

slipset 2017-11-15T13:19:17.000417Z

just the exeption (which in the logs tells me that no spec is found for ::bar

slipset 2017-11-15T13:19:32.000135Z

This is somewhat unfortunate, as it makes gradual typing hard.

slipset 2017-11-15T13:20:03.000156Z

This may or may not be a design choice 🙂

ikitommi 2017-11-15T13:22:17.000274Z

There was just today a discussion about this in the Clojure Google Group: https://groups.google.com/forum/m/#!topic/clojure/i8Rz-AnCoa8

ikitommi 2017-11-15T13:23:22.000083Z

I think both could be “fixed” on c-api side: 1) validate the plumbing.core/letk syntax for bad syntax 2) validate that all s/keys specs are fully set and fail nicely

ikitommi 2017-11-15T13:24:23.000165Z

could you write issues of these? and if you have time to fix ’em too, PRs welcome 🙂

ikitommi 2017-11-15T13:25:04.000062Z

and, actually, that swagger-gen should fail-early on the spec-tools.

ikitommi 2017-11-15T13:25:08.000014Z

should be easy to catch..

slipset 2017-11-15T13:25:14.000284Z

I’ll make issues for them for sure, compojure-api project?

ikitommi 2017-11-15T13:25:31.000388Z

1 to c-api, 2 to spec-tools, thanks!

slipset 2017-11-15T13:25:41.000047Z


slipset 2017-11-15T13:26:00.000131Z

Also, I hope this is the correct place to discuss these kinds of things?

ikitommi 2017-11-15T13:27:32.000073Z

yes, definetely

slipset 2017-11-15T14:12:34.000102Z

Hmm, Still some problems 😞

slipset 2017-11-15T14:13:06.000199Z

So, in a nested context, where the outer context has default coercion, I have the following:

slipset 2017-11-15T14:13:48.000315Z

(context "/product/:id" []
          :coercion :spec
          :path-params [id :- ::id]
          (GET "/foo" []
            :return boolean?
            :summary "foo"
            (ok (reports/foo 1))))

slipset 2017-11-15T14:14:17.000203Z

and ::id is defined as (spec/def ::id pos-int?)

slipset 2017-11-15T14:14:42.000396Z

This shows up fine in swagger, but when I press “try it out” i get the following stack-trace:

slipset 2017-11-15T14:15:16.000162Z

2017-11-15 14:14:47,930 [worker-4] ERROR compojure.api.exception - No implementation of method: :spec of protocol: #'schema.core/Schema found for class: clojure.lang.Keyword 
java.lang.IllegalArgumentException: No implementation of method: :spec of protocol: #'schema.core/Schema found for class: clojure.lang.Keyword
	at clojure.core$_cache_protocol_fn.invokeStatic(core_deftype.clj:583)
	at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:575)
	at schema.core$eval7391$fn__7403$G__7380__7408.invoke(core.clj:110)
	at schema.coerce$eval8639$coercer__8644$fn__8645$fn__8646.invoke(coerce.clj:32)
	at schema.spec.core$sub_checker$fn__7162.invoke(core.clj:92)

ikitommi 2017-11-15T14:37:34.000179Z

I’ll take a look on that later today

slipset 2017-11-15T14:55:23.000189Z


j0ni 2017-11-15T17:21:45.000061Z

hey all - I have a question about using middleware to transform responses. I have code I want to apply to the response generated by all my handler functions, which brings the response into conformance with the schemas I've defined for them via compojure-api. It seems that the schema validation is happening before any of the middleware gets the response. Am I missing some way to configure middleware, or must I guarantee that anything returned by a handler is in the correct form?

j0ni 2017-11-15T17:22:40.000261Z

my current workaround is a defhandler macro which adds the transformation step to the end of all my handler functions. That's a little gruesome though.

slipset 2017-11-15T20:27:00.000243Z

Ok, so a smaller repro here:

slipset 2017-11-15T20:27:25.000194Z

(context "/spec-test/:id" []
        :coercion :spec
        :path-params [id :- ::id]
        (GET "/foo/" []
          :return any?
          (ok {:id 1 :name "foo" :date (DateTime.)})))

slipset 2017-11-15T20:27:29.000008Z


slipset 2017-11-15T20:28:48.000294Z


slipset 2017-11-15T20:28:51.000023Z

(context "/spec-test" []
        :coercion :spec

        (GET "/:id/foo/" []
          :path-params [id :- ::id]
          :return any?
          (ok {:id 1 :name "foo" :date (DateTime.)})))

slipset 2017-11-15T20:28:53.000436Z

works fine

slipset 2017-11-15T20:30:13.000183Z

(context "/spec-test/:id" []
        :coercion :spec

        (GET "/foo/" []
          :path-params [id :- ::id]
          :return any?
          (ok {:id 1 :name "foo" :date (DateTime.)})))

slipset 2017-11-15T20:30:25.000355Z

seems to work as well, which is a workaround I can live with...

slipset 2017-11-15T20:34:28.000132Z

If you tell me which project this relates to, I'll be more than happy to file another issue 🙂

slipset 2017-11-15T22:47:32.000456Z

Another one

slipset 2017-11-15T22:47:37.000139Z

"pred": "clojure.lang.LazySeq@ba0ee888",

slipset 2017-11-15T22:48:10.000065Z

Should probably do a doall or smthing to realize the lazy-sql which is the pred?