开放 API 规范 Swagger 中 API 中的对象作为查询字符串参数

SSK*_*SSK 3 query-string swagger swagger-ui openapi

我有一个 API,它接受查询参数作为对象。我正在使用它添加多个过滤器来过滤结果。

当我收到来自 swagger 的请求时,控制器中的过滤器对象为 null。

userFilter是 POJO 类。它被用作 aquery param并且在控制器中,它作为 null 出现。

在swagger上,显示如下

招摇截图

userFilter当尝试从 userFilter 访问任何字段时,对象未构造并在控制器类中获取 NullPointerException。

SSK*_*SSK 6

我从swagger.io得到了解决方案。

根据解释,content用于complex serialization scenarios样式和爆炸未涵盖的情况。例如,如果我们需要发送一个 JSON 字符串,如下所示query string

filter={"type":"t-shirt","color":"blue"}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我们需要将parameter架构包装成content/<media-type>如下所示。

我们需要添加content = {@Content(schema = @Schema(type = "object"))}@Parameter.

@Parameter(description = "Filters", required = true, content = {@Content(schema = @Schema(type = "object"))})
Run Code Online (Sandbox Code Playgroud)

其格式JSON如下所示。

parameters:
  - in: query
    name: filter
    
    # Wrap 'schema' into 'content.<media-type>'
    content:
      application/json:  # <---- media type indicates how to serialize / deserialize the parameter content
        schema:
          type: object
          properties:
            type:
              type: string
            color:
              type: string
Run Code Online (Sandbox Code Playgroud)