Fork me on GitHub
#ring-swagger
<
2023-02-20
>
Al Z. Heymer16:02:13

Hey, we are using compojure-api, but stumbled upon a problem, we are unsure how to solve. This is our OpenAPI Spec, that we created with swagger.

parameters:
      - name: "content"
        in: "formData"
        description: "file to upload"
        required: true
        type: "file"
      - name: "content-id"
        in: "formData"
        description: ""
        required: false
        type: "string"
        default: "test"
      - in: "formData"
        name: "context-data"
        required: false
        type: "array"
        collectionFormat: "multi"
        items:
          type: "array"
          minItems: 2
          maxItems: 2
          items:
            type: "string"
        default: [["from","max\\,[email protected]"],["mail-id","123456"]]
the context-data format is a requirement by our customer. An according curl would be
curl -X 'POST' \
  'https://...' \
  -H 'accept: application/xml' \
  -H 'Content-Type: multipart/form-data' \
  -F 'content=@black-hole-7-3840×2160.jpg;type=image/jpeg' \
  -F 'content-id=test' \
  -F 'context=key,value' \
  -F 'context=from,[email protected]' \
  -F 'context=mail-id,123456' \
  -F 'context=value-with-comma,1\,2\,'
So that arbitrary context-data can be provided. However, we found that this oa2 spec does not go well with the implementation of compojure-api. Our initial approach to this was something like
(POST ...
    :multipart-params [content :- compojure.api.upload/ByteArrayUpload
                       content-id :- s/Str
                       context-data :- (compojure.api.sweet/describe s/Str "context-data kv-pairs")]
    :middleware [[ring.middleware.multipart-params/wrap-multipart-params
                  {:store (ring.middleware.multipart-params.byte-array/byte-array-store)}]])
According to ChatGPT the 2D-Array solution is possible, yet only without additional params like so
:form-params [context-data [(array-of (array-of string?))] :required? false]
Unfortunately I can't find anything to this in the documentation of compojure-api nor compojure. We intend to move over to reitit in the future, yet this is further away on the roadmap, and not an immediate option. Would anybody be willing to help?

Al Z. Heymer16:02:13

I think a possible alternative solution would be to provide arbitrary kv-pairs in the form of -F 'from=' , but I am uncertain how this could be achieved either.