Search code examples
spring-bootswagger-uispringfox

Springfox Swagger generates fields not in the method signature


I enabled springfox swagger for my spring boot application. Here is the code setup

SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig
{
    @Bean
    public Docket api()
    {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.ant("/api/**")).build().apiInfo(metaData());
    }

    private ApiInfo metaData()
    {
        ApiInfo apiInfo = new ApiInfo("myApp REST API", "REST APIs for myApp", "1.0.0", "", new Contact("", "", ""), "Proprietary", "", Collections
                .emptyList());
        return apiInfo;
    }
}

Here is my controller

@RequestMapping("/api/user")
@RestController
public class UserApiController
{
    @Autowired
    UserService userService;

    @Secured(value = { "ROLE_ADMIN", "PERMISSION_LIST_USERS" })
    @GetMapping(value = "/list", produces = "application/json")
    List<UserBO> listUsers(HttpSession session)
    {
        Long companyId = (Long) session.getAttribute("companyId");
        List<UserBO> users = userService.listUsers(companyId);
        return users;
    }

    @Secured(value = { "ROLE_ADMIN", "PERMISSION_USER_CREATE" })
    @PostMapping("/create")
    ResponseEntity<String> createUser(@RequestParam String login, @RequestParam String password, @RequestParam String firstName, @RequestParam String lastName, @RequestParam Long usergroupId, HttpSession session)
    {
        Long companyId = (Long) session.getAttribute("companyId");
        ResponseEntity<String> response = userService
                .createUser(login, password, firstName, lastName, usergroupId, companyId);
        return response;
    }

    ...
    ...
    ...
}

Here is the swagger ui I see

enter image description here

There are a lot of other parameters that are not in the method signature (creationTime, lastAccessedTime etc).

Why is swagger generating those and how do I prevent it and have only the params in the method signature generated?


Solution

  • I finally figured it out, as @AlanHay has pointed out, the other parameters were showing because the HttpSession object was in the method signature and in order to ignore that I had to use the @ApiIgnore annotation for the HttpSession parameter in the method signature.

    So the method would be

    @Secured(value = { "ROLE_ADMIN", "PERMISSION_LIST_USERS" })
    @GetMapping(value = "/list", produces = "application/json")
    List<UserBO> listUsers(@ApiIgnore HttpSession session)
    {
        Long companyId = (Long) session.getAttribute("companyId");
        List<UserBO> users = userService.listUsers(companyId);
        return users;
    }