【问题标题】:Swagger Springfox - how to generate schema type array for query param List<String>Swagger Springfox - 如何为查询参数 List<String> 生成模式类型数组
【发布时间】:2021-02-15 19:45:25
【问题描述】:

我正在使用 springfox 3.0.0 版并使用 OAS 注释。

我需要在 Swagger JSON 中为我的方法生成这样的片段(带有模式类型数组):

"parameters":[
   {
      "name":"ids",
      "in":"query",
      "description":"multiple ids",
      "required":true,
      "schema":{
         "uniqueItems":true,
         "type":"array",
         "items":{
            "type":"string"
         }
      }
   },
   ...
]

在 Swagger UI 中生成所需的参数组件:

但是,我无法使用任何可用的 Swagger 注释来获取此 Swagger JSON(以及参数的 Swagger UI 外观)。 目前我的控制器中有这样的代码(顺便说一句,这就像我们在迁移到 springfox 之前使用的 springdoc 库的魅力一样):

import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

@Api
@RestController
@RequestMapping("/something")
@RequiredArgsConstructor
public class MyController {

  @Operation(summary = "Return list of...")
  @ApiResponse(responseCode = "200", description = "Success")
  @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
  public List<SomeDTO> getSomethingByIds(@Parameter(description = "List of ids") @RequestParam("ids") Set<String> ids) {
    ...
  }

但我也尝试过手动设置 io.swagger.v3.oas.annotations.Parameter 注释的架构(如 @Parameter(description = "List of ids", content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) )。
还尝试尝试使用较旧的(非 OAS)Swagger 注释 - 例如 io.swagger.annotations.ApiImplicitParamio.swagger.annotations.ApiParam
还尝试将Set&lt;String&gt; 类型更改为List&lt;String&gt;String[]
这些都没有成功,无论我尝试了什么,springfox 总是会产生这样的 Swagger JSON 片段(带有模式类型字符串),因此只是在 swagger UI 中输入简单的字符串:

"parameters":[
   {
      "name":"ids",
      "in":"query",
      "description":"List of ids",
      "required":true,
      "style":"form",
      "explode":true,
      "schema":{
         "type":"string"
      }
   },
   ...
]

谁有更好的运气?

【问题讨论】:

  • 您是否以某种方式解决了这个问题?我有同样的问题
  • 同样的问题。

标签: java spring swagger springfox


【解决方案1】:

这对我来说很好用:

@Parameter(name = "languages", in = QUERY, array = @ArraySchema( schema = @Schema(type = "string")))

【讨论】:

  • 这个解决方案不适用于 springfox 3.0.0
猜你喜欢
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 2021-08-05
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多