Skip to content

Commit

Permalink
Retain original value in 'ring-response' metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Scherer committed Mar 3, 2015
1 parent 9b21024 commit f44ddc8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/liberator/representation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@
(defrecord RingResponse [ring-response value]
Representation
(as-response [_ context]
(let [base (when value (as-response value context))]
(util/combine base ring-response))))
(-> (when value
(as-response value context))
(util/combine ring-response)
(vary-meta assoc :liberator/value value))))

(defn ring-response
"Returns the given map as a ring response. The map is not converted
Expand Down Expand Up @@ -260,7 +262,10 @@
{:headers {\"Content-Type\" \"application/json\"
\"X-Custom\" \"value\"}
:status 999
:body \"{'foo': 'bar'}\"} "
:body \"{'foo': 'bar'}\"}
The response will contain the original value in its metadata, using the
`:liberator/value` key."
([ring-response-map] (ring-response nil ring-response-map))
([value ring-response-map] (->RingResponse ring-response-map value)))

Expand Down
8 changes: 7 additions & 1 deletion test/test_representation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@
(as-response (ring-response "foo" {:headers {"X-Foo" "bar"}})
{:status 200})
=> (contains {:headers {"X-Foo" "bar"
"Content-Type" "text/plain;charset=UTF-8"}})))))
"Content-Type" "text/plain;charset=UTF-8"}}))))
(fact "retains original value in metadata"
(let [response (as-response
(ring-response {:foo :bar} {:status 200})
{:representation {:media-type "application/json"}})]
(:body response) =not=> {:foo :bar}
(-> response meta :liberator/value) => {:foo :bar})))

0 comments on commit f44ddc8

Please sign in to comment.