Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve :default swagger #47

Merged
merged 21 commits into from
Jul 12, 2024
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches:
- master
pull_request:
branches:
- master
schedule:
# 12:00AM on the first of every month
- cron: "0 0 1 * *"
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.1-SNAPSHOT

- populate `:default` for all schemas and use it for Swagger's `:example` field

# 1.0.0 - 19th December 2023

- add support for malli via `flanders.malli/->malli`
Expand Down
6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
[org.clojure/core.match "1.0.0"]
[cheshire "5.9.0"]

[prismatic/schema "1.1.12"]
[metosin/ring-swagger "0.26.2"]
[metosin/schema-tools "0.12.2"]]
[prismatic/schema "1.2.0"]
[metosin/ring-swagger "1.0.0"]
[metosin/schema-tools "0.12.3"]]
:global-vars {*warn-on-reflection* true}
:release-tasks [["clean"]
["vcs" "assert-committed"]
Expand Down
33 changes: 18 additions & 15 deletions src/flanders/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,38 @@
(defn anything [& {:as opts}]
(ft/map->AnythingType opts))

(defn bool [& {:keys [equals] :as opts}]
(defn bool [& {:keys [equals default] :as opts}]
(ft/map->BooleanType
(merge opts
{:open? (nil? equals)
:default (when (some? equals) equals)})))
:default (if (some? equals)
equals
(when (some? default)
default))})))

(defn inst [& {:as opts}]
(ft/map->InstType opts))

(defn int [& {:keys [equals] :as opts}]
(defn int [& {:keys [equals default] :as opts}]
(ft/map->IntegerType
(merge opts
{:open? (not equals)
:values (when equals #{equals})
:default (when equals equals)})))
:default (or equals default)})))

(defn num [& {:keys [equals] :as opts}]
(defn num [& {:keys [equals default] :as opts}]
(ft/map->NumberType
(merge opts
{:open? (not equals)
:values (when equals #{equals})
:default (when equals equals)})))
:default (or equals default)})))

(defn keyword [& {:keys [equals] :as opts}]
(defn keyword [& {:keys [equals default] :as opts}]
(ft/map->KeywordType
(merge opts
{:open? (not equals)
:values (when equals #{equals})
:default (when equals equals)})))
:default (or equals default)})))

(defn key [equals & {:as opts}]
(ft/map->KeywordType
Expand All @@ -166,22 +169,22 @@
:values #{equals}
:default equals})))

(defn str [& {:keys [equals] :as opts}]
(defn str [& {:keys [equals default] :as opts}]
(ft/map->StringType
(merge opts
{:open? (not equals)
:values (when equals #{equals})
:default (when equals equals)})))
:default (or equals default)})))

(defn enum [values & {:keys [open?]
:or {open? false}
:as opts}]
(let [v (first values)]
(let [v (-> values sort first)]
(cond
(integer? v) (ft/map->IntegerType (merge opts {:values values :open? open?}))
(number? v) (ft/map->NumberType (merge opts {:values values :open? open?}))
(keyword? v) (ft/map->KeywordType (merge opts {:values values :open? open?}))
(string? v) (ft/map->StringType (merge opts {:values values :open? open?})))))
(integer? v) (ft/map->IntegerType (merge {:default v} opts {:values values :open? open?}))
(number? v) (ft/map->NumberType (merge {:default v} opts {:values values :open? open?}))
(keyword? v) (ft/map->KeywordType (merge {:default v} opts {:values values :open? open?}))
(string? v) (ft/map->StringType (merge {:default v} opts {:values values :open? open?})))))

(defn eq [value & {:keys [description reference comment usage name]}]
(enum #{value}
Expand Down
72 changes: 37 additions & 35 deletions src/flanders/example.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
:as ft
:refer [AnythingType BooleanType EitherType InstType IntegerType
KeywordType MapEntry MapType NumberType SequenceOfType
SetOfType SignatureType StringType]])
[flanders.schema :as fs])
SetOfType SignatureType StringType]]))
#?(:clj (:import
[flanders.types
AnythingType
Expand All @@ -32,63 +31,66 @@
;; Branches

EitherType
(->example [{:keys [choices]} f]
(f (first choices)))
(->example [{:keys [choices] :as ddl} f]
(let [[_ example :as example?] (find ddl :default)]
(if example?
example
(f (first choices)))))
frenchy64 marked this conversation as resolved.
Show resolved Hide resolved

MapEntry
(->example [{:keys [key type]} f]
[(f (assoc key
:key? true))
(f type)])
(->example [{:keys [key type] :as ddl} f]
(let [[_ example :as example?] (find ddl :default)]
[(f (assoc key :key? true))
(f (cond-> type
example? (assoc :default example)))]))

MapType
(->example [{:keys [entries]} f]
(reduce (fn [m [k v]]
(assoc m k v))
{}
(map f entries)))
(->example [{:keys [entries default]} f]
(or default
(reduce (fn [m [k v]]
(assoc m k v))
{}
(map f entries))))

SequenceOfType
(->example [{:keys [type]} f]
[(f type)])
(->example [{:keys [type default]} f]
(or default [(f type)]))

SetOfType
(->example [{:keys [type]} f]
#{(f type)})
(->example [{:keys [type default]} f]
(or default #{(f type)}))

;; Leaves

AnythingType
(->example [_ _]
{:anything "anything"})
(->example [{:keys [default]} _]
(or default "anything"))
frenchy64 marked this conversation as resolved.
Show resolved Hide resolved

BooleanType
(->example [_ _]
true)
(->example [{:keys [default] :or {default true}} _]
default)

InstType
(->example [_ _]
#?(:clj (Date. 1451610061000)
:cljs (js/date. 1451610061000)))
(->example [{:keys [default]} _]
(or default
#?(:clj (Date. 1451610061000)
:cljs (js/date. 1451610061000))))

IntegerType
(->example [_ _]
10)
(->example [{:keys [default]} _]
(or default 10))

KeywordType
(->example [node _]
(let [schema (fs/->schema node)]
(if (keyword? schema)
(name schema)
"keyword")))
(->example [{:keys [default]} _]
(or default :keyword))

NumberType
(->example [_ _]
10.0)
(->example [{:keys [default]} _]
(or default 10.0))

StringType
(->example [_ _]
"string")
(->example [{:keys [default]} _]
(or default "string"))

SignatureType
(->example [{:keys [parameters rest-parameter return]} f]
Expand Down
Loading
Loading