swagger 在springdoc中禁用处理验证注解

pieyvz9o  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(46)

我想从自动生成的swagger文档中删除参数“minimum”
我使用SpringDoc库自动生成Swagger文档

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.1.0</version>
</dependency>

字符串
我在其中一个控制器中有这个方法

@GetMapping("page")
@ResponseStatus(HttpStatus.OK)
List<RecordInfoDto> getPage(@RequestParam("size")
                            @jakarta.validation.constraints.Min(value = 1, message = "size must be more than 0") 
                            int size,
                            @RequestParam("number")
                            @jakarta.validation.constraints.Min(value = 0, message = "number must be non-negative")
                            int number);


为该方法生成以下文档

"/page": {
    "get": {
    ...
        "parameters": [
          {
            "name": "size",
            "in": "query",
            "required": true,
            "schema": {
              "minimum": 1,
              "type": "integer",
              "format": "int32"
            }
          },
          {
            "name": "number",
            "in": "query",
            "required": true,
            "schema": {
              "minimum": 0,
              "type": "integer",
              "format": "int32"
            }
          }
        ],
     ...


我想要同样的swagger文档,只是在“模式”部分没有“最小”参数
我尝试使用io.swagger.v3.oas.annotations.media.annotation.Scheme显式地指定scheme,但无论我将注解放在什么顺序,都无济于事

@GetMapping("page")
@ResponseStatus(HttpStatus.OK)
List<RecordInfoDto> getPage(@RequestParam("size")
                            @io.swagger.v3.oas.annotations.media.Schema(minimum = "100")
                            @jakarta.validation.constraints.Min(value = 1, message = "size must be more than 0")
                            int size,
                            @RequestParam("number")
                            @jakarta.validation.constraints.Min(value = 0, message = "number must be non-negative")
                            int number);


文档中仍然有“最小值”:1作为参数大小
我尝试使用io.swagger.v3.oas.annotations.Parameter,但它也没有帮助

@GetMapping("page")
@ResponseStatus(HttpStatus.OK)
List<RecordInfoDto> getPage(@RequestParam("size")
                            @io.swagger.v3.oas.annotations.Parameter(name = "size",
                                    required = true,
                                    in = ParameterIn.QUERY,
                                    schema = @Schema(minimum = "100"))
                            @jakarta.validation.constraints.Min(value = 1, message = "size must be more than 0")
                            int size,
                            @RequestParam("number")
                            @jakarta.validation.constraints.Min(value = 0, message = "number must be non-negative")
                            int number);


同时,如果我删除注解jaks.validation.constrains.Min,则根据注解生成文档,最小值变为100

zzwlnbp8

zzwlnbp81#

你想要的是使开放API规范谎言
正如您在评论中所说,您“不想删除@Min注解,因为它是真实的验证所必需的",另一方面,您想在生成的Open API规范中隐藏此验证约束。
你的要求是矛盾的,你不应该感到惊讶,这是不可能的。
文档的目的是指定API的合约。如果您的API验证了最小值,则它是其合约的一部分。机器可读文档(Open API)的目的之一是自动防止客户端发送无效请求。
@Schema(minimum="100")@Min(value=1)是精神分裂症。
验证是
合约的一部分**,它 * 应该 * 在Open API规范中。如果合约的一部分是最小值为1,你不需要指定其他任何东西。这是显而易见的。(任何Web服务的更一般的契约的一部分)客户端将接收400和 * 一些 * 有意义的错误消息。合同中的任何确切错误信息。
你也可以给整个RestController类或每个方法添加类似的东西:

@ApiResponse(responseCode = "400", 
    content = @Content(
        mediaType = MediaType.APPLICATION_JSON_VALUE, 
        schema = @Schema(implementation = MyError.class),
        examples = @ExampleObject(value = "{\"code\": \"INVALID_INPUT\", \"message\": \"Currency USD not supported\" }")
    )
)
@ApiResponse(responseCode = "403",
    content = @Content(
        mediaType = MediaType.APPLICATION_JSON_VALUE,
        schema = @Schema(implementation = MyError.class),
        examples = @ExampleObject(value = "{\"code\": \"FORBIDDEN\", \"message\": \"Forbidden access to something\" }")
    )
)
@ApiResponse(responseCode = "404", 
    content = @Content(
        mediaType = MediaType.APPLICATION_JSON_VALUE,
        schema = @Schema(implementation = MyError.class),
        examples = @ExampleObject(value = "{\"code\": \"RESOURCE_NOT_FOUND\", \"message\": \"User not found\" }")
    )
)
@ApiResponse(responseCode = "500",
    content = @Content(
        mediaType = MediaType.APPLICATION_JSON_VALUE, 
        schema = @Schema(implementation = MyError.class),
        examples = @ExampleObject(value = "{\"code\": \"EXTERNAL_API_CALL_ERROR\", \"message\": \"Central register API not available\" }")))

字符串

相关问题