metosin / compojure-api

Sweet web apis with Compojure & Swagger
http://metosin.github.io/compojure-api/doc/
Eclipse Public License 1.0
1.11k stars 149 forks source link

prevent error caused by trying to confrom value that has already been… #436

Open antonmos opened 4 years ago

antonmos commented 4 years ago

… coerced

in my situation, i am using a spec like this to transform the parameter:

(def get-events-spec
  (st/spec
    {:spec (s/coll-of string?)
     :description "comma separated list of user-ids"
     :json-schema/type {:type "string"}
     :json-schema/example "1,2,3"
     :decode/string #(cs/split %2 #",")
     :encode/string #(cs/join %2 ",")}))

referenced like this

(GET "/events" []
      :coercion :spec
      :query-params [user-ids :- get-events-spec,
                                  max-age :- ::corespec/max-age ]
...
)

When invoked with GET /events?user-ids=1 (i.e. with missing max-age param), I get a class cast exception thrown from cs/split. This happens because the old code was calling conform on result of st/coerce, which means that the string has already been coerced into a vector of strings.