clojureswaggercompojure-api

Compojure-Api return vector of schema objects


I have started learning Luminus framework to get into Clojure(script) for web applications. For RESTful services framework suggests using swagger/compojure-api. Examples were helpful, but I can't find any for returning collections.

Here is my model:

CREATE TABLE thread
(id INTEGER PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(30));

And Conman query:

-- :name get-threads :? :*
-- :doc selects all threads
SELECT * FROM thread

For service there is a schema (it is called Threadd because of java's Thread):

(s/defschema Threadd {:id s/Int
                      :name s/Str})

Finally, in service-routes (defapi expression), simple GET:

(GET "/thread" []
      :return       Threadd
      :summary      "All threads"
      (ok  (db/get-threads)))

Application runs, and Swagger-UI returns this on my request(edn format):

{:errors "(not (map? a-clojure.lang.LazySeq))"}

Unfortunately, this is not obvious for me. How can I return multiple objects with schema specified? Can I return them in transit+json format?


Solution

  • You can define another schema for a sequence of threads:

    (s/defschema Threads [Threadd])
    

    and specify it as a return type of your endpoint:

    (GET "/thread" []
          :return       Threads
          :summary      "All threads"
          (ok  (db/get-threads)))
    

    You could also define it inline:

    (GET "/thread" []
          :return       [Threadd]
          :summary      "All threads"
          (ok  (db/get-threads)))