diff --git a/resources/schemas/adl-1.4.2.dtd b/resources/schemas/adl-1.4.2.dtd new file mode 100644 index 0000000..d2f63d8 --- /dev/null +++ b/resources/schemas/adl-1.4.2.dtd @@ -0,0 +1,628 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/schemas/adl-1.4.2.xsd b/resources/schemas/adl-1.4.2.xsd new file mode 100644 index 0000000..225477a --- /dev/null +++ b/resources/schemas/adl-1.4.2.xsd @@ -0,0 +1,559 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/adl/to_swagger.clj b/src/adl/to_swagger.clj index 3ce11ca..521b385 100644 --- a/src/adl/to_swagger.clj +++ b/src/adl/to_swagger.clj @@ -2,7 +2,7 @@ :author "Simon Brooke"} adl.to-swagger (:require [adl-support.utils :refer :all] - [adl.to-hugsql-queries :refer [queries]] + [adl.to-hugsql-queries :refer [generate-documentation queries]] [clj-time.core :as t] [clj-time.format :as f] [clojure.java.io :refer [file make-parents writer]] @@ -43,21 +43,98 @@ (list 'ns (symbol (str (safe-name (:name (:attrs application))) ".routes.auto-api")) - (str "API routes for " (:name (:attrs application)) + (str "Swagger routes for " (:name (:attrs application)) " auto-generated by [Application Description Language framework](https://github.com/simon-brooke/adl) at " (f/unparse (f/formatters :basic-date-time) (t/now))) (list :require - '[adl-support.core :as support] - '[clj-http.client :as client] - '[clojure.tools.logging :as log] - '[compojure.api.sweet :refer :all] - '[hugsql.core :as hugsql] + '[reitit.swagger :as swagger] + '[reitit.swagger-ui :as swagger-ui] + '[reitit.ring.coercion :as coercion] + '[reitit.coercion.spec :as spec-coercion] + '[reitit.ring.middleware.muuntaja :as muuntaja] + '[reitit.ring.middleware.multipart :as multipart] + '[reitit.ring.middleware.parameters :as parameters] + '[placenames.middleware.formats :as formats] + '[placenames.middleware.exception :as exception] + '[placenames.routes.auto-jason :as aj] '[ring.util.http-response :refer :all] - '[noir.response :as nresponse] - '[noir.util.route :as route] - '[ring.util.http-response :as response] - '[schema.core :as s] - (vector (symbol (str (safe-name (:name (:attrs application))) ".db.core")) :as 'db)))) + '[clojure.java.io :as io]))) + +(defn def-routes + "Generate Swagger routes for all queries implied by this ADL `application` spec." + ;; THIS ISN'T NEARLY FINISHED! + ([application] + (list 'defn 'auto-api-routes [] + ["/api" + {:coercion spec-coercion/coercion + :muuntaja formats/instance + :swagger {:id ::api} + :middleware [;; query-params & form-params + parameters/parameters-middleware + ;; content-negotiation + muuntaja/format-negotiate-middleware + ;; encoding response body + muuntaja/format-response-middleware + ;; exception handling + exception/exception-middleware + ;; decoding request body + muuntaja/format-request-middleware + ;; coercing response bodys + coercion/coerce-response-middleware + ;; coercing request parameters + coercion/coerce-request-middleware + ;; multipart + multipart/multipart-middleware]}] + (map #(def-routes application %) + (children-with-tag application :entity))) + ([application entity] + [(str "/" (safe-name entity)) + {:get (make-get-route entity) + (cons + 'defroutes + (cons + 'auto-rest-routes + (map + #(let [handler (handlers-map %)] + (list + (symbol (s/upper-case (name (:method handler)))) + (str "/json/auto/" (safe-name (:name handler))) + 'request + (list + 'route/restricted + (list (:name handler) 'request)))) + (sort + (keys handlers-map)))))}]))) + + +(defn to-swagger + "Generate a Swagger API for all queries implied by this ADL `application` spec." + [application] + (let [filepath (str + *output-path* + "src/" + (safe-name (:name (:attrs application))) + "/routes/auto_api.clj")] + (make-parents filepath) + (do-or-warn + (do + (spit + filepath + (s/join + "\n\n" + (cons + (file-header application) + (map + (fn [q] + (str + ;; THIS ISN'T NEARLY FINISHED! + )) + (sort + #(compare (:name %1) (:name %2)) + (vals + (queries application))))))) + (if (pos? *verbosity*) + (*warn* (str "\tGenerated " filepath)))))))