oliyh / martian

The HTTP abstraction library for Clojure/script, supporting OpenAPI, Swagger, Schema, re-frame and more
MIT License
525 stars 42 forks source link

Can you use a spec file directly without a url? #160

Closed michaelwhitford closed 1 year ago

michaelwhitford commented 1 year ago

I am writing manual openapi yaml spec files for some subset of a rest api. I have it working well, but would like to keep the yaml spec file in the git repository, rather than have to serve it from a web server. Is there a syntax to hand a file to the bootstrap process, rather than a url? I looked in the documentation but didn't see anything. It seems all the examples use a url to the spec file.

oliyh commented 1 year ago

Hi,

Yes, you can. The bootstrap-swagger function requests the schema over http, then parses it to turn it into the internal martian data structure.

You can see this in the implementation of these functions, e.g. https://github.com/oliyh/martian/blob/master/cljs-http/src/martian/cljs_http.cljs#L31-L37

You could write yours as follows:

(defn bootstrap-from-yaml [url opts]
  (let [definition (read-yaml-file)]
        (martian.core/bootstrap-openapi url definition (merge default-opts opts))))

Note that default-opts here contains the interceptors for a specific http library e.g. cljs-http.

Hope this helps?

michaelwhitford commented 1 year ago

Ok I think I almost have it working. It looks like this currently because I am in a scratch project just to play with martian.

(def m (martian/bootstrap-openapi "http://localhost:7000"
    (slurp "openapi_minimum.yaml")
    {:interceptors my-interceptors}))

I can see the interceptors look correct on the m object, but when I try to (martian/explore m) it just returns an empty vector.

This works fine, with the same exact file, just served via http:

(def m (martian-http/bootstrap-openapi "http://localhost:8080/openapi_minimum.yaml"
   {:interceptors my-interceptors}))
oliyh commented 1 year ago

You will need to convert the yaml into something martian understands, use martian.yaml/yaml->edn https://github.com/oliyh/martian/blob/master/core/src/martian/yaml.clj#L34

michaelwhitford commented 1 year ago

Yeah I just saw that a few minutes ago and got a lightbulb moment! Thank you so much for a wonderful library, it's working perfectly.

michaelwhitford commented 1 year ago

Using the following code works instead of a url for bootstrapping:

(def m (martian.core/bootsrap-openapi "http://localhost:7000"
                   (martian.yaml/yaml->edn (slurp "/path/to/file.yaml"))
                   {:interceptors my-custom-interceptors}))