test: use string refs in openapi-malli-tests

... in order to pass the validator, which disallows "/"
This commit is contained in:
Joel Kaasinen 2024-04-19 16:07:59 +03:00
parent 037763561e
commit 3296323059

View file

@ -917,24 +917,26 @@
(testing "ref schemas" (testing "ref schemas"
(let [registry (merge (mc/base-schemas) (let [registry (merge (mc/base-schemas)
(mc/type-schemas) (mc/type-schemas)
{::plus [:map [:x :int] [:y ::y]] {"plus" [:map [:x :int] [:y "y"]]
::y :int}) "y" :int})
app (ring/ring-handler app (ring/ring-handler
(ring/router (ring/router
[["/openapi.json" [["/openapi.json"
{:get {:no-doc true {:get {:no-doc true
:openapi {:info {:title "" :version "0.0.1"}}
:handler (openapi/create-openapi-handler)}}] :handler (openapi/create-openapi-handler)}}]
["/post" ["/post"
{:post {:coercion malli/coercion {:post {:coercion malli/coercion
:parameters {:body (mc/schema ::plus {:registry registry})} :parameters {:body (mc/schema "plus" {:registry registry})}
:handler identity}}] :handler identity}}]
["/get" ["/get"
{:get {:coercion malli/coercion {:get {:coercion malli/coercion
:parameters {:query (mc/schema ::plus {:registry registry})} :parameters {:query (mc/schema "plus" {:registry registry})}
:handler identity}}]])) :handler identity}}]]))
spec (:body (app {:request-method :get :uri "/openapi.json"}))] spec (:body (app {:request-method :get :uri "/openapi.json"}))]
(is (= {:openapi "3.1.0" (is (= {:openapi "3.1.0"
:x-id #{:reitit.openapi/default} :x-id #{:reitit.openapi/default}
:info {:title "" :version "0.0.1"}
:paths {"/get" {:get {:parameters [{:in "query" :paths {"/get" {:get {:parameters [{:in "query"
:name :x :name :x
:required true :required true
@ -942,25 +944,27 @@
{:in "query" {:in "query"
:name :y :name :y
:required true :required true
:schema {:$ref "#/components/schemas/reitit.openapi-test~1y"}}]}} :schema {:$ref "#/components/schemas/y"}}]}}
"/post" {:post "/post" {:post
{:requestBody {:requestBody
{:content {:content
{"application/json" {"application/json"
{:schema {:schema
{:$ref "#/components/schemas/reitit.openapi-test~1plus"}}}}}}} {:$ref "#/components/schemas/plus"}}}}}}}
:components {:schemas :components {:schemas
{"reitit.openapi-test/y" {:type "integer"} {"y" {:type "integer"}
"reitit.openapi-test/plus" {:type "object" "plus" {:type "object"
:properties {:x {:type "integer"} :properties {:x {:type "integer"}
:y {:$ref "#/components/schemas/reitit.openapi-test~1y"}} :y {:$ref "#/components/schemas/y"}}
:required [:x :y]}}}} :required [:x :y]}}}}
spec)))) spec))
(is (nil? (validate spec)))))
(testing "var schemas" (testing "var schemas"
(let [app (ring/ring-handler (let [app (ring/ring-handler
(ring/router (ring/router
[["/openapi.json" [["/openapi.json"
{:get {:no-doc true {:get {:no-doc true
:openapi {:info {:title "" :version "0.0.1"}}
:handler (openapi/create-openapi-handler)}}] :handler (openapi/create-openapi-handler)}}]
["/post" ["/post"
{:post {:coercion malli/coercion {:post {:coercion malli/coercion
@ -973,6 +977,7 @@
spec (:body (app {:request-method :get :uri "/openapi.json"}))] spec (:body (app {:request-method :get :uri "/openapi.json"}))]
(is (= {:openapi "3.1.0" (is (= {:openapi "3.1.0"
:x-id #{:reitit.openapi/default} :x-id #{:reitit.openapi/default}
:info {:title "" :version "0.0.1"}
:paths :paths
{"/post" {"/post"
{:post {:post
@ -1000,4 +1005,9 @@
:y {:$ref "#/components/schemas/reitit.openapi-test~1Y"}} :y {:$ref "#/components/schemas/reitit.openapi-test~1Y"}}
:required [:x :y]} :required [:x :y]}
"reitit.openapi-test/Y" {:type "integer"}}}} "reitit.openapi-test/Y" {:type "integer"}}}}
spec))))) spec))
;; TODO: the OAS 3.1 json schema disallows "/" in :components :schemas keys,
;; even though the text of the spec allows it. See:
;; https://github.com/seriousme/openapi-schema-validator/blob/772375bf4895f0e641d103c27140cdd1d2afc34e/schemas/v3.1/schema.json#L282
#_
(is (nil? (validate spec))))))