xiaoymin / knife4j

Knife4j is a set of Swagger2 and OpenAPI3 All-in-one enhancement solution
https://doc.xiaominfo.com
Apache License 2.0
4.15k stars 622 forks source link

4.1.0 版本中,请求参数模型始终都是 applican/json ,这个应该是个 bug #571

Open zq99299 opened 1 year ago

zq99299 commented 1 year ago

implementation 'com.github.xiaoymin:knife4j-openapi3-jakarta-spring-boot-starter:4.1.0'

    @Operation(summary = "登录", description = "从 SSO 登录后")
    @PostMapping(value = "/signin")
    public Result signin(@Validated SigninReq params) {
        String token = ssoService.signin(params);
        return ResultHelper.ok(token);
    }

image image

然后明确的声明了请求类型

    @Operation(summary = "登录", description = "从 SSO 登录后")
    @PostMapping(value = "/signin",consumes = "application/x-www-form-urlencoded")
    public Result signin(@Validated SigninReq params) {
        String token = ssoService.signin(params);
        return ResultHelper.ok(token);
    }

界面中还是显示 application/json ,但是 open-api 数据中已经变成 application/x-www-form-urlencoded 了 image

en-o commented 1 year ago

@Valid 这个注解的问题,我也还在找怎么解决

en-o commented 1 year ago

可以用这个@RequestParam

zq99299 commented 1 year ago

这个能对应一个类吗

en-o commented 1 year ago

这个能对应一个类吗

我测试可以

zq99299 commented 1 year ago

这个能对应一个类吗

我测试可以

你是对的,确实解决了

    @Operation(summary = "登录", description = "从 SSO 登录后")
    @PostMapping(value = "/signin")
    public Result signin(@Validated @RequestParam SigninReq params) {
        String token = ssoService.signin(params);
        return ResultHelper.ok(token);
    }

image image

zq99299 commented 1 year ago

确实,如果这样的话,@Validated 就无法使用了,那还是有问题

   @Operation(summary = "登录", description = "从 SSO 登录后")
    @PostMapping(value = "/signin",consumes = "application/x-www-form-urlencoded")
    public Result signin(@Validated SigninReq params) {
        String token = ssoService.signin(params);
        return ResultHelper.ok(token);
    }

这样写得话, swagger 是能正确识别并显示的

objectboy2016 commented 6 months ago

这个bug还没解决......我这边也遇到同样的问题了 , 无法设置请求数据类型, 我设置 @PostMapping(value = "delete", consumes = "application/json") , 接口的请求数据类型依旧无法动态设置, 里面的逻辑应该是通过参数前面的@RequestBody @requestparam 注解获取的吧

springboot version: 3.1.12 版本 4.5.0