Giter Site home page Giter Site logo

Comments (13)

oliyh avatar oliyh commented on May 20, 2024

Hello,

Thanks for reporting this. Could you attach the edn and yaml files please?

Thanks

from martian.

bombaywalla avatar bombaywalla commented on May 20, 2024

Had to change the extensions of the files to .txt to get github to allow the upload.
The internal oas-edn was created by (yaml/parse-string (slurp "sponsored-products-yaml.txt")).
The edn file was created by (spit "sponsored-products-edn.txt" oas-edn).

sponsored-products-yaml.txt
sponsored-products-edn.txt

from martian.

bombaywalla avatar bombaywalla commented on May 20, 2024

On taking a closer look, it seems that the EDN that is generated from the YAML may be faulty.
For example, the following YAML snippet

  /v2/sp/campaigns:
    post:
      tags:
        - Campaigns
      operationId: createCampaigns
      summary: Creates one or more campaigns.
      parameters:
        - $ref: '#/components/parameters/clientHeader'
        - $ref: '#/components/parameters/profileHeader'
      requestBody:
        description: An array of campaigns.
        content:
          application/json:
            schema:
              type: array
              minItems: 0
              maxItems: 100
              items:
                $ref: '#/components/schemas/CreateCampaign'
      responses:
        207:
          description: Success.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/CampaignResponse'
        401:
          $ref: '#/components/responses/Unauthorized'
    put:
      tags:
        - Campaigns
      operationId: updateCampaigns
      summary: Updates one or more campaigns.
      parameters:
        - $ref: '#/components/parameters/clientHeader'
        - $ref: '#/components/parameters/profileHeader'
      requestBody:
        description: An array of campaigns with updated values.
        content:
          application/json:
            schema:
              type: array
              minItems: 0
              maxItems: 100
              items:
                $ref: '#/components/schemas/UpdateCampaign'
      responses:
        207:
          description: Multi-status. An array of campaign response objects reflecting the same order as the request.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/CampaignResponse'
        401:
          $ref: '#/components/responses/Unauthorized'
    get:
      tags:
        - Campaigns
      operationId: listCampaigns
      summary: Gets an array of campaigns.
      parameters:
        - $ref: '#/components/parameters/clientHeader'
        - $ref: '#/components/parameters/profileHeader'
        - $ref: '#/components/parameters/startIndex'
        - $ref: '#/components/parameters/count'
        - $ref: '#/components/parameters/stateFilter'
        - $ref: '#/components/parameters/name'
        - $ref: '#/components/parameters/portfolioIdFilter'
        - $ref: '#/components/parameters/campaignIdFilter'
      responses:
        200:
          description: Success.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Campaign'
        401:
          $ref: '#/components/responses/Unauthorized'
        404:
          $ref: '#/components/responses/NotFound'

results in the following EDN snippet

[:/v2/sp/campaigns
 {:post
  {:tags ["Campaigns"],
   :operationId "createCampaigns",
   :summary "Creates one or more campaigns.",
   :parameters
   [{:$ref "#/components/parameters/clientHeader"}
    {:$ref "#/components/parameters/profileHeader"}],
   :requestBody
   {:description "An array of campaigns.",
    :content
    #:application{:json
                  {:schema
                   {:type "array",
                    :minItems 0,
                    :maxItems 100,
                    :items
                    {:$ref
                     "#/components/schemas/CreateCampaign"}}}}},
   :responses {nil {:$ref "#/components/responses/Unauthorized"}}},
  :put
  {:tags ["Campaigns"],
   :operationId "updateCampaigns",
   :summary "Updates one or more campaigns.",
   :parameters
   [{:$ref "#/components/parameters/clientHeader"}
    {:$ref "#/components/parameters/profileHeader"}],
   :requestBody
   {:description "An array of campaigns with updated values.",
    :content
    #:application{:json
                  {:schema
                   {:type "array",
                    :minItems 0,
                    :maxItems 100,
                    :items
                    {:$ref
                     "#/components/schemas/UpdateCampaign"}}}}},
   :responses {nil {:$ref "#/components/responses/Unauthorized"}}},
  :get
  {:tags ["Campaigns"],
   :operationId "listCampaigns",
   :summary "Gets an array of campaigns.",
   :parameters
   [{:$ref "#/components/parameters/clientHeader"}
    {:$ref "#/components/parameters/profileHeader"}
    {:$ref "#/components/parameters/startIndex"}
    {:$ref "#/components/parameters/count"}
    {:$ref "#/components/parameters/stateFilter"}
    {:$ref "#/components/parameters/name"}
    {:$ref "#/components/parameters/portfolioIdFilter"}
    {:$ref "#/components/parameters/campaignIdFilter"}],
   :responses {nil {:$ref "#/components/responses/NotFound"}}}}]

