Repository: oliyh/martian
Pull request #28 (2016-11-30 17:56:40)

Filter methods without "operationId" from Martian instance.
Martian can now be used when not all methods have an operationId, allowing
a more gradual introduction of Martian.


Patch:
diff --git a/core/src/martian/swagger.cljc b/core/src/martian/swagger.cljc
index 360bb2d..d295dd7 100644
--- a/core/src/martian/swagger.cljc
+++ b/core/src/martian/swagger.cljc
@@ -45,33 +45,35 @@
             %) parts)))
 
 (defn- ->handler [{:keys [definitions] :as swagger-map} url-pattern [method swagger-definition]]
-  (let [path-parts (tokenise-path url-pattern)
-        uri (string/join (map str path-parts))
-        parameters (:parameters swagger-definition)]
-    {:path uri
-     :path-parts path-parts
-     :method method
-     :path-schema (path-schema definitions parameters)
-     :query-schema (query-schema definitions parameters)
-     :body-schema (body-schema definitions parameters)
-     :form-schema (form-schema definitions parameters)
-     :headers-schema (headers-schema definitions parameters)
-     :response-schemas (response-schemas definitions (:responses swagger-definition))
-     :produces (some :produces [swagger-definition swagger-map])
-     :consumes (some :consumes [swagger-definition swagger-map])
-     :swagger-definition swagger-definition
-     ;; todo path constraints - required?
-     ;; :path-constraints {:id "(\\d+)"},
-     ;; {:in "path", :name "id", :description "", :required true, :type "string", :format "uuid"
-     :route-name (->kebab-case-keyword (:operationId swagger-definition))}))
+  (when-let [route-name (some-> (:operationId swagger-definition) ->kebab-case-keyword)]
+    (let [path-parts (tokenise-path url-pattern)
+          uri (string/join (map str path-parts))
+          parameters (:parameters swagger-definition)]
+      {:path uri
+       :path-parts path-parts
+       :method method
+       :path-schema (path-schema definitions parameters)
+       :query-schema (query-schema definitions parameters)
+       :body-schema (body-schema definitions parameters)
+       :form-schema (form-schema definitions parameters)
+       :headers-schema (headers-schema definitions parameters)
+       :response-schemas (response-schemas definitions (:responses swagger-definition))
+       :produces (some :produces [swagger-definition swagger-map])
+       :consumes (some :consumes [swagger-definition swagger-map])
+       :swagger-definition swagger-definition
+       ;; todo path constraints - required?
+       ;; :path-constraints {:id "(\\d+)"},
+       ;; {:in "path", :name "id", :description "", :required true, :type "string", :format "uuid"
+       :route-name route-name})))
 
 (defn swagger->handlers [swagger-json]
   (let [swagger-spec (keywordize-keys swagger-json)]
     (reduce-kv
      (fn [handlers url-pattern swagger-handlers]
-       (into handlers (map (partial ->handler
-                                    swagger-spec
-                                    url-pattern)
-                           swagger-handlers)))
+       (into handlers (remove nil?
+                        (map (partial ->handler
+                                      swagger-spec
+                                      url-pattern)
+                             swagger-handlers))))
      []
      (:paths swagger-spec))))


Test patch:
diff --git a/core/test/martian/core_test.cljc b/core/test/martian/core_test.cljc
index 61930c6..e6ab3c3 100644
--- a/core/test/martian/core_test.cljc
+++ b/core/test/martian/core_test.cljc
@@ -53,7 +53,10 @@
                                                                                 :in "body"
                                                                                 :required true
                                                                                 :schema {:type "array"
-                                                                                         :items {:$ref "#/definitions/User"}}}]}}}
+                                                                                         :items {:$ref "#/definitions/User"}}}]}
+                                                           ;; operationId is intentionally missing from the get method
+                                                           :get {}}}
+
    :definitions {:Pet {:type "object"
                        :properties {:id {:type "integer"
                                          :required true}