Skip to content

Commit

Permalink
Fix JSON Schema default if the default value is nil
Browse files Browse the repository at this point in the history
JSON Schema should not contain default key if the default value is null.

Fixes #79
  • Loading branch information
Deraen committed Jan 13, 2016
1 parent b79caa0 commit 591610c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ring/swagger/json_schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
[m x {no-meta ::no-meta key-meta :key-meta}]
(if-not no-meta
(merge (json-schema-meta x)
(if key-meta (select-keys key-meta [:default]))
(c/remove-empty-keys (if key-meta (select-keys key-meta [:default])))
m)
m))

Expand Down
16 changes: 15 additions & 1 deletion test/ring/swagger/json_schema_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns ring.swagger.json-schema-test
(:require [midje.sweet :refer :all]
[schema.core :as s]
[plumbing.core :as p]
[plumbing.fnk.pfnk :as pfnk]
[ring.swagger.json-schema :refer :all]
[ring.swagger.core :refer [with-named-sub-schemas]]
[linked.core :as linked])
Expand Down Expand Up @@ -119,7 +121,19 @@

(fact "Optional-key default metadata"
(properties {(with-meta (s/optional-key :foo) {:default "bar"}) s/Str})
=> {:foo {:type "string" :default "bar"}})
=> {:foo {:type "string" :default "bar"}}

(fact "nil default is ignored"
(properties {(with-meta (s/optional-key :foo) {:default nil}) s/Str})
=> {:foo {:type "string"}})

(fact "pfnk schema"
(properties (pfnk/input-schema (p/fnk [{x :- s/Str "foo"}])))
=> {:x {:type "string" :default "foo"}})

(fact "pfnk schema - nil default is ignored"
(properties (pfnk/input-schema (p/fnk [{x :- s/Str nil}])))
=> {:x {:type "string"}}))

(fact "Describe"
(tabular
Expand Down

0 comments on commit 591610c

Please sign in to comment.