Archie
Archie

Reputation: 982

Using Java objects as request parameters breaks down to primitives in swagger ui springfox

I have the following controller code

@GetMapping("/users")
public ResponseEntity<UserDto> getUsers(Filter filter) {
    return ResponseEntity.ok(userService.findUsers(filter));
}

Filter.java:

public class Filter {
    private Integer page;

    private Integer size;

    // Contains 2 fields: "propertyName" and "order"
    private Sort sort;

    ... getters and setters
}

The URL is following: /users?page=1&size=10&sort=+firstName. So I have a custom converter from String to Sort and it works perfectly.

However, the generated swagger documentation is incorrect:

"parameters":[  
           {  
              "name":"sort.propertyName",
              "in":"query",
              "required":false,
              "type":"string"
           },
           {  
              "name":"sort.order",
              "in":"query",
              "required":false,
              "type":"string",
              "enum":[  
                 "+",
                 "-"
              ]
           },
           {  
              "name":"page",
              "in":"query",
              "required":false,
              "type":"integer",
              "format":"int32"
           },
           {  
              "name":"size",
              "in":"query",
              "required":false,
              "type":"integer",
              "format":"int32"
           }
]

As you can see, it has broken down the Sort field of Filter and has generated 2 parameters: sort.propertyName and sort.order. Instead, I want to have one parameter sort with type string.

Is there any way to achieve that? I have tried annotating the sort field with @ApiParam(name = "sort", value = "Sort", type = "string"), but it doesn't work.

Upvotes: 2

Views: 515

Answers (1)

Dilip Krishnan
Dilip Krishnan

Reputation: 5487

You could create an alternate type rule that treats Sort as a String

import static springfox.documentation.schema.AlternateTypeRules.*;
...
// Configure your docket bean
    new Docket(...)
         ...
         .alternateTypeRules(newRule(Sort.class, String.class)
         ...

Upvotes: 1

Related Questions