Skip to content

Commit

Permalink
Add support for anyOf by removing it before we process the property
Browse files Browse the repository at this point in the history
  • Loading branch information
luandy64 committed May 4, 2020
1 parent cb6bbe1 commit 41b04ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
33 changes: 27 additions & 6 deletions scripts/tap-generate-docs/src/tap_generate_docs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,31 @@
[{:keys [tap-schema-dir]} file]
(read-schema (io/file tap-schema-dir file)))

(defn remove-anyOf [[property-name property-json-schema-partial]]
[property-name (apply merge-with
(fn [x y]
(let [x (if (vector? x)
x
[x])
y (if (vector? y)
y
[y])]
(vec (dedupe (into x y)))))
(property-json-schema-partial "anyOf"))])

(defn maybe-remove-anyOf [property]
(if (get-in property [1 "anyOf"])
(remove-anyOf property)
property))

(defn property->converted-unary-types [tap-fs schema property]
(->> (maybe-remove-anyOf property)
property->unary-type-properties
(map (partial convert-unary-type
tap-fs
schema))
dedupe))

(defn convert-multiary-type
"multiary-type = a property that _may_ have more than one type."
[tap-fs schema [property-name property-json-schema-partial
Expand Down Expand Up @@ -237,12 +262,8 @@
:schema schema})))
[property-name referenced-json-schema-partial])
property)]
(let [unary-type-properties
(property->unary-type-properties property)

converted-unary-type-properties
(map (partial convert-unary-type tap-fs schema)
unary-type-properties)]
(let [converted-unary-type-properties
(property->converted-unary-types tap-fs schema property)]
(if (empty? converted-unary-type-properties)
(do
(println (str "Null unary type passed for property"
Expand Down
20 changes: 20 additions & 0 deletions scripts/tap-generate-docs/test/tap_generate_docs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,26 @@
{"definitions" {"integer" {"type" "integer"}}}
["an_string" {"$ref" "#/definitions/string"}]))))

(deftest maybe-remove-anyOf-test
(are [x y] (= y
(maybe-remove-anyOf x))
["field1" {"anyOf" [{"type" "string"}
{"type" "integer"}]}]
["field1" {"type" ["string" "integer"]}]

["field2" {"anyOf" [{"type" ["null" "string"]}
{"type" "integer"}]}]
["field2" {"type" ["null" "string" "integer"]}]

["field3" {"anyOf" [{"type" ["null" "string"]
"format" "date-time"}
{"type" "string"}]}]
["field3" {"type" ["null" "string"]
"format" "date-time"}]

["field4" {"type" ["null" "string"]}]
["field4" {"type" ["null" "string"]}]))

(deftest convert-types-with-bad-refs-tests
(is (thrown? clojure.lang.ExceptionInfo
(convert-multiary-type nil
Expand Down

0 comments on commit 41b04ac

Please sign in to comment.