Skip to content

Commit

Permalink
Adds support for string to collection coercion based on collectionFormat
Browse files Browse the repository at this point in the history
Fixes #69
  • Loading branch information
Deraen committed May 24, 2016
1 parent 8341b31 commit 7f03367
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/ring/swagger/coerce.clj
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,22 @@
(date-matcher schema)
(pattern-matcher schema)))

(def collection-format-split-regex
{"csv" #","
"ssv" #" "
"tsv" #"\t"
"pipes" #"\|"})

(defn split-params-matcher [schema]
(if (or (and (coll? schema) (not (record? schema))))
(fn [x]
(if (string? x)
(string/split x #",")
x))))
;; FIXME: Can't use json-schema/json-schema-meta because of cyclic dependency
(let [collection-format (:collectionFormat (:json-schema (meta schema)) "csv")
split-regex (get collection-format-split-regex collection-format)]
(if split-regex
(fn [x]
(if (string? x)
(string/split x split-regex)
x))))))

(defn multi-params-matcher
"If only one parameter is provided to multi param, ring
Expand All @@ -148,7 +158,7 @@
(defn query-schema-coercion-matcher
[schema]
(or (query-coercions schema)
; (split-params-matcher schema)
(split-params-matcher schema)
(multi-params-matcher schema)
(json-schema-coercion-matcher schema)))

Expand Down
21 changes: 21 additions & 0 deletions test/ring/swagger/coerce_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns ring.swagger.coerce-test
(:require [midje.sweet :refer :all]
[schema.core :as s]
[schema.coerce :as sc]
[ring.swagger.coerce :as rsc]
[ring.swagger.json-schema :as json-schema]
[schema.coerce :as sc])
(:import [org.joda.time LocalDate DateTime]
[java.util Date UUID]))
Expand Down Expand Up @@ -52,3 +54,22 @@
((coerce UUID) #uuid "77e70512-1337-dead-beef-0123456789ab") => #uuid "77e70512-1337-dead-beef-0123456789ab"
((coerce UUID) "77e70512-1337-dead-beef-0123456789ab") => #uuid "77e70512-1337-dead-beef-0123456789ab"
((coerce UUID) "invalid") => "invalid")))

(fact "collection-format coercions"
(let [coercer #(sc/coercer % rsc/query-schema-coercion-matcher)]

(fact "multi"
((coercer (json-schema/field [s/Int] {:collectionFormat "multi"})) ["1" "2"]) => [1 2])

(fact "csv"
((coercer (json-schema/field [s/Int] {:collectionFormat "csv"})) "1,2") => [1 2])

(fact "ssv"
((coercer (json-schema/field [s/Int] {:collectionFormat "ssv"})) "1 2") => [1 2])

(fact "tsv"
((coercer (json-schema/field [s/Int] {:collectionFormat "tsv"})) "1\t2") => [1 2])

(fact "pipes"
((coercer (json-schema/field [s/Int] {:collectionFormat "pipes"})) "1|2") => [1 2])
))
9 changes: 8 additions & 1 deletion test/ring/swagger/json_schema_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@
(fact "s/cond-pre"
(->swagger (s/cond-pre Model [s/Str]))
=> {:type "void" :oneOf [(->swagger Model) (->swagger [s/Str])]})
))
)

(fact "collection format"
(->swagger (field [Long] {:collectionFormat "multi"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "multi"}
(->swagger (field [Long] {:collectionFormat "csv"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "csv"}
(->swagger (field [Long] {:collectionFormat "ssv"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "ssv"}
(->swagger (field [Long] {:collectionFormat "tsv"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "tsv"}
(->swagger (field [Long] {:collectionFormat "pipes"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "pipes"}))

(fact "Optional-key default metadata"
(properties {(with-meta (s/optional-key :foo) {:default "bar"}) s/Str})
Expand Down

0 comments on commit 7f03367

Please sign in to comment.