如何在 swagger 规范中表示十进制浮点数?
How can I denote decimal floating point in swagger specs?
我想在我的 api 文档中用 2 位表示小数,用 1 位表示小数。我正在使用 swagger 2.0,规范中是否有内置定义类型或任何其他 'round' 参数,或者我唯一的选择是使用 'x-' 扩展?
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
.
中口头记录任何限制
我想在我的 api 文档中用 2 位表示小数,用 1 位表示小数。我正在使用 swagger 2.0,规范中是否有内置定义类型或任何其他 'round' 参数,或者我唯一的选择是使用 'x-' 扩展?
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
.