简体   繁体   中英

How to model OneOf in Flask Restx?

I have the following schema doc

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

and I'm trying to create the flask restx model, but I'm not sure how to model the required OneOf fields?

I think the following will work, but then it ignores the oneof requirement.

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)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM