Обязательное поле для отправки в POST - PullRequest
0 голосов
/ 20 марта 2020

У меня есть контроллер, который принимает следующий объект в качестве аргумента к сообщению

@Schema(name="MenuItem", description="Menu Item in shopping cart")
open class MenuItem {

    @NotBlank
    @NotEmpty(message = "Please enter name")
    @Schema(description="The name of the menu item")
    val name : String = "";

    @NotNull(message = "Please enter price")
    @Schema(description="The price of the menu item")
    @Digits(integer=10, fraction=2)
    val price : BigDecimal = BigDecimal("0.0");

    @NotNull(message = "Please enter the quantity")
    @Schema(description="The quantity the user would like to purchase")
    val quantity : Int = 0;
}

, однако следующая полезная нагрузка считается действительной

{
    "name": "Veggie Taco",
    "quantity": "100"
}

Это плохо, так как я хочу пользователь всегда должен давать значение для всех параметров. один из способов решить эту проблему, изменив MenuItem на

@Schema(name="MenuItem", description="Menu Item in shopping cart")
class MenuItem {

    @NotBlank
    @NotEmpty(message = "Please enter name")
    @Schema(description="The name of the menu item")
    public val name : String? = null;


    @NotNull(message = "Please enter price")
    @Schema(description="The price of the menu item")
    @Digits(integer=10, fraction=2)
    public val price : BigDecimal? = null;

    @NotNull(message = "Please enter the quantity")
    @Schema(description="The quantity the user would like to purchase")
    public val quantity : Int? = null;
}

, однако теперь сгенерированный openAPI заявляет, что нулевой является правильным ответом

MenuItem:
  required:
  - name
  - price
  - quantity
  type: object
  properties:
    name:
      minLength: 1
      type: string
      description: The name of the menu item
      nullable: true
    price:
      type: number
      description: The price of the menu item
      nullable: true
    quantity:
      type: integer
      description: The quantity the user would like to purchase
      format: int32
      nullable: true
  description: Menu Item in s

Есть ли способ сказать, что значение должно будет дано при прохождении через JSON?

...