5

While designing the API, I am defining a UserGeo model that contains two fields - domain (string array) and country (string array).

An empty List [] should be used in the request body if no value is provided for the domain. But on defining the default property as [] the generates Spring code does not assign empty ArrayList<> aka [].

Attaching the code samples:

Swagger Definition:

definitions:
  UserGeo:
    type: "object"
    properties:
      country:
        type: "array"
        items:
          type: "string"
      domain:
        type: "array"
        items:
          type: "string"
        default: []

This does not default the domain value to an empty list, check this generated Spring/Java code:

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

  @JsonProperty("domain")
  @Valid
  private List<String> domain = null;
.
.

Whereas when I define the domain field as required without even defining it with default, the generated code assigns an empty list as default for the domain.

definitions:
  UserGeo:
    type: "object"
    required:
      - "domain"
    properties:
      country:
        type: "array"
        items:
          type: "string"
      domain:
        type: "array"
        items:
          type: "string"

And as mentioned the generated code has an empty list assigned for the domain.

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

  @JsonProperty("domain")
  @Valid
  private List<String> domain = new ArrayList<String>();

And the problem is if a field is required them I want to mark the request as BadRequest when the key is not available, instead of assigning an empty array to it.

Can anyone tell me how to make an array use the default [] without being required? and how can I make sure that my required marked array item raises a BadRequest instead of assigning default [] to the property?

im_bhatman
  • 896
  • 1
  • 18
  • 28

2 Answers2

2

What fixed my issue was that i added the required and set the field to it. like this, an empty list got assigned to it instead of null.

so in your case i would suggest:

definitions:
  UserGeo:
    type: "object"
    properties:
      country:
        type: "array"
        items:
          type: "string"
      domain:
        type: "array"
        items:
          type: "string"
    required:
      - country
      - items

PS. even assigning a nullable: false did not help in my case.

Yasi Klingler
  • 606
  • 6
  • 13
  • There's a small error in your answer... it should be `domain` in the list `required`. I presume this has already spotted by many, which is why the edit queue is full. Please fix it when you get time.. :) – Romeo Sierra Oct 20 '22 at 06:44
0

Have the same problem myself, but changed the logic to use minItems on an array and get a BadRequest if array is empty:

definitions:
  UserGeo:
    type: "object"
    required:
      - "domain"
    properties:
      country:
        type: "array"
        items:
          type: "string"
      domain:
        type: "array"
        minItems: 1
        items:
          type: "string"

HTH

Magic Wand
  • 1,572
  • 10
  • 9