metosin / reitit

A fast data-driven routing library for Clojure/Script

Home Page:https://cljdoc.org/d/metosin/reitit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Speccing a list query parameter

gnarroway opened this issue · comments

If you spec a request query parameter as a list of string (eg [string?]), the swagger UI correctly renders it as accepting an array of strings.

However, submitting a valid input blows up with an error similar to #420 (comment)

Presumably this is happening as the input is being passed as a csv string which cannot be coerced into a list of strings.

My current workaround is to set the spec to string? and manually transform it in the handler.

  1. Is there any other method to get it to play nice with swagger and also transform correctly?
  2. As with the linked issue, is there a built in way to get more friendly input validation error messages than the current blow up?

found some further info by digging through slack archives:

It looks like the transformation is done by ring-params, which only supports multiple values passed as k=1&k=2…, so it doesn’t support csv style k=1,2..

That said, this snippet will make swagger display correctly, with the caveat you still need to split the csv manually:

 (st/spec {:spec string? :swagger/type "array" :swagger/items {:type "string"} :swagger/collectionFormat "csv"})

setting the collectionFormat to “multi” and changing the spec to (coll-of string?) will make it send in the format ring supports and will be available as a collection.