具有compojure-api的可选查询参数(带有默认值) [英] Optional query parameters (with default value) with compojure-api

查看:271
本文介绍了具有compojure-api的可选查询参数(带有默认值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 compojure-api时,使用默认值声明可选查询参数的正确方法是什么?

What is the proper way to declare an optional query parameter, with default value, when using compojure-api?

我的路线元素之一如下(在阅读 this ):

One of my route elements is as follows (after reading this):

(GET "/:id/descendants" [id]
     :return [d/CategoryTreeElement]
     :path-params [id :- Long]
     :query-params [context-type :- d/ContextType
                    levels :- Integer
                    {tenant :- d/Tenant :DEF_TENANT}
                    {show-future :- Boolean false}
                    {show-expired :- Boolean false}
                    {show-suppressed :- Boolean false}
     :summary "Fetch category descendants"
     (ok ...))

首先是布尔参数,其中定义为其他的(例如 show-future Boolean ),但生成的Swagger UI将它们显示为组合框 true 值作为默认值。在当前形式中,UI显示没有选择选项的组合框。

At first the boolean params where defined as the other ones (e.g. show-future Boolean) but the generated Swagger UI presented them as a combobox with true value as default. In the present form the UI shows a combobox with no option selected. The same happens with tenant.

一个问题:当我使用Swagger生成的UI发送请求并返回错误时:levels (not(instance?java.lang.Integer \2 \))。这是为什么?不是库应该强制转换字符串值到API声明的指定类型?

One side question: when I use Swagger generated UI to send a request and error is returned: "levels": "(not (instance? java.lang.Integer \"2\"))". Why is that? Isn't the library supposed to coerce/convert string values to the designated types declared by the API?

提前感谢。

推荐答案

对于你的第一个问题,这是按照设计工作。当您需要布尔查询参数时,Swagger会渲染UI,强制您选择一个值( true false

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).

当你将布尔查询参数改为可选时,第一个空值意味着don t发送这个查询参数所有,当你不改变它到 true false 请将此查询参数附加到请求。

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.

关于整数查询的第二个问题:param:默认情况下模式的 json-coercion-matcher 指定 String-> Long 强制,但不是 String-> Integer ,因此不支持 Integer 的盒子。您可以使用:coercion 选项(在compojure-api test 中有一个示例)。你可以提供自己的coercer,它可以用 String-> Integer 情况扩展现有的 json-coercion-matcher

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.

这篇关于具有compojure-api的可选查询参数(带有默认值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