Skip to content

Instantly share code, notes, and snippets.

@miikka
Last active August 16, 2016 10:57
Show Gist options
  • Save miikka/77e2b89420e1626b606c308ec232b3f3 to your computer and use it in GitHub Desktop.
Save miikka/77e2b89420e1626b606c308ec232b3f3 to your computer and use it in GitHub Desktop.
clojure.spec version of ring-swagger's README example
(ns example
(:require [clojure.spec :as spec]
[ring.swagger.swagger2 :as rs]
[schema.core :as s]))
(spec/def ::id string?)
(spec/def ::name string?)
(spec/def ::street string?)
(spec/def ::city #{"tre" "hki"})
(spec/def ::address (spec/keys :req [::street ::city]))
(spec/def ::user
(spec/keys :req [::id ::name ::address]))
(defn get-def []
(rs/swagger-json
{:info {:version "1.0.0"
:title "Sausages"
:description "Sausage description"
:termsOfService "http://helloreverb.com/terms/"
:contact {:name "My API Team"
:email "[email protected]"
:url "http://www.metosin.fi"}
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}}
:tags [{:name "user"
:description "User stuff"}]
:paths {"/api/ping" {:get {}}
"/user/:id" {:post {:summary "User Api"
:description "User Api description"
:tags ["user"]
:parameters {:path {:id ::id}
:body ::user}
:responses {200 {:schema ::user
:description "Found it!"}
404 {:description "Ohnoes."}}}}}}))
{:swagger "2.0",
:info
{:title "Sausages",
:version "1.0.0",
:description "Sausage description",
:termsOfService "http://helloreverb.com/terms/",
:contact
{:name "My API Team",
:email "[email protected]",
:url "http://www.metosin.fi"},
:license
{:name "Eclipse Public License",
:url "http://www.eclipse.org/legal/epl-v10.html"}},
:produces ["application/json"],
:consumes ["application/json"],
:tags [{:name "user", :description "User stuff"}],
:paths
{"/api/ping" {:get {:responses {:default {:description ""}}}},
"/user/{id}"
{:post
{:summary "User Api",
:description "User Api description",
:tags ["user"],
:parameters
[{:in "path",
:name "id",
:description "",
:required true,
:type "string"}],
:responses
{200
{:schema
{:type "object",
:properties
{"id" {:type "string"},
"name" {:type "string"},
"address"
{:type "object",
:properties
{"street" {:type "string"}, "city" {:enum ["hki" "tre"]}},
:required ("street" "city"),
:additionalProperties false}},
:required ("id" "name" "address"),
:additionalProperties false},
:description "Found it!"},
404 {:description "Ohnoes."}}}}},
:definitions {}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment