gpt4 book ai didi

clojure - 带有 compojure-api 的可选查询参数(具有默认值)

转载 作者:行者123 更新时间:2023-12-03 23:55:43 26 4
gpt4 key购买 nike

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

我的路由元素之一如下(阅读 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 ...))

起初, bool 参数定义为其他参数(例如 show-future Boolean ),但生成的 Swagger UI 将它们显示为带有 true 的组合框值作为默认值。在目前的形式中,用户界面显示了一个没有选择任何选项的组合框。租户也是如此。

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

提前致谢。

最佳答案

对于您的第一个问题,这是按设计工作的。当您需要 bool 查询参数时,Swagger 会呈现 UI,迫使您选择一个值( truefalse ,碰巧它首先显示为 true )。

当您将 bool 查询参数更改为可选时,第一个空值表示“根本不发送此查询参数”,而当您不将其更改为 true 时或 false它不会将此查询参数附加到请求中。

关于整数查询参数的第二个问题:默认情况下 schema's json-coercion-matcher 指定 String->Long coercion but not String->Integer 所以不支持 Integer盒子外面。您可以使用 :coercion 为您的 API 或每个路由全局指定您自己的强制转换程序。选项( there is an example in compojure-api test )。你可以提供你自己的 coercer,它可以扩展现有的 json-coercion-matcherString->Integer案件。

关于clojure - 带有 compojure-api 的可选查询参数(具有默认值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35850921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com