首页 > 解决方案 > 使用 SpringDoc OpenAPI,Double 和其他非原始数字不会被标记为可为空

问题描述

我们将 SpringDoc OpenAPI 与 SpingBoot 2.3 和 WebFlux 一起使用,并在其之上拥有 Swagger UI(更准确地说是 springdoc-openapi-webflux-ui 1.4.8)。

最近,我们注意到无法区分可空/可选数字 (Double) 和原始数字 (double)。

例如下面的类

@With
@Value
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CurrencyBalanceDto {
    double balance;
    Double hardCapping;
}

产生以下 OpenAPI Schema

"CurrencyBalanceDto": {
    "type": "object",
    "properties": {
        "balance": {
            "type": "number",
            "format": "double"
        },
        "hardCapping": {
            "type": "number",
            "format": "double"
        }
    }
}

基于https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#schemaNullable,我希望 hardCapping 属性具有"nullable": true

标签: spring-bootswaggeropenapispringdocspringdoc-openapi-ui

解决方案


提供的链接中的文档引用:

nullable boolean 允许为定义的模式发送空值。默认值为 false

所以默认情况下,所有字段都不能为空。您需要通过使用注释或使用明确地将字段设置为可@Null@Nullable@Schema(nullable = true)


推荐阅读