Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging the return value of a decision causes duplicate values in vectors #60

Closed
ebaxt opened this issue Jul 20, 2013 · 2 comments
Closed

Comments

@ebaxt
Copy link

ebaxt commented Jul 20, 2013

:allowed-methods [:put :delete :get]
  :available-media-types ["application/json"]
  :allowed? (fn [_]
                     {::instance {:children ["one" "two"]})
  :exists? ::instance
  :can-put-to-missing? false
  :new? false
  :respond-with-entity? (fn [{{method :request-method} :request}] (= :put method))
  :put! (fn [ctx]
          (let [old (get ctx ::instance)
                new {::instance {:children ["one"]}]))
...
  :handle-ok (fn [ctx]
               (get ctx ::instance)))

The above example yields ["one" "one" "two"], but I think it's reasonable to expect the same behavior as merge?

user=> (merge {:foo ["one" "two"]} {:foo ["one"]})
{:foo ["one"]}

Caused by the use of concat on lists and vectors:
https://github.com/clojure-liberator/liberator/blob/master/src/liberator/core.clj#L77

Mail group thread:

http://groups.google.com/group/clojure-liberator/t/4c58338847eb585e

@ordnungswidrig
Copy link
Member

This is a know regression in the way combine works. liberator.util/combine works like a recursive merge but id will also concat lists, vectory and sets.

You can attach the meta data attribute :replace to prevent a merge:

user=> (combine {:foo [:bar :baz]} {:foo ^:replace [:qux]})
{:foo [:qux]}

@bago2k4
Copy link

bago2k4 commented Nov 7, 2018

We had a similar issue caused by this "deep merge/concat" of combine, we solved it using ^:replace.
For those who wonder you can't use with-meta with all values but you can add it conditionally:

(defn rep [v]
  (if (instance? clojure.lang.IMeta v)
    (with-meta v {:replace true})
    v))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants