metosin / reitit

A fast data-driven routing library for Clojure/Script
https://cljdoc.org/d/metosin/reitit/
Eclipse Public License 1.0
1.4k stars 252 forks source link

[Bug] Can't generate swagger with var references #669

Closed bsless closed 3 months ago

bsless commented 3 months ago

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

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"
    }
  }
}
opqdonut commented 3 months ago

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.

opqdonut commented 3 months ago

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
    • 616

    • 645

    • Should get fixed by: #673
  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