metosin / spec-tools

Clojure(Script) tools for clojure.spec
Eclipse Public License 2.0
593 stars 94 forks source link

ArityException Wrong number of args (2) passed to: clojure.lang.PersistentVector #242

Closed Biserkov closed 4 years ago

Biserkov commented 4 years ago

This code works on 0.10.0, but breaks in 0.10.1 - 0.10.4. Please advise on any workarounds?

(ns core-account.models.bug
  (:require [clojure.spec.alpha :as s]
            [spec-tools.core :as st]))

(s/def ::a int?)
(s/def ::b int?)
(s/def ::c int?)

(s/def ::d (st/spec (s/merge (s/or :one (s/keys :req-un [::a])
                                   :two (s/keys :req-un [::b]))
                             (s/keys :req-un [::c]))))

;; ArityException Wrong number of args (2) passed to: clojure.lang.PersistentVector
;; spec-tools.parse/eval1987/fn--1988/type-priority--1989 (parse.cljc:192)
;; clojure.core/sort-by/fn--5978 (core.clj:3131)
;; clojure.lang.AFunction.compare (AFunction.java:51)
;; java.util.TimSort.countRunAndMakeAscending (TimSort.java:355)
;; java.util.TimSort.sort (TimSort.java:220)
;; java.util.Arrays.sort (Arrays.java:1441)
;; clojure.core/sort (core.clj:3115)
;; clojure.core/sort-by (core.clj:3119)
;; clojure.core/sort-by (core.clj:3119)
;; clojure.core/sort-by (core.clj:3119)
;; spec-tools.parse/eval1987/fn--1988 (parse.cljc:196)
;; clojure.lang.MultiFn.invoke (MultiFn.java:234)
Biserkov commented 4 years ago

To answer my own question: wrapping the s/or in an st/spec seems to work?

(s/def ::d (st/spec (s/merge (st/spec (s/or :one (s/keys :req-un [::a])
                                            :two (s/keys :req-un [::b])))
                             (s/keys :req-un [::c]))))
wandersoncferreira commented 4 years ago

Hello @Biserkov , the problem is the following: there is a bug introduced with multi-spec support after version 0.10.0 at the parse-form function. Your spec is decomposed by parse-form which yields :type [:or [:map]]to the inner most portion of your spec.

When we look at the parse-form, we find (current-version):

(defmethod parse-form 'clojure.spec.alpha/merge [_ form]
  (let [type-priority #((:type %) {:map 0
                                   :multi-spec 1} 0)]
    (apply impl/deep-merge (->> (rest form)
                                (map parse-spec)
                                (sort-by type-priority)))))

The ArityException is thrown by this operation:

 #([:or [:map]] {:map 0
                 :multi-spec 1} 0)
 ;; => Wrong number of args (2) passed to: clojure.lang.PersistentVector

The same code in version 0.10.0 looks like this:

(defmethod parse-form 'clojure.spec.alpha/merge [_ form]
  (apply impl/deep-merge (map parse-spec (rest form))))

Does not contain the portion to sort by priority, thus no error. I opened a PR to change the type-priority function to be more defensive.