Skip to content

Instantly share code, notes, and snippets.

@ikitommi
Last active August 29, 2015 13:56
Show Gist options
  • Save ikitommi/9325802 to your computer and use it in GitHub Desktop.
Save ikitommi/9325802 to your computer and use it in GitHub Desktop.
Models with ring-swagger (https://github.com/metosin/ring-swagger)
;; play with the latest
lein try metosin/ring-swagger
; nREPL server started on port 63138 on host 127.0.0.1
; REPL-y 0.3.0
; Clojure 1.5.1
; Docs: (doc function-name-here)
; (find-doc "part-of-name-here")
; Source: (source function-name-here)
; Javadoc: (javadoc java-object-or-class-here)
; Exit: Control+D or (exit) or (quit)
; Results: Stored in vars *1, *2, *3, an exception in *e
;;
;; create models
;;
(require '[ring.swagger.schema :refer :all])
(require '[schema.core :as s])
(defmodel Country {:code (s/enum :fi :sv)
:name String
:description (s/maybe String)})
(defmodel Customer {:id Long
:name String
(s/optional-key :address) {:street String
:country Country}})
Country
; => {:code (enum :fi :sv), :name java.lang.String, :description (maybe java.lang.String)}
Customer
; => {:id java.lang.Long, :name java.lang.String, #schema.core.OptionalKey{:k :address} {:street java.lang.String, :country {:code (enum :fi :sv), :name java.lang.String, :description (maybe java.lang.String)}}}
; nested models get generated automatically
CustomerAddress
; => {:street java.lang.String, :country {:code (enum :fi :sv), :name java.lang.String, :description (maybe java.lang.String)}}
; new customers don't have ids
(defmodel NewCustomer (dissoc Customer :id))
NewCustomer
; => {:name java.lang.String, #schema.core.OptionalKey{:k :address} {:street java.lang.String, :country {:code (enum :fi :sv), :name java.lang.String, :description (maybe java.lang.String)}}}
;;
;; values
;;
(def finland {:code "fi" :name "Finland" :description nil})
; => #'user/finland
;;
;; model coercion
;;
(coerce Country finland)
; => {:description nil, :name "Finland", :code :fi}
(coerce NewCustomer {:name "Pertti"})
; => {:name "Pertti"}
(coerce Customer {:name "Pertti"})
; => #schema.utils.ErrorContainer{:error {:id missing-required-key}}
(coerce Customer {:id 123 :name "Pertti" :address {:country finland}})
; => #schema.utils.ErrorContainer{:error {:address {:street missing-required-key}}}
(coerce Customer {:id 123 :name "Pertti" :address {:street "Latokuja" :country finland}})
; => {:address {:country {:description nil, :name "Finland", :code :fi}, :street "Latokuja"}, :name "Pertti", :id 123}
; sub-models can be coarced seperately
(coerce! CustomerAddress {:street "Latokuja" :country finland})
; => {:country {:description nil, :name "Finland", :code :fi}, :street "Latokuja"}
;;
;; Generating json-schemas (for swagger)
;;
(require '[ring.swagger.core :as swagger])
(pprint (swagger/transform-models Customer))
; {:Country
; {:id "Country",
; :properties
; {:code {:type "string", :enum (:fi :sv)},
; :name {:type "string"},
; :description {:type "string"}},
; :required (:code :name :description)},
; :CustomerAddress
; {:id "CustomerAddress",
; :properties {:street {:type "string"}, :country {:$ref "Country"}},
; :required (:street :country)},
; :Customer
; {:id "Customer",
; :properties
; {:id {:format "int64", :type "integer"},
; :name {:type "string"},
; :address {:$ref "CustomerAddress"}},
; :required (:id :name)}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment