metosin / reitit

A fast data-driven routing library for Clojure/Script

Home Page:https://cljdoc.org/d/metosin/reitit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug] Can't generate swagger with var references

bsless opened this issue · comments

I encountered two different problems when schemas use var refs - no schema for GET and bad resolution for POST

Steps to reproduce:

Go to examples/ring-malli-swagger

Explicitly bump reitit to 15:

[metosin/malli "0.15.0"]

Add

(def X :int)
(def Y :int)
(def Plus [:map
           [:x
            {:title "X parameter"
             :description "Description for X parameter"
             :json-schema/default 42}
            #'X]
           [:y #'Y]])

Change parameters in get and post to use schema reference:

:parameters {:query #'Plus}

Start server and navigate to openapi page

Get error:

Resolver error at requestBody.content.application/json.schema.$ref
Could not resolve reference: JSON Pointer evaluation failed while evaluating token "definitions" against an ObjectElement
Resolver error at requestBody.content.application/json.schema.$ref
Could not resolve reference: JSON Pointer evaluation failed while evaluating token "definitions" against an ObjectElement

Observe the generated openapi.json

  • No parameters for GET /math/plus at all
  • Ref for POST /math/plus is dangling: "#/definitions/example.server~1Plus"

See schema opbject:

{
  "$ref": "#/definitions/example.server~1Plus",
  "definitions": {
    "example.server/Plus": {
      "type": "object",
      "properties": {
        "x": {
          "title": "X parameter",
          "description": "Description for X parameter",
          "$ref": "#/definitions/example.server~1X",
          "default": 42
        },
        "y": {
          "$ref": "#/definitions/example.server~1Y"
        }
      },
      "required": [
        "x",
        "y"
      ]
    },
    "example.server/X": {
      "type": "integer"
    },
    "example.server/Y": {
      "type": "integer"
    }
  }
}

This is interesting! I started testing and thought reitit + malli vars is completely broken, but it seems to actually mostly works. I'll try to narrow down what exactly goes wrong in the example.

Ok, so the example ring-malli-swagger is broken in two three ways (when edited to use var references).

  1. The openapi doc is invalid and has the reference errors you pasted. This is probably related to
  2. The swagger doc generation fails (HTTP 500). I have a fix for this: #671
  3. The swagger doc is missing the GET parameters. This is fixed by metosin/malli#1044