Fork me on GitHub
#ring-swagger
<
2017-01-06
>
vladclj08:01:25

Hi, I have question about ring-swagger. I have one handler, and if you pass one param - you get one scheme. And if you pass some specific param - you get another scheme. How to validate different scheme depending on the http params?

ikitommi16:01:21

@metametadata the fnk syntax only works with keyword-keys, so when *-params is used, all keys must be changed.

ikitommi16:01:39

(require '[plumbing.core :refer [fnk])
((fnk [a b] [a b]) {:a 1 :b 2})
; => [1 2]

((fnk [a b] [a b]) {:a 1 "b" 2})
; CompilerException clojure.lang.ExceptionInfo: Key :b not found in (:a "b") {:error :missing-key, :key :b, :map {:a 1, "b" 2}}, compiling:(/Users/tommi/projects/metosin/compojure-api/src/compojure/api/meta.clj:346:1)

ikitommi16:01:43

there is no reason why they are changed with :body, so with small change they could retain the keys as-is.

ikitommi16:01:24

e.g. :body [data {:item s/Any}].

metametadata16:01:47

@ikitommi alright, that's not obvious but probably makes sense. I'll try this later, thank you!

metametadata16:01:15

I'm not sure if it's important at the moment but I forgot to add that it was a map inside the item that got its keys turned from the strings to keywords. kind of like this: item = {:a :b :nested-map {"a" "b" "c" "d"})

metametadata16:01:16

yeah, using :body didn't help - it still converts the keys in the nested map

abarylko17:01:51

Is there any way to add documentation describing the response for a handler? For example if I am returning a JSON with customer information I'd like to be able to describe each field.....

ikitommi19:01:11

@metametadata could you write an issue about the :body? I don’t see a reason it forces the keys into keywords. Could fix that for 1.2.

ikitommi19:01:27

for the nil coercion… with schema, validation is done within the coercion, so if one disables the coercion, there is no validation.

ikitommi19:01:33

but like the :body, the response coercion should not transform the keys into keywords => sounds like a bug/feature.

ikitommi19:01:10

@vladclj you could try s/cond-pre, s/if or s/abstract-map-schema. See https://github.com/plumatic/schema#other-schema-types for details

ikitommi19:01:18

@abarylko you can use :responses to describe the responses (set the response name). To describe the individual fields, you can use ring-swagger or describe. Some pointers: https://github.com/metosin/compojure-api/blob/c2274ee56334f9bfae05d69c4311cb95d18626fc/examples/thingie/src/examples/thingie.clj#L103-L104 & https://github.com/metosin/ring-swagger#example. I think this is a common case, and should be documented in the wiki...

vladclj19:01:08

thanks) for support

abarylko20:01:15

Thanks @ikitommi ! Very much appreciated 🙂