Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger Param Documentation

Current State:

I have two methods in my controller to get data based on what parameters are passed. The code:

@RestController
@RequestMapping("/samples")
public class SampleController {

    @RequestMapping(value = "/{id}", params = {"cost"}, method = RequestMethod.GET)
    public String getSamplesByIdAndCost(@PathVariable String id, @RequestParam(value = "cost") String cost) {
        return "result";
    }

    @RequestMapping(value = "/{id}", params = {"cost", "size"}, method = RequestMethod.GET)
    public String getSamplesByIdCostAndSize(@PathVariable String id, @RequestParam(value = "cost") String cost,
                                        @RequestParam(value = "size") String size) {
    return "ID : " + id + " / COST : " + cost + " / SIZE : " + size;
    }
}

Everything works fine, but the swagger documentation is not what I expected.

enter image description here

enter image description here

Question

Is there a way to remove {?size,cost} from the Request URL?

Here is my Docket info:

@Bean
    public Docket myApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .directModelSubstitute(LocalDate.class,
                        String.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .alternateTypeRules(
                        newRule(typeResolver.resolve(DeferredResult.class,
                                typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                                typeResolver.resolve(WildcardType.class)))
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET,
                        newArrayList(new ResponseMessageBuilder()
                                .code(500)
                                .message("500 message")
                                .responseModel(new ModelRef("Error"))
                                .build()))
                .enableUrlTemplating(true);

    }

    @Autowired
    TypeResolver typeResolver;

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(
                "validatorUrl",// url
                "none",       // docExpansion          => none | list
                "alpha",      // apiSorter             => alpha
                "schema",     // defaultModelRendering => schema
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS,
                false,        // enableJsonEditor      => true | false
                true);        // showRequestHeaders    => true | false
    }
like image 645
Safari137 Avatar asked Jul 27 '26 15:07

Safari137


1 Answers

Multiple documentations for the same path based on query strings is not something that is supported by Swagger spec and therefore not by swagger-ui.

What you have enabled by setting enableUrlTemplating(true) appears to be an incubation feature in springfox but won't work as of now with swagger-ui.

Relevant discussion can be found here:

  • https://github.com/swagger-api/swagger-ui/issues/1665
  • https://github.com/springfox/springfox/issues/866

For now it seem like you either have to live with paths looking weird in the swagger-ui, or you have to merge your documentation.

like image 50
Tobias Raski Avatar answered Jul 30 '26 04:07

Tobias Raski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!