【问题标题】:How to model OneOf in Flask Restx?如何在 Flask Restx 中建模 OneOf?
【发布时间】:2020-10-06 17:58:13
【问题描述】:

我有以下架构文档

{
    "$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)
)

【问题讨论】:

    标签: python flask-restx


    【解决方案1】:

    试试这个

    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-24
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 2020-12-03
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多