ring-swagger

Dumch 2022-02-12T07:02:08.622359Z

Is there an option to provide optional file spec? I am using :require [[reitit.ring.middleware.multipart :as multipart]] provide middleware multipart/multipart-middleware and set up parameters as :parameters {:multipart {:logs multipart/temp-file-part}} when I tried something like (spec/nilable multipart/temp-file-part) , it doesn’t work.

Dumch 2022-02-12T07:02:33.898379Z

So I have to provide two routes instead of one with the ability to pass files optionally

ikitommi 2022-02-12T07:06:44.265549Z

looking at https://cljdoc.org/d/metosin/spec-tools/0.10.5/doc/data-specs, maybe :parameters {(ds/opt :logs) multipart/temp-file-part}

👍 1
Dumch 2022-02-12T08:17:44.379019Z

thank you, but it still

Dumch 2022-02-12T09:29:00.496729Z

but your answer helped me to find a solution {:logs (ds/maybe multipart/temp-file-part)}

Dumch 2022-02-12T09:46:13.315359Z

oh, now when I send emty value, it sends empty string “” and spec predicate failes

Dumch 2022-02-12T09:46:41.531819Z

tried (ds/opt :rules) (ds/maybe multipart/temp-file-part) output is

{
  "spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$28868/logs] :opt-un [:spec$28868/rules]), :type :map, :leaf? false})",
  "problems": [
    {
      "path": [
        "rules",
        "clojure.spec.alpha/nil"
      ],
      "pred": "nil?",
      "val": "",
      "via": [
        "spec$28868/rules"
      ],
      "in": [
        "rules"
      ]
    },
    {
      "path": [
        "rules",
        "clojure.spec.alpha/pred"
      ],
      "pred": "clojure.core/map?",
      "val": "",
      "via": [
        "spec$28868/rules"
      ],
      "in": [
        "rules"
      ]
    }
  ],
  "type": "reitit.coercion/request-coercion",
  "coercion": "spec",
  "value": {
    "logs": {
      "filename": "bugreport-33345431500100001233071f-1.74.52-01-16-2022-11-46-19.zip",
      "content-type": "application/zip",
      "tempfile": "/var/folders/gm/sgh_2g690cn85ybhmv1phs5c0000gq/T/ring-multipart-1380586888493316976.tmp",
      "size": 4644776
    },
    "rules": ""
  },
  "in": [
    "request",
    "multipart-params"
  ]
} 

Dumch 2022-02-12T11:03:14.971829Z

I figured out how to force it to work. This is the final spec

:rules 
(ds/maybe
 (spec-tools.core/spec
   {:spec         (spec/or
                    :file multipart/temp-file-part
                    :nan string?)
    :swagger/type "file"}))
and in my handler I check if (string? rules)