7

我有以下架构文档

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "foo",
    "definitions": {
        "stuff": {
            "type": "object",
            "properties": {
                "id": {
                    "description": "the id",
                    "type": "string"
                },
                "type": {
                    "description": "the type",
                    "type": "string"
                }
            },
            "oneOf": [{
                "required": ["id"]
            }, {
                "required": ["type"]
            }],
        },
    },
    "type": "object",
    "properties": {
        "bar": {
            "description": "blah",
            "type": "object",
            "additionalProperties": {
                "$ref": "#/definitions/stuff"
            }
        }
    },
    "required": ["bar"]
}

我正在尝试创建烧瓶 restx 模型,但我不确定如何为所需的 OneOf 字段建模?

我认为以下将起作用,但随后它忽略了 oneof 要求。

stuff_model = (
    "Stuff Model",
    "id": fields.String(description="the id", required=False),
    "type": fields.String(description="the type", required=False)
)

bar_model = (
    "Bar Model",
    "bar": fields.Nested(stuff_model, required=True)
)
4

1 回答 1

0

尝试这个

one_of = api.model(
    "OneOf",
    {
        "required": fields.List(fields.String)
    }
)
stuff_model = api.model(
    "Stuff Model",
    {
        "id": fields.String(description="the id", required=False),
        "type": fields.String(description="the type", required=False),
        "oneOf": fields.Nested(one_of, as_list=True)
    }
)
于 2021-11-09T14:06:04.730 回答