This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-08-08
Channels
- # aleph (4)
- # beginners (5)
- # cljs-dev (21)
- # clojure (155)
- # clojure-dev (3)
- # clojure-italy (10)
- # clojure-losangeles (3)
- # clojure-nl (2)
- # clojure-russia (5)
- # clojure-spec (42)
- # clojure-uk (11)
- # clojurescript (170)
- # code-art (1)
- # component (3)
- # core-async (28)
- # cursive (70)
- # data-science (3)
- # datascript (1)
- # datomic (28)
- # emacs (6)
- # gorilla (1)
- # graphql (2)
- # jobs (1)
- # lein-figwheel (4)
- # lumo (7)
- # off-topic (13)
- # om (63)
- # parinfer (66)
- # planck (1)
- # re-frame (22)
- # reagent (2)
- # ring-swagger (53)
- # rum (3)
- # sql (13)
- # test-check (2)
- # unrepl (48)
- # vim (8)
- # yada (33)
(def plus-resource
(resource
{:get {:parameters {:query-params {:x Long, :y Long}}
:responses {200 {:schema {:total Long}}}
:handler my-plus-request-handler-with-coerced-inputs-and-outputs}}))
In order to support more responses (404, 500, what-have-you), do I provide one handler for each response? Or should the one handler return all possible responses? How does it know which schema to map to which return from the handler?return coercion checks the returned response :status
and selects the corresppnding model under :responses
@ikitommi So does that mean that it returns, say, 500 when the shape of the data is found to comply with the spec for 500?
the readme seems to indicate that the proper way to add custom coercions to ring-swagger is to catch the validation errors in middleware and do the custom coercion there
is that right? just seems like a roundabout way of doing it
there are utilities in ring-swagger for doing the json/string coercions with schema.
well so what i’m trying to accomplish is ser/deser of joda.money.Money
the json serialization is easy, just extend the convert-class multimethod
trying to figure out how to do the coercion from json input to a Money object
the only way i see to do that though is in middleware. the readme says “One can catch these exceptions via ring.swagger.middleware/wrap-validation-errors and return a JSON-friendly map of the contents.”
i suppose i can just create a new schema called Money with amount and currency primitive types and convert that internally to joda.money.Money
@nickmbailey here’s a commit that added support for new types: https://github.com/metosin/ring-swagger/commit/8ed688a057a695f602f173502130c62e0247cc7f
1) Protocol extension to Cheshire to support encoding
2) A Schema matcher to be used in coercion
3) Create new versions of json-schema-coercion-matcher
& query-schema-coercion-matcher
that have the matcher
well time-matcher is already in json-schema-coercion-matcher
yeah step 3 is the tricky part then, i need to redef those things/
yeah that hack might work
Money is not time 😕
definitely not ideal
yeah its just named too specifically
also, as the Yoda is already a dependency, the Money mappings could be in ring-swagger.
joda-money is not dependency
joda-time is
right
clj-time pulls in joda time but not joda money
(extension/java-time
(defmethod time-matcher java.time.Instant [_]
(coerce-if-string (fn [x] (java.time.Instant/parse x))))
wouldn’t that complain if joda time wasn’t on the classpath?
same with compojure-api with Manifold & core.async, not direct dependencies, but get mapped if found: https://github.com/metosin/compojure-api/blob/master/src/compojure/api/async.clj
ah gotcha
well this works:
(defmethod coerce/time-matcher org.joda.money.Money [_] (coerce/coerce-if-string ma/parse))
so i wonder if the best approach is just rename time-matcher to custom-matcher
or similar
i’ll at least file an issue