如何在 swagger 规范中表示十进制浮点? [英] How can I denote decimal floating point in swagger specs?
问题描述
我想在我的 api 文档中用 2 位表示小数,用 1 位表示小数.我正在使用 swagger 2.0,规范中是否有内置定义类型或任何其他圆形"参数,或者我唯一的选择是使用x-"扩展名?
I would like to denote decimal with 2 places and decimal with 1 place in my api documentation. I'm using swagger 2.0, Is there inbuilt defined type or any other 'round' parameter in the specs, or my only option is to use 'x-' extension?
推荐答案
OpenAPI (fka Swagger) 规范使用 JSON Schema 来描述数据类型.
OpenAPI (fka Swagger) Specification uses a subset of JSON Schema to describe the data types.
如果参数作为数字传递,您可以尝试使用 this Q& 中建议的 multipleOf
一个:
If the parameter is passed as a number, you can try using multipleOf
as suggested in 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
验证可能不可靠/a>.
Hovewer, multipleOf
validation against floating-point numbers can be unreliable due to floating-point math specifics.
如果您的号码作为字符串传递,您可以为所需的号码格式指定正则表达式 pattern
:
type: string
pattern: your_regex
在任何情况下,您也可以在 description
中口头记录任何限制.
In any case, you can also document any restrictions verbally in the description
.
这篇关于如何在 swagger 规范中表示十进制浮点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!