diff --git a/scripts/tap-generate-docs/src/tap_generate_docs.clj b/scripts/tap-generate-docs/src/tap_generate_docs.clj index 4dad86520..7e1aedb66 100644 --- a/scripts/tap-generate-docs/src/tap_generate_docs.clj +++ b/scripts/tap-generate-docs/src/tap_generate_docs.clj @@ -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")) @@ -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 @@ -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" @@ -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") diff --git a/scripts/tap-generate-docs/test/tap_generate_docs_test.clj b/scripts/tap-generate-docs/test/tap_generate_docs_test.clj index e1d72589b..221b7ca41 100644 --- a/scripts/tap-generate-docs/test/tap_generate_docs_test.clj +++ b/scripts/tap-generate-docs/test/tap_generate_docs_test.clj @@ -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" @@ -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