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

Tap generate docs: anyOf support and improved error messages #476

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 55 additions & 23 deletions scripts/tap-generate-docs/src/tap_generate_docs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,25 @@
"subattributes"
(convert-object-properties tap-fs schema (property-json-schema-partial "properties"))))
(= "array" (property-json-schema-partial "type"))
(let [items (property-json-schema-partial "items")
item-type (items "type")
converted-property (do
(when ((set item-type) "array")
(throw (ex-info (str "Currently cannot handle a type with arrays of arrays. "
"Discuss how docs output should work if encountered.")
{:property property})))
(if (or (= "object" item-type)
((set item-type) "object"))
(convert-array-object-type tap-fs schema property items)
[(convert-multiary-type tap-fs schema ["value" items])]))]
;; TODO Log (or verify that it's already logged) dropped value
(if (empty? converted-property)
base-converted-property
(assoc base-converted-property
"subattributes"
converted-property)))
(if (nil? (property-json-schema-partial "items"))
(throw (ex-info "Found array type without items defined: " {:property-name property-name}))
(let [items (property-json-schema-partial "items")
item-type (items "type")
converted-property (do
(when ((set item-type) "array")
(throw (ex-info (str "Currently cannot handle a type with arrays of arrays. "
"Discuss how docs output should work if encountered.")
{:property property})))
(if (or (= "object" item-type)
((set item-type) "object"))
(convert-array-object-type tap-fs schema property items)
[(convert-multiary-type tap-fs schema ["value" items])]))]
;; TODO Log (or verify that it's already logged) dropped value
(if (empty? converted-property)
base-converted-property
(assoc base-converted-property
"subattributes"
converted-property))))

(and (= "string" (property-json-schema-partial "type"))
(contains? property-json-schema-partial "enum"))
Expand Down Expand Up @@ -205,6 +207,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 +264,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 Expand Up @@ -461,6 +484,15 @@
(when-not *interactive*
(System/exit 0))))))

(comment
;; How to run with just one file
(map (partial convert-multiary-type nil nil)
(-> "/Users/andylu/git/tap-closeio/tap_closeio/schemas/activities.json"
slurp
json/read-str
(get "properties")))
)

(comment
(def ^:dynamic *interactive* true)
(-main "bigcommerce")
Expand Down
27 changes: 27 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 @@ -246,6 +246,13 @@
"type" "date-time, integer"
"description" ""}

["a_date" {"anyOf" [{"type" ["null" "string"]
"format" "date-time"}
{"type" "integer"}]}]
{"name" "a_date"
"type" "date-time, integer"
"description" ""}

["a_date" {"type" "string"
"format" "date-time"}]
{"name" "a_date"
Expand Down Expand Up @@ -318,6 +325,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