Fork me on GitHub
#ring-swagger
<
2017-09-15
>
kennethkalmer08:09:09

hey everyone, I have a question on handling the case when a request is cancelled by a client but the handler is still doing some expensive computation… any ideas on how I could interrupt the handler as well?

kennethkalmer08:09:36

this is with compojure-api 1.1.10

ikitommi09:09:07

@kennethkalmer I added you to #ring, I think James knows best way to this with sync & async ring.

mgrbyte14:09:18

tried using compojure-api 2.0.0-alpha7, using a s/map-of spec for resource :responses and :paramters -> :body-params - does anyone know if this should work?

mgrbyte14:09:44

currently getting: java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to clojure.lang.Named

mgrbyte14:09:00

my map-of spec looks like:

(s/def ::name-new (s/and (s/keys :opt [:gene/cgc-name]
                                 :req [:gene/species
                                       (or :gene/cgc-name
                                           (and :gene/sequence-name
                                                :gene/biotype))])
                         names-valid?))

(s/def ::names-created (s/map-of st/keyword? (s/coll-of ::name-new :kind st/vector?)))

mgrbyte14:09:15

(`s/explain`. s/exercise and s/valid? work as expected on this spec)

ikitommi17:09:55

@mgrbyte the s/and had ill mappings, if you update to latest spec-tools, it should be fixed. See https://github.com/metosin/compojure-api/issues/336. Will cut out alpha8 soon with the fix.

mgrbyte09:09:45

@ikitommi tried using [metosin/spec-tools "0.4.0-20170910.061347-1"], but still get the same error unfortunately.

ikitommi10:09:15

@mgrbyte what do you get if you evaluate the spec in REPL, e.g. (st/spec (s/and (s/keys ...))?

ikitommi10:09:44

You should see :type :map and :keys #{the_keys_here} in the metadata.

mgrbyte10:09:30

{:spec
 #object[clojure.spec.alpha$and_spec_impl$reify__875 0x1708af82 "[email protected]"],
 :form
 (clojure.spec.alpha/and
  (clojure.spec.alpha/keys
   :opt
   [:gene/cgc-name]
   :req
   [:gene/species
    (clojure.core/or
     :gene/cgc-name
     (clojure.core/and :gene/sequence-name :gene/biotype))])
  org.wormbase.specs.gene/names-valid?),
 :type :map,
 :keys
 #{:gene/sequence-name :gene/cgc-name :gene/species :gene/biotype}}

mgrbyte10:09:57

seems good?

ikitommi10:09:30

well, that’s ok. Hmm.

ikitommi10:09:38

that’s the regression test I wrote. works ok.

mgrbyte10:09:43

the actual spec I'm using for {:parameters :body-params} gives:

{:spec
 #object[clojure.spec.alpha$every_impl$reify__946 0x259f0a58 "[email protected]"],
 :form
 (clojure.spec.alpha/map-of
  spec-tools.spec/keyword?
  :org.wormbase.specs.gene/names-new),
 :type :map}

mgrbyte10:09:17

where the :org.wormbase.specs.gene/names-new spec is the first one I posted above

ikitommi10:09:31

oh, there are no :keys.

ikitommi10:09:30

I’ll fix that now.

mgrbyte10:09:38

My resource def is like:

(def routes
  (sweet/routes
   (sweet/context "/gene" []
     :tags ["gene"]
     (sweet/resource
      {:coercion :spec
       :post
       {:summary "Create new names for a gene (cloned or un-cloned)"
        :parameters {:body-params ::gene-specs/names-new-request}
        :responses {201 {:schema (s/keys :req [::gene-specs/names-created])}
                    400 {:schema  ::common/error-response}}
        :handler create-new-names}}))
   (sweet/context "/gene/:id" []
     :tags ["gene"]
     :path-params [id :- :gene/id]
     (sweet/resource
      {:coercion nil
       :post
       {:summary "Add new names to an existing gene"
        :parameters {:body-params ::gene-specs/names-update-request}
        :responses {200 {:schema ::gene-specs/names-updated}
                    400 {:schema ::common/error-response}}
        :handler (fn [request]
                   (update-names request id))}}))))

mgrbyte10:09:31

::gene-specs/names-new-request is the second eval print above (the one you mention that has no :keys)

ikitommi10:09:30

could you try 0.4.0-20170918.103911-2

mgrbyte11:09:02

trying now 🙂

mgrbyte11:09:17

looks like I have less failing tests, but still some issues

mgrbyte11:09:30

org.wormbase.names.service> (st/spec ::gs/names-new-request)
{:spec
 #object[clojure.spec.alpha$every_impl$reify__946 0x3d47c365 "clojure.spec.alpha$ev[email protected]"],
 :form
 (clojure.spec.alpha/map-of
  spec-tools.spec/keyword?
  :org.wormbase.specs.gene/names-new),
 :type :map}
org.wormbase.names.service> (st/spec ::gs/names-created)
{:spec
 #object[clojure.spec.alpha$every_impl$reify__946 0x2742d814 "[email protected]"],
 :form
 (clojure.spec.alpha/map-of
  spec-tools.spec/keyword?
  (clojure.spec.alpha/coll-of
   :org.wormbase.specs.gene/name-new
   :kind
   spec-tools.spec/vector?)),
 :type :map}

mgrbyte11:09:18

getting ClassCastExceptions - double checking it's not my code...

ikitommi11:09:18

there are no keys for map-of or coll-of :into {}, just added a check that doesn’t drop keys if no keys are present.

mgrbyte14:09:36

still working through my test suite, not sure if I'll get to the end today, but I think your changes here have worked. Thank you so much! :thumbsup: 😄

ikitommi14:09:48

glad I could help

mgrbyte17:09:34

Thanks @ikitommi, will try this out over the weekend 👍