【问题标题】:How can I denote decimal floating point in swagger specs?如何在 swagger 规范中表示十进制浮点?
【发布时间】:2017-07-07 10:02:24
【问题描述】:

我想在我的 api 文档中用 2 位表示小数,用 1 位表示小数。我正在使用 swagger 2.0,规范中是否有内置定义类型或任何其他“圆形”参数,或者我唯一的选择是使用“x-”扩展名?

【问题讨论】:

    标签: swagger swagger-2.0 openapi


    【解决方案1】:

    OpenAPI (fka Swagger) 规范使用JSON Schema 的子集来描述数据类型。

    如果参数作为数字传递,您可以尝试使用multipleOf,如this Q&A中的建议:

    type: number
    multipleOf: 0.1     # up to 1 decimal place,  e.g. 4.2
    # multipleOf: 0.01  # up to 2 decimal places, e.g. 4.25
    

    但是,multipleOf 针对浮点数的验证可能不可靠,因为 floating-point math specifics


    如果您的号码作为字符串传递,您可以为所需的号码格式指定正则表达式 pattern

    type: string
    pattern: your_regex
    


    无论如何,您还可以在description 中口头记录任何限制。

    【讨论】:

    • 在 java 中,type: number 将转换为 BigDecimal
    猜你喜欢
    • 2012-08-10
    • 2021-08-08
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2016-01-01
    相关资源
    最近更新 更多