From 41b04ac73c56545c3877c0b6d611354481c132cb Mon Sep 17 00:00:00 2001 From: Andy Lu Date: Sun, 3 May 2020 23:26:19 -0500 Subject: [PATCH 1/4] Add support for anyOf by removing it before we process the property --- .../src/tap_generate_docs.clj | 33 +++++++++++++++---- .../test/tap_generate_docs_test.clj | 20 +++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/scripts/tap-generate-docs/src/tap_generate_docs.clj b/scripts/tap-generate-docs/src/tap_generate_docs.clj index 4dad86520..8db2c4e23 100644 --- a/scripts/tap-generate-docs/src/tap_generate_docs.clj +++ b/scripts/tap-generate-docs/src/tap_generate_docs.clj @@ -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 @@ -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" 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..3c3528e9f 100644 --- a/scripts/tap-generate-docs/test/tap_generate_docs_test.clj +++ b/scripts/tap-generate-docs/test/tap_generate_docs_test.clj @@ -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 From 5d630216c03a25d736f7d0b02cf4d7ae07a4b7c6 Mon Sep 17 00:00:00 2001 From: Andy Lu Date: Mon, 4 May 2020 08:07:13 -0500 Subject: [PATCH 2/4] Add code to help with debugging --- scripts/tap-generate-docs/src/tap_generate_docs.clj | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/scripts/tap-generate-docs/src/tap_generate_docs.clj b/scripts/tap-generate-docs/src/tap_generate_docs.clj index 8db2c4e23..d6b8ee75f 100644 --- a/scripts/tap-generate-docs/src/tap_generate_docs.clj +++ b/scripts/tap-generate-docs/src/tap_generate_docs.clj @@ -482,6 +482,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") From 256d9f9bf0a6ebce7c8810aae21fbe2204199da2 Mon Sep 17 00:00:00 2001 From: Andy Lu Date: Mon, 4 May 2020 08:18:44 -0500 Subject: [PATCH 3/4] Add error message for arrays without an items field --- .../src/tap_generate_docs.clj | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/scripts/tap-generate-docs/src/tap_generate_docs.clj b/scripts/tap-generate-docs/src/tap_generate_docs.clj index d6b8ee75f..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")) From e08bd22ff1d10d89786ffa8fd5cd20cf4299062e Mon Sep 17 00:00:00 2001 From: Andy Lu Date: Mon, 4 May 2020 08:23:03 -0500 Subject: [PATCH 4/4] Add test with anyOf in schema for convert-multiary-type-tests --- scripts/tap-generate-docs/test/tap_generate_docs_test.clj | 7 +++++++ 1 file changed, 7 insertions(+) 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 3c3528e9f..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"