Skip to content

Commit e1df7ec

Browse files
committed
Adds support for string to collection coercion based on collectionFormat
Fixes #69
1 parent 8341b31 commit e1df7ec

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/ring/swagger/coerce.clj

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,22 @@
127127
(date-matcher schema)
128128
(pattern-matcher schema)))
129129

130+
(def collection-format-split-regex
131+
{"csv" #","
132+
"ssv" #" "
133+
"tsv" #"\t"
134+
"pipes" #"\|"})
135+
130136
(defn split-params-matcher [schema]
131137
(if (or (and (coll? schema) (not (record? schema))))
132-
(fn [x]
133-
(if (string? x)
134-
(string/split x #",")
135-
x))))
138+
;; FIXME: Can't use json-schema/json-schema-meta because of cyclic dependency
139+
(let [collection-format (:collectionFormat (:json-schema (meta schema)) "csv")
140+
split-regex (get collection-format-split-regex collection-format)]
141+
(if split-regex
142+
(fn [x]
143+
(if (string? x)
144+
(string/split x split-regex)
145+
x))))))
136146

137147
(defn multi-params-matcher
138148
"If only one parameter is provided to multi param, ring
@@ -148,7 +158,7 @@
148158
(defn query-schema-coercion-matcher
149159
[schema]
150160
(or (query-coercions schema)
151-
; (split-params-matcher schema)
161+
(split-params-matcher schema)
152162
(multi-params-matcher schema)
153163
(json-schema-coercion-matcher schema)))
154164

test/ring/swagger/coerce_test.clj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
(ns ring.swagger.coerce-test
22
(:require [midje.sweet :refer :all]
33
[schema.core :as s]
4+
[schema.coerce :as sc]
45
[ring.swagger.coerce :as rsc]
6+
[ring.swagger.json-schema :as json-schema]
57
[schema.coerce :as sc])
68
(:import [org.joda.time LocalDate DateTime]
79
[java.util Date UUID]))
@@ -52,3 +54,22 @@
5254
((coerce UUID) #uuid "77e70512-1337-dead-beef-0123456789ab") => #uuid "77e70512-1337-dead-beef-0123456789ab"
5355
((coerce UUID) "77e70512-1337-dead-beef-0123456789ab") => #uuid "77e70512-1337-dead-beef-0123456789ab"
5456
((coerce UUID) "invalid") => "invalid")))
57+
58+
(fact "collection-format coercions"
59+
(let [coercer #(sc/coercer % rsc/query-schema-coercion-matcher)]
60+
61+
(fact "multi"
62+
((coercer (json-schema/field [s/Int] {:collectionFormat "multi"})) ["1" "2"]) => [1 2])
63+
64+
(fact "csv"
65+
((coercer (json-schema/field [s/Int] {:collectionFormat "csv"})) "1,2") => [1 2])
66+
67+
(fact "ssv"
68+
((coercer (json-schema/field [s/Int] {:collectionFormat "ssv"})) "1 2") => [1 2])
69+
70+
(fact "tsv"
71+
((coercer (json-schema/field [s/Int] {:collectionFormat "tsv"})) "1\t2") => [1 2])
72+
73+
(fact "pipes"
74+
((coercer (json-schema/field [s/Int] {:collectionFormat "pipes"})) "1|2") => [1 2])
75+
))

test/ring/swagger/json_schema_test.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@
117117
(fact "s/cond-pre"
118118
(->swagger (s/cond-pre Model [s/Str]))
119119
=> {:type "void" :oneOf [(->swagger Model) (->swagger [s/Str])]})
120-
))
120+
)
121+
122+
(fact "collection format"
123+
(->swagger (field [Long] {:collectionFormat "multi"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "multi"}
124+
(->swagger (field [Long] {:collectionFormat "csv"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "csv"}
125+
(->swagger (field [Long] {:collectionFormat "tsv"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "ssv"}
126+
(->swagger (field [Long] {:collectionFormat "tsv"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "tsv"}
127+
(->swagger (field [Long] {:collectionFormat "pipes"})) => {:type "array" :items {:format "int64" :type "integer"} :collectionFormat "pipes"}))
121128

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

0 commit comments

Comments
 (0)