Note that the response status codes are missing.

from martian.

oliyh avatar oliyh commented on May 20, 2024

Yes, those nil status codes correspond to the exception that's being thrown.

from martian.

bombaywalla avatar bombaywalla commented on May 20, 2024

Thanks. That addresses the NPE.

from martian.

bombaywalla avatar bombaywalla commented on May 20, 2024

I was able to generate EDN from the YAML that was acceptable to martian. To help anyone wanting to add built-in YAML support for martian, here is what I needed to do.

  (require '[clj-yaml.core :as yaml])
  (require '[martian.core :as martian])
  (require '[martian.hato :as martian-http])
  (require '[martian.openapi :as openapi])
  (require '[clojure.walk :as w])

  (defn cleanup
    "Clean up the EDN returned by clj-commons/clj-yaml
    to be compatible with what martian expects."
    [edn]
    (w/postwalk (fn [x]
                  (cond
                    ;; replace all LazySeqs with Vectors
                    ;; See https://github.com/clj-commons/clj-yaml/pull/18
                    (and (seq? x) (not (vector? x)))
                    (into [] x)
                    ;; make sure all the keys of maps are keywords, including keys that were numbers
                    ;; See https://github.com/clj-commons/clj-yaml/blob/master/src/clojure/clj_yaml/core.clj#L128-L129
                    (map? x)
                    (into {} (map (fn [[k v]] [(or (keyword k) (if (number? k) (keyword (str k)) k)) v]) x))
                    ;; otherwise do nothing
                    :else
                    x))
                edn))

  (defn yaml->edn
    "Convert a YAML OpenAPI Spec to EDN compatible with martian."
    [url]
    (-> url
        (slurp)
        (yaml/parse-string)
        (cleanup)))


  (let [sp-url "https://d3a0d0y2hgofx6.cloudfront.net/openapi/en-us/sponsored-products/2-0/openapi.yaml"
        edn (yaml->edn sp-url)
        base-url (openapi/base-url edn)
        m (martian/bootstrap-openapi base-url edn martian-http/default-opts)]
    (martian/explore m))

from martian.

bombaywalla avatar bombaywalla commented on May 20, 2024

If you'd like a PR that handles YAMP OpenAPI specs for just CLJ clients, I'm happy to make one based on the above comment.

from martian.

oliyh avatar oliyh commented on May 20, 2024

Hi @bombaywalla ,

Thanks for your work on this. I'd be happy to accept a PR, you could make a new namespace called martian.yaml in the core/src directory.

Thanks

from martian.

bombaywalla avatar bombaywalla commented on May 20, 2024

I'm presuming you want a martian.yaml cljc file but one that will (currently) only work for CLJ.
If something different, let me know.

from martian.

oliyh avatar oliyh commented on May 20, 2024

Hi,

You might as well make it a clj file if it won't have any public cljs fns, makes it clearer how to use it.

Cheers

from martian.

bombaywalla avatar bombaywalla commented on May 20, 2024

Okay.

from martian.

awb99 avatar awb99 commented on May 20, 2024

Very keen to see yaml working. It seems many apis only publish yaml. Xero accounting api for example
https://raw.githubusercontent.com/XeroAPI/Xero-OpenAPI/master/xero_accounting.yaml

Converting such a huge api to edn seems like a lot of room for translation errors.

from martian.

oliyh avatar oliyh commented on May 20, 2024

Added in #132 and available in 0.1.21-SNAPSHOT

from martian.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.