Как смоделировать OneOf в Flask Restx?

У меня есть следующая схема документа

{
    "$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"]
}

и я пытаюсь создать модель flask 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)
)

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)
    }
)
Другие вопросы по тегам