2

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.

2
  • 1
    You can create an alternate type rule to treat Sort as a String Commented Apr 22, 2018 at 11:23
  • Thanks, @DilipKrishnan, that worked! Can you please add your comment as an answer, so that I can accept it? Commented Apr 25, 2018 at 9:38

1 Answer 1

1

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)
         ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.