未将空数组选为 Open API AKA Swagger 生成代码中定义模型的默认值

bha*_*man 6 api swagger openapi swagger-codegen openapi-generator

在设计 API 时,我定义了一个UserGeo包含两个字段的模型 - domain(字符串数组) 和country(字符串数组)。

[]如果没有为 提供值,则应在请求正文中使用空列表domain。但是在将default属性定义为[]生成Spring代码时,不会分配空 ArrayList<> 又名[]

附上代码示例:

招摇定义:

definitions:
  UserGeo:
    type: "object"
    properties:
      country:
        type: "array"
        items:
          type: "string"
      domain:
        type: "array"
        items:
          type: "string"
        default: []
Run Code Online (Sandbox Code Playgroud)

这不会将域值默认为空列表,请检查生成的 Spring/Java 代码:

.
.
public class Classification   {
  @JsonProperty("country")
  @Valid
  private List<String> country = null;

  @JsonProperty("domain")
  @Valid
  private List<String> domain = null;
.
.
Run Code Online (Sandbox Code Playgroud)

然而,当我将域字段定义为required甚至没有使用默认值定义它时,生成的代码会分配一​​个空列表作为domain.

definitions:
  UserGeo:
    type: "object"
    required:
      - "domain"
    properties:
      country:
        type: "array"
        items:
          type: "string"
      domain:
        type: "array"
        items:
          type: "string"
Run Code Online (Sandbox Code Playgroud)

如前所述,生成的代码为该域分配了一个空列表。

public class UserGeo   {
  @JsonProperty("country")
  @Valid
  private List<String> country = null;

  @JsonProperty("domain")
  @Valid
  private List<String> domain = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)

问题是,如果需要一个字段,我想在密钥不可用时将请求标记为 BadRequest,而不是为其分配一个空数组。

谁能告诉我如何让数组使用默认值[]而不需要?如何确保我所需的标记数组项引发 BadRequest,而不是为[]属性分配默认值?

Mag*_*and 0

我自己也有同样的问题,但更改了逻辑以在数组上使用 minItems 并在数组为空时获取 BadRequest:

definitions:
  UserGeo:
    type: "object"
    required:
      - "domain"
    properties:
      country:
        type: "array"
        items:
          type: "string"
      domain:
        type: "array"
        minItems: 1
        items:
          type: "string"
Run Code Online (Sandbox Code Playgroud)

华泰