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

Not an issue with martian; just don't know what I'm doing. #151

Closed rkirchofer closed 2 years ago

rkirchofer commented 2 years ago

Hey!

I'm having trouble getting started with martian. I'm trying to get a small example working so I'm trying to bootstrap against my own application's routes.

Here's the json that I have and some code that tries to bootstrap it. The result of calling bootstrap-swagger gives me a martian.core.Martian but I think it's missing the interesting stuff. I don't see any paths. That doesn't seem right; what am I doing wrong?

Thanks!

(require '[martian.core :as martian])
(require '[cheshire.core :as json])

(def m (-> (slurp "json.json")
           (json/parse-string true)
           (->> (martian/bootstrap-swagger "https://www.business-company.com/"))))

(keys m)
;; (:api-root :handlers :interceptors :opts)

(:handlers m)
;; ()
{
  "swagger": "2.0",
  "x-id": [
    "reitit.swagger/default"
  ],
  "info": {
    "title": "com.company/application",
    "description": "TODO"
  },
  "paths": {
    "/health/shallow": {
      "get": {
        "responses": {
          "200": {
            "schema": {
              "type": "object",
              "properties": {
                "version": {
                  "type": "string"
                },
                "git_sha": {
                  "type": "string"
                },
                "time": {
                  "type": "string",
                  "format": "date-time"
                }
              },
              "required": [
                "version",
                "git_sha",
                "time"
              ],
              "title": "com.company.health-check/shallow-health"
            },
            "description": ""
          }
        },
        "produces": [
          "application/json",
          "application/transit+msgpack",
          "application/transit+json",
          "application/edn"
        ],
        "consumes": [
          "application/json",
          "application/transit+msgpack",
          "application/transit+json",
          "application/edn"
        ],
        "parameters": [],
        "summary": "Shallow Health Check",
        "description": "Check the health of the service.",
        "tags": [
          "Health Checks"
        ]
      }
    },
    "/api/v1/{foo}/{bar}/business/report": {
      "get": {
        "responses": {
          "200": {
            "result": {
            },
            "description": ""
          },
          "404": {
            "result": {
              "__methodImplCache": null
            },
            "description": ""
          }
        },
        "produces": [
          "application/json",
          "application/transit+msgpack",
          "application/transit+json",
          "application/edn"
        ],
        "consumes": [
          "application/json",
          "application/transit+msgpack",
          "application/transit+json",
          "application/edn"
        ],
        "parameters": [
          {
            "in": "header",
            "name": "x-api-key",
            "description": "",
            "type": "string",
            "required": true
          },
          {
            "in": "query",
            "name": "baz",
            "description": "",
            "type": "string",
            "required": false
          },
          {
            "in": "query",
            "name": "quux",
            "description": "",
            "type": "string",
            "required": false
          }
        ],
        "summary": "Retrieve the business report.",
        "description": "Retrieve the business report.",
        "tags": [
          "API"
        ]
      }
    }
  }
}
oliyh commented 2 years ago

Hello,

The issue with your swagger JSON is that there are no operationIds. In the README (although the README is now very long) is a caveats section:

You need :operationId in the OpenAPI/Swagger spec to name routes when using bootstrap-openapi

You can also call (martian.core/explore m) to see the handlers that martian can see.

Another thing you'll want to do is call bootstrap from an implementation namespace like martian.clj-http/bootstrap-openapi as this will include interceptors to actually perform the http request (martian.core does not have these).

Hope this helps!

rkirchofer commented 2 years ago

Thank you, that is exactly what I needed!