drf-yasg
我有一个用于生成swagger.json
文件的 Django(Django Rest Framework)网络服务。在模型中,我有几个枚举/选择字段用于多个地方。默认情况下,drf-yasg
为每个事件定义内联字段:
Choices = serializers.ChoiceField(choices=['a', 'b', 'c'])
class SomeObject(serializers.Serializer):
field_1 = Choices
field_2 = Choices
在 swagger 文件中生成以下定义:
{
"definitions": {
"SomeObject": {
"required": [ "field_1", "field_2" ],
"type": "object",
"properties": {
"field_1": {
"title": "Field 1",
"type": "string",
"enum": [ "a", "b", "c" ]
},
"field_2": {
"title": "Field 1",
"type": "string",
"enum": [ "a", "b", "c" ]
}
}
}
}
}
这是一个小问题,因为它使客户端代码生成工具生成每个枚举作为自己的类型,而不是重用定义。因此,我想创建一个像这样的 swaggerfile:
{
"definitions": {
"Choices": {
"title": "Field 1",
"type": "string",
"enum": [ "a", "b", "c" ]
},
"SomeObject": {
"required": [ "field_1", "field_2" ],
"type": "object",
"properties": {
"field_1": {
"$ref": "#/definitions/Choices"
},
"field_2": {
"$ref": "#/definitions/Choices"
}
}
}
}
}
是否可以在 Django Rest Framework 中启用此行为?