Optional query parameters (with default value) with compojure-api

狂风中的少年 提交于 2019-12-04 23:45:32

For your first issue, this is working as designed. When you had your boolean query param required, Swagger rendered the UI which forces you to choose a value (either true or false, it just happens that it displays true on the first place).

When you changed the boolean query param to be optional, then the first empty value means "don't send this query param at all" and when you don't change it to true or false it won't append this query param to the request.

Regarding your second issue with integer query param: by default schema's json-coercion-matcher specifies String->Long coercion but not String->Integer so there is no support for Integer out of the box. You can specify your own coercer globally for your API or per route by using :coercion option (there is an example in compojure-api test). You could provide your own coercer which could extend the existing json-coercion-matcher with String->Integer case.

If you use clojure.spec and you want as a default value false in your swagger documentation for a boolean variable you can use spec tool library and do something like :

(s/def ::show-future 
  (st/spec {:spec boolean?
            :json-schema/example false
            :json-schema/default false}))

Then, in your query params :

:query-params [{show-future :- ::show-future false}]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!