Fork me on GitHub
#ring-swagger
<
2015-10-11
>
trieloff18:10:08

Is there a way to get compojure-swagger to handle single value form parameters more gracefully when having only a single value?

trieloff18:10:08

I am using

:form-params [inputs :- [String] outputs :- [String] name :- String]
but it is only working, iff I include more than one parameter for inputs and more than one for outputs.

juhoteperi18:10:46

Hmh, not too familiar with form-params but I guess they are encoded like query params, thus collection with one item is the same as single input

juhoteperi18:10:12

It would need one item -> collection coercer for form-params

juhoteperi18:10:43

@trieloff: Which compojure-api version are you using?

trieloff18:10:29

I’m on 0.22.0

juhoteperi18:10:59

Hmm should work

trieloff19:10:59

Does this mean I should override the default coercion?

juhoteperi19:10:23

The default coercion should already be wrapping single items in collections for query and form params

juhoteperi19:10:20

Okay, looks like collections are not supported in url-encoded form-data currently

juhoteperi19:10:32

At least for swagger-ui generated requests

trieloff19:10:28

Yes, it looks like swagger-style requests key=value1,value2 is not enabled

juhoteperi19:10:29

In url-encoded form-data collections are encoded as comma separated string

trieloff19:10:03

This should be the split-params-matcher, correct?

juhoteperi19:10:33

I don't remember exactly why it was commented out, but probably because there should be some way to control when it's used

juhoteperi19:10:51

As a quick fix you can set up custom coercer using split-params-matcher

juhoteperi19:10:02

Or use JSON params

juhoteperi19:10:34

And perhaps if you set swagger to use :collectionFormat multi it will generate requests with separate properties for each value

trieloff19:10:31

Hm, but switching from form-params to query-params has the same problem

trieloff19:10:08

?name=help&inputs=A1&outputs=A2&inputs=B1&outputs=B2 works fine, but don’t leave one out

juhoteperi19:10:14

According to Swagger-spec this should work: :form-params [x :- (field [String] {:collectionFormat "multi"})] and it generates valid swagger.json, but looks like Swagger-ui doesn't care about that

trieloff19:10:23

For now, I’m building the requests manually in curl – Swagger UI is secondary, but even then it fails unless I really have more than one value

juhoteperi19:10:51

Do you have a reason to use either form or query params?

juhoteperi19:10:58

body params work fine

trieloff19:10:45

Let me try that

juhoteperi19:10:57

With query-params, ?inputs=a and ?inputs=a&inputs=b should both give a vector

trieloff19:10:09

Body params works fine for me as well.

trieloff19:10:10

I can’t find it in the docs and the compiler is complaining that it can’t find it either.

juhoteperi19:10:19

It's in ring.swagger.json-schema

trieloff19:10:26

(no wonder, if I can’t tell it where to look)

trieloff19:10:50

:query-params [x :- (js/field [String] {:collectionFormat "multi"})]

trieloff19:10:06

POST /formula/hans/*?x=A1 HTTP/1.1

trieloff19:10:26

{
  "errors": {
    "x": "(not (sequential? \"A1\"))"
  }
}

trieloff19:10:02

With multiple x parameters, it’s working.

trieloff19:10:46

Let me bump the version to 0.23.1

trieloff19:10:11

That seems to have helped. @juhoteperi – thanks for the help. I should have started with the version bump. simple_smile

juhoteperi19:10:28

Oh right, now that I check changelogs, 0.23 entry mentions this

trieloff19:10:35

I’m definitely not working on this frequently enough. When I started, 0.22 was the latest version.

trieloff19:10:09

Now all that’s missing is split-params-matcher – but I can live with this and will watch out for 0.24