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

Ability to be used as a general parser #190

Open lispyclouds opened 8 months ago

lispyclouds commented 8 months ago

Hello!

Thanks a lot for making and maintaining this great tool, has helped me out quite a lot!

I have a few use cases wherein I have a need to just parse OpenAPI/Swagger docs into data for me to walk that tree and infer/transform it:

I use the quite heavy and clunky to use standard java parser for OpenAPI in these and was wondering if martian could be used just for this:

I tried playing with it and reading the docs but wasn't sure if this could be done? Apologies if this already works and I didn't see it!

Thanks again!

oliyh commented 8 months ago

Hi,

Thanks for your kind words! You could use this function https://github.com/oliyh/martian/blob/b96a1def9e25284ec99ca68855218fc959ee5418/core/src/martian/openapi.cljc#L163

It will return a normalised list of handlers with the parameters described using plumatic schema. The shape is defined by the spec here: https://github.com/oliyh/martian/blob/b96a1def9e25284ec99ca68855218fc959ee5418/core/src/martian/spec.cljc#L23

I thought Malli had a json schema / openapi parser but it seems maybe not.

lispyclouds commented 8 months ago

Thanks a lot!

I tried parsing this spec with this code with my limited understanding:

(-> "api.yaml"
    (slurp)
    (clj-yaml.core/parse-string)
    (martian.openapi/openapi->handlers {}))

But I still see the refs in the content types etc. Am I doing this right?

oliyh commented 5 months ago

Hi,

Apologies for the delayed reply.

Martian does not have the ability to parse remote refs. It would not be impossible to add it as an additional resolver function here, but that would make it less pure. Alternatively the schema could be pre-walked to resolved remote refs first, and then this function would remain pure.