metosin / compojure-api

Sweet web apis with Compojure & Swagger
http://metosin.github.io/compojure-api/doc/
Eclipse Public License 1.0
1.11k stars 149 forks source link

spec/with-gen breaks swagger spec #411

Closed willspecht closed 5 years ago

willspecht commented 5 years ago

Library Version(s) "2.0.0-alpha28"

Problem

If you use a spec that is created with with-gen it breaks swagger generation, with error:

10:55:26.898 ERROR [compojure.api.exception] (XNIO-8 task-8) Unable to resolve spec: :clojure.spec.alpha/unknown
java.lang.Exception: Unable to resolve spec: :clojure.spec.alpha/unknown
    at clojure.spec.alpha$reg_resolve_BANG_.invokeStatic(alpha.clj:69) ~[spec.alpha-0.2.176.jar:na]
    at clojure.spec.alpha$reg_resolve_BANG_.invoke(alpha.clj:64) ~[spec.alpha-0.2.176.jar:na]
    at clojure.spec.alpha$fn__1803.invokeStatic(alpha.clj:132) ~[spec.alpha-0.2.176.jar:na]
    at clojure.spec.alpha$fn__1803.invoke(alpha.clj:130) ~[spec.alpha-0.2.176.jar:na]
    at clojure.spec.alpha$fn__1782$G__1777__1791.invoke(alpha.clj:121) ~[spec.alpha-0.2.176.jar:na]
    at clojure.spec.alpha$specize.invokeStatic(alpha.clj:152) ~[spec.alpha-0.2.176.jar:na]
    at clojure.spec.alpha$specize.invoke(alpha.clj:151) ~[spec.alpha-0.2.176.jar:na]
    at clojure.spec.alpha$form.invokeStatic(alpha.clj:177) ~[spec.alpha-0.2.176.jar:na]
    at clojure.spec.alpha$form.invoke(alpha.clj:173) ~[spec.alpha-0.2.176.jar:na]
    at spec_tools.visitor$spec_dispatch.invokeStatic(visitor.cljc:13) ~[na:na]
    at spec_tools.visitor$spec_dispatch.invoke(visitor.cljc:9) ~[na:na]
    at clojure.lang.MultiFn.invoke(MultiFn.java:238) ~[clojure-1.10.0.jar:na]
    at spec_tools.visitor$visit.invokeStatic(visitor.cljc:52) ~[na:na

Sample c2 code that recreates the issue

(ns c2.spec
  (:require [compojure.api.sweet :refer [context GET POST resource]]
            [ring.util.http-response :refer [ok]]
            [clojure.spec.alpha :as s]
            [spec-tools.spec :as spec]
            [clojure.spec.gen.alpha :as gen]))

(def valid-guid?
  (s/with-gen
    int?
    (constantly 1)))

(s/def ::search-id valid-guid?)

(def routes
  (context "/" []
    :tags ["spec"]
    :coercion :spec

    (POST "/get-guid" []
      :summary "always return 1"
      :return ::search-id
      (ok {:inst (gen/generate (s/gen ::search-id))}))))
willspecht commented 5 years ago

This is only an issue with clojure 9, updating to 10 fixes this issue. It seems in 9 s/form gives :clojure.spec.alpha/unknown for any with-gen spec

willspecht commented 5 years ago

also to note, you can't use anonymous functions in with-gen or it will break as well.