Esempio n. 1
0
    def test_flatten_anyof_with_title(self):
        input_ = {
            "anyOf": [
                {
                    "type": "object",
                    "title": "a",
                    "properties": {
                        "a": {
                            "type": "string"
                        }
                    },
                },
                {
                    "type": "object",
                    "title": "b",
                    "properties": {
                        "b": {
                            "type": "string"
                        }
                    },
                },
            ],
            "title":
            "union",
        }

        expected_schema = {"$ref": "#/definitions/union"}

        expected_definitions = {
            "a": {
                "type": "object",
                "title": "a",
                "properties": {
                    "a": {
                        "type": "string"
                    }
                },
            },
            "b": {
                "type": "object",
                "title": "b",
                "properties": {
                    "b": {
                        "type": "string"
                    }
                },
            },
            "union": {
                "anyOf": [{
                    "$ref": "#/definitions/a"
                }, {
                    "$ref": "#/definitions/b"
                }],
                "title": "union",
            },
        }

        schema, definitions = flatten(input_, base="#/definitions")
        self.assertEqual(schema, expected_schema)
        self.assertEqual(definitions, expected_definitions)
Esempio n. 2
0
    def test_flatten_subschemas(self):
        input_ = {
            "anyOf": [
                {"type": "null"},
                {
                    "type": "object",
                    "title": "a",
                    "properties": {"a": {"type": "string"}},
                },
                {
                    "type": "array",
                    "title": "b",
                    "items": {
                        "type": "object",
                        "title": "c",
                        "properties": {"a": {"type": "string"}},
                    },
                },
                {
                    "anyOf": [
                        {
                            "type": "object",
                            "title": "d",
                            "properties": {"a": {"type": "string"}},
                        }
                    ]
                },
            ]
        }

        expected_schema = {
            "anyOf": [
                {"type": "null"},
                {"$ref": "#/definitions/a"},
                {"type": "array", "title": "b", "items": {"$ref": "#/definitions/c"}},
                {"anyOf": [{"$ref": "#/definitions/d"}]},
            ]
        }

        expected_definitions = {
            "a": {
                "type": "object",
                "title": "a",
                "properties": {"a": {"type": "string"}},
            },
            "c": {
                "type": "object",
                "title": "c",
                "properties": {"a": {"type": "string"}},
            },
            "d": {
                "type": "object",
                "title": "d",
                "properties": {"a": {"type": "string"}},
            },
        }

        schema, definitions = flatten(input_, base="#/definitions")
        self.assertEqual(schema, expected_schema)
        self.assertEqual(definitions, expected_definitions)
Esempio n. 3
0
    def test_flatten(self):
        input_ = {
            "type": "object",
            "title": "x",
            "properties": {
                "a": {
                    "type": "object",
                    "title": "y",
                    "properties": {"b": {"type": "integer"}},
                },
                "b": {"type": "string"},
            },
        }

        expected_schema = {"$ref": "#/definitions/x"}

        expected_definitions = {
            "x": {
                "type": "object",
                "title": "x",
                "properties": {
                    "a": {"$ref": "#/definitions/y"},
                    "b": {"type": "string"},
                },
            },
            "y": {
                "type": "object",
                "title": "y",
                "properties": {"b": {"type": "integer"}},
            },
        }

        schema, definitions = flatten(input_, base="#/definitions")
        self.assertEqual(schema, expected_schema)
        self.assertEqual(definitions, expected_definitions)
    def test_flatten_array(self):
        input_ = {
            "type": "array",
            "title": "x",
            "items": {
                "type": "array",
                "title": "y",
                "items": {
                    "type": "object",
                    "title": "z",
                    "properties": {
                        "a": {
                            "type": "integer"
                        }
                    },
                },
            },
        }

        expected_schema = {
            "type": "array",
            "title": "x",
            "items": {
                "type": "array",
                "title": "y",
                "items": {
                    "$ref": "#/definitions/z"
                },
            },
        }

        expected_definitions = {
            "z": {
                "type": "object",
                "title": "z",
                "properties": {
                    "a": {
                        "type": "integer"
                    }
                },
            }
        }

        schema, definitions = flatten(input_, base="#/definitions")
        self.assertEqual(schema, expected_schema)
        self.assertEqual(definitions, expected_definitions)