'Spring Rest cannot deserialize model attribute that contains List<Long> categoryIDs in Rest API controller

My Spring Rest controller:

@GetMapping("/partner/criteria")
@ValidateRequest
@Operation(summary = "Return partner tasks according to provided search criteria")
public ResponseEntity<PageImpl<TaskSearchResultDto>> getPartnerTasksByCriteria(
        @ModelAttribute @Valid TaskSearchCriteriaDto criteriaDto,
        BindingResult bindingResult,
        @PageableDefault(size = 6, sort = "deadline", direction = DESC) Pageable pageable) {
    ...
}

TaskSearchCriteriaDto.class with which the deserialization problem occurs in List<Long> categoryIds field:

public class TaskSearchCriteriaDto {        
    private Long organizationId;

    private List<@NotNull @Positive Long> categoryIds = new ArrayList<>();
    
    //Getters, Setters, Constructors
}

then swagger generates openapi.json:

"/v1.0/tasks/search/partner/criteria" : {
  "get" : {
    "tags" : [ "task-search-resource" ],
    "summary" : "Return partner tasks according to provided search criteria",
    "operationId" : "getPartnerTasksByCriteriaUsingGet",
    "parameters" : [ {
      "name" : "categoryIds",
      "in" : "query",
      "required" : false,
      "style" : "pipeDelimited",
      "schema" : {
        "type" : "array",
        "items" : {
          "type" : "integer",
          "format" : "int64"
        }
      }
    }, {
    ...
    }

Swagger uses: "style" : "pipeDelimeted" for List<Long> categoryIds in TaskSearchCriteriaDto.class

Now let's run the application and make a get request: GetRequest. As you can see we get an error:400 Bad request

Let's debug and see what's happening: Debug. Spring Rest cannot parse query param - category id.

Now the question is how to resolve this problem. How to make the application correctly parse incoming arrays in query param because the client code is generated using an open API generator.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source