如何将空值传递给 swagger 中的查询参数?

Ana*_*and 7 swagger swagger-ui

我正在尝试访问类似于http://example.com/service1?q1=a&q2=b的网址。然而,q1 有时不会有任何与之关联的值,但需要访问服务 ( http://example.com/service1?q1=&q2=b )。我如何通过 swagger ui JSON 实现这一点?我尝试过使用 allowedEmptyValue 选项,但它似乎不起作用。

请在下面找到我尝试使用allowEmptyValue选项的示例JSON,

{
    "path": "/service1.do",
    "operations": [{
        "method": "GET",
        "type": "void",
        "parameters": [{
                "name": "q1",
                "in" : "query",
                "required": false,
                "type": "string",
                "paramType": "query",
                "allowEmptyValue": true
            },{
                "name": "q2",
                "in" : "query",
                "required": true,
                "type": "string",
                "paramType": "query",
            }


        ],
        "responseMessages": [{
            "code": 200,
            "responseModel": "/successResponseModel"
        }
}
Run Code Online (Sandbox Code Playgroud)

当将空值传递给 q1 时,swagger 将 URL 构造为http://example.com/service1?q2=b。无论如何,是否要在 URL 中包含带有空值的 q1 ( http://example.com/service1?q1=&q2=b ) ?

任何帮助将不胜感激。

Sup*_*hne 5

看来您的问题是 swagger-ui 的一个已知问题,尚未修复。

作为解决方法,您可以执行以下操作之一。

选项 1:指定默认值。

该选项与 swagger-ui 无关。在您的 ws 实现中,您必须添加一个默认值(在您的情况下为“”),以便在未添加“q1”时使用。任何 REST 框架都有此选项。

从 ws 实现的角度来看,这应该存在于您的 ws 中,除非您在未添加“q1”时要触发另一个服务。(在大多数情况下,这可能不是一个好的设计)并且您可以将其用作永久解决方案,而不是临时解决方案。

选项 2:使用枚举(不是一致的解决方案)

正如 中所解释的。您可以为 swagger 定义指定查询参数“q1”,如下所示。

{
                 "in": "query",
                 "name": "q1",
                 "type": "boolean",
                 "required": false,
                 "enum" : [true],
                 "allowEmptyValue" : true
}
Run Code Online (Sandbox Code Playgroud)

(1)“要求”必须为假。

(2) “allowEmptyValue”必须为 true。

(3) “enum”必须恰好有一个非空值。

(4)“类型”必须是“布尔值”。(或带有特殊枚举的“字符串”,例如“INCLUDE”)