Fork me on GitHub
#ring-swagger
<
2017-08-08
>
henrik05:08:06

(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?

ikitommi05:08:05

return coercion checks the returned response :status and selects the corresppnding model under :responses

ikitommi05:08:26

so, same handler

henrik05:08:43

@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?

henrik05:08:59

Ah, no. :status.

henrik05:08:23

Sorry, slow brain morning.

nickmbailey17:08:25

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

nickmbailey17:08:50

is that right? just seems like a roundabout way of doing it

ikitommi17:08:11

coercion should happen before exceptions.

ikitommi17:08:24

there are utilities in ring-swagger for doing the json/string coercions with schema.

nickmbailey17:08:22

well so what i’m trying to accomplish is ser/deser of joda.money.Money

nickmbailey17:08:44

the json serialization is easy, just extend the convert-class multimethod

nickmbailey17:08:19

trying to figure out how to do the coercion from json input to a Money object

nickmbailey17:08:48

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.”

nickmbailey17:08:52

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

ikitommi17:08:59

I'll give a pointer in 10min

ikitommi18:08:19

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

ikitommi18:08:39

I think the 3 should also have a multimethod for easy adding of types to coercion.

ikitommi18:08:03

the time-matcher in that commit seems to do that actually.

nickmbailey18:08:02

well time-matcher is already in json-schema-coercion-matcher

ikitommi18:08:20

(defmethod ring.swagger.coerce/time-matcher Money [_] (coerce...)) could work?

nickmbailey18:08:28

yeah step 3 is the tricky part then, i need to redef those things/

nickmbailey18:08:35

yeah that hack might work

juhoteperi18:08:42

Money is not time 😕

nickmbailey18:08:49

definitely not ideal

ikitommi18:08:09

we could change that to match-identity etc?

ikitommi18:08:45

as it’s not tied to time anyway: (defmulti time-matcher identity) 😮

ikitommi18:08:02

without that, one has to redefine a lot of things.

nickmbailey18:08:02

yeah its just named too specifically

ikitommi18:08:30

also, as the Yoda is already a dependency, the Money mappings could be in ring-swagger.

ikitommi18:08:35

would you like to do a PR?

juhoteperi18:08:57

joda-money is not dependency

juhoteperi18:08:00

joda-time is

nickmbailey18:08:14

clj-time pulls in joda time but not joda money

ikitommi18:08:26

oh, but we have the “if-the-classes-are-found” macros for these.

ikitommi18:08:35

(extension/java-time
  (defmethod time-matcher java.time.Instant [_]
    (coerce-if-string (fn [x] (java.time.Instant/parse x))))

ikitommi18:08:52

=> those plug in if one has the dependency

nickmbailey18:08:26

wouldn’t that complain if joda time wasn’t on the classpath?

ikitommi18:08:22

thanks to macros, the require is done within try. bit dirty, but handy.

ikitommi18:08:09

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

nickmbailey18:08:56

well this works:

nickmbailey18:08:06

(defmethod coerce/time-matcher org.joda.money.Money  [_]  (coerce/coerce-if-string ma/parse))

nickmbailey18:08:30

so i wonder if the best approach is just rename time-matcher to custom-matcher

nickmbailey18:08:11

i’ll at least file an issue

ikitommi18:08:35

+1 for the rename