metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.48k stars 210 forks source link

Malli generates circular json-schema #868

Closed opqdonut closed 1 year ago

opqdonut commented 1 year ago

... at least for mu/closed schemas

(require '[malli.util :as mu])
(require 'malli.json-schema)
(malli.json-schema/transform (mu/closed-schema [:schema {:registry {"Foo" :int}} "Foo"]))
 ==> {:$ref "#/definitions/Foo", :definitions {"Foo" {:$ref "#/definitions/Foo"}}}  ;; wrong, circular schema
(malli.json-schema/transform [:schema {:registry {"Foo" :int}} "Foo"])
 ==> {:$ref "#/definitions/Foo", :definitions {"Foo" {:type "integer"}}} ;; right

863 seems to fix this.

cap10morgan commented 1 year ago

@opqdonut Should this be closed now too since #863 was merged?

opqdonut commented 1 year ago

I'll do some more testing and see if I can find any other corner cases.

opqdonut commented 1 year ago

Mutual recursion still doesn't work:

user=> (malli.json-schema/transform [:schema {:registry {"Foo" [:vector [:ref "Bar"]] "Bar" [:ref "Foo"]}} "Foo"])
{:$ref "#/definitions/Foo", :definitions {"Foo" {:type "array", :items {:$ref "#/definitions/Bar"}}}}
ikitommi commented 1 year ago

Fixed in master:

(malli.json-schema/transform [:schema {:registry {"Foo" [:vector [:ref "Bar"]] "Bar" [:ref "Foo"]}} "Foo"])
;{:$ref "#/definitions/Foo",
; :definitions {"Foo" {:type "array", :items {:$ref "#/definitions/Bar"}}
;               "Bar" {:$ref "#/definitions/Foo"}}}