我希望在我的API文档中用2位小数和1位小数表示十进制数。 我正在使用Swagger 2.0,规范中是否有内置定义类型或任何其他“round”参数,还是我唯一的选择是使用“x-”扩展?
我希望在我的API文档中用2位小数和1位小数表示十进制数。 我正在使用Swagger 2.0,规范中是否有内置定义类型或任何其他“round”参数,还是我唯一的选择是使用“x-”扩展?
OpenAPI规范 (前身为Swagger) 使用JSON Schema 的子集来描述数据类型。
如果参数以数字形式传递,您可以尝试使用multipleOf
,如此问答所建议的:
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
验证可能不够可靠。
如果您的数字以字符串形式传递,则可以为所需的数字格式指定一个正则表达式 pattern
:
type: string
pattern: your_regex
无论如何,您也可以在description
中口头记录任何限制。
@Getter
@Setter
public class MyWrapper {
@Digits(integer = 1, fraction = 8)
private BigDecimal bigDecimal;
}
openapi-generator-maven-plugin
。 <importMappings>
<importMapping>MyWrapper=my.package.MyWrapper</importMapping>
</importMappings>
<typeMappings>
<typeMapping>number+digits=MyWrapper</typeMapping>
</typeMappings>
properties:
myProperty:
type: number
format: digits
type: number
将转换为BigDecimal
。 - Hariom Yadav