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

pieyvz9o  于 2024-01-08  发布在  Spring
关注(0)|答案(1)|浏览(252)

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

  1. <dependency>
  2. <groupId>org.springdoc</groupId>
  3. <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>

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

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


为该方法生成以下文档

  1. "/page": {
  2. "get": {
  3. ...
  4. "parameters": [
  5. {
  6. "name": "size",
  7. "in": "query",
  8. "required": true,
  9. "schema": {
  10. "minimum": 1,
  11. "type": "integer",
  12. "format": "int32"
  13. }
  14. },
  15. {
  16. "name": "number",
  17. "in": "query",
  18. "required": true,
  19. "schema": {
  20. "minimum": 0,
  21. "type": "integer",
  22. "format": "int32"
  23. }
  24. }
  25. ],
  26. ...


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

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


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

  1. @GetMapping("page")
  2. @ResponseStatus(HttpStatus.OK)
  3. List<RecordInfoDto> getPage(@RequestParam("size")
  4. @io.swagger.v3.oas.annotations.Parameter(name = "size",
  5. required = true,
  6. in = ParameterIn.QUERY,
  7. schema = @Schema(minimum = "100"))
  8. @jakarta.validation.constraints.Min(value = 1, message = "size must be more than 0")
  9. int size,
  10. @RequestParam("number")
  11. @jakarta.validation.constraints.Min(value = 0, message = "number must be non-negative")
  12. 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类或每个方法添加类似的东西:

  1. @ApiResponse(responseCode = "400",
  2. content = @Content(
  3. mediaType = MediaType.APPLICATION_JSON_VALUE,
  4. schema = @Schema(implementation = MyError.class),
  5. examples = @ExampleObject(value = "{\"code\": \"INVALID_INPUT\", \"message\": \"Currency USD not supported\" }")
  6. )
  7. )
  8. @ApiResponse(responseCode = "403",
  9. content = @Content(
  10. mediaType = MediaType.APPLICATION_JSON_VALUE,
  11. schema = @Schema(implementation = MyError.class),
  12. examples = @ExampleObject(value = "{\"code\": \"FORBIDDEN\", \"message\": \"Forbidden access to something\" }")
  13. )
  14. )
  15. @ApiResponse(responseCode = "404",
  16. content = @Content(
  17. mediaType = MediaType.APPLICATION_JSON_VALUE,
  18. schema = @Schema(implementation = MyError.class),
  19. examples = @ExampleObject(value = "{\"code\": \"RESOURCE_NOT_FOUND\", \"message\": \"User not found\" }")
  20. )
  21. )
  22. @ApiResponse(responseCode = "500",
  23. content = @Content(
  24. mediaType = MediaType.APPLICATION_JSON_VALUE,
  25. schema = @Schema(implementation = MyError.class),
  26. examples = @ExampleObject(value = "{\"code\": \"EXTERNAL_API_CALL_ERROR\", \"message\": \"Central register API not available\" }")))

字符串

展开查看全部

相关问题