def test_flatten_combiners_resolve_types_object_by_default(combiner):
    # this should fail, since we declare an object type and string type
    # https://github.com/aws-cloudformation/aws-cloudformation-rpdk/issues/333
    ref = ("definitions", "obj")
    test_schema = {
        "typeName": "AWS::Valid::TypeName",
        "definitions": {
            "obj": {
                "properties": {
                    "Foo": {
                        "type": "object"
                    }
                }
            }
        },
        "properties": {
            "p": {
                combiner: [{
                    "type": "string"
                }, {
                    "$ref": fragment_encode(ref)
                }]
            }
        },
    }

    flattener = JsonSchemaFlattener(test_schema)
    flattener.flatten_schema()
    assert ref in flattener._schema_map
def test_flatten_combiners_no_clobber(combiner):
    # https://github.com/awslabs/aws-cloudformation-rpdk/pull/92#discussion_r231348534
    ref = ("properties", "p2", combiner, 0)
    test_schema = {
        "typeName": "AWS::Valid::TypeName",
        "properties": {
            "p1": {
                "$ref": fragment_encode(ref)
            },
            "p2": {
                combiner: [
                    {
                        "properties": {
                            "a2": {
                                "type": "integer"
                            }
                        }
                    },
                    {
                        "properties": {
                            "b1": {
                                "type": "integer"
                            }
                        }
                    },
                ]
            },
        },
    }

    flattener = JsonSchemaFlattener(test_schema)
    flattener.flatten_schema()
    assert ref in flattener._schema_map
def test_flattener_full_example():
    test_schema = resource_json(__name__, "data/area_definition.json")

    flattener = JsonSchemaFlattener(test_schema)
    flattened = flattener.flatten_schema()

    assert flattened == AREA_DEFINITION_FLATTENED
def test_flatten_combiners_resolve_types(combiner):
    ref_type = ("definitions", "obj_type")
    ref = ("definitions", "obj")
    test_schema = {
        "typeName": "AWS::Valid::TypeName",
        "definitions": {
            "obj_type": {
                "type": "object"
            },
            "obj": {
                "properties": {
                    "Foo": {
                        "type": "object"
                    }
                }
            },
        },
        "properties": {
            "p": {
                combiner: [
                    {
                        "type": "string"
                    },
                    {
                        "type": "integer"
                    },
                    {
                        "$ref": fragment_encode(ref_type)
                    },
                ]
            },
            "p2": {
                combiner: [{
                    "type": "string"
                }, {
                    "$ref": fragment_encode(ref)
                }]
            },
        },
    }

    flattener = JsonSchemaFlattener(test_schema)
    flattener.flatten_schema()

    assert ref in flattener._schema_map
def test_flatten_combiners_flattened_before_merge_failed_but_should_not(
        combiner):
    # this should not fail, since the refs are actually compatible with each other
    # https://github.com/aws-cloudformation/aws-cloudformation-rpdk/issues/333
    ref = ("definitions", "obj")
    ref2 = ("definitions", "obj2")
    test_schema = {
        "typeName": "AWS::Valid::TypeName",
        "definitions": {
            "obj": {
                "properties": {
                    "a": {
                        "type": "object"
                    }
                }
            },
            "obj2": {
                "properties": {
                    "a": {
                        "type": "object"
                    }
                }
            },
        },
        "properties": {
            "p": {
                combiner: [
                    {
                        "$ref": fragment_encode(ref)
                    },
                    {
                        "$ref": fragment_encode(ref2)
                    },
                ]
            }
        },
    }

    flattener = JsonSchemaFlattener(test_schema)
    with pytest.raises(ConstraintError) as excinfo:
        flattener.flatten_schema()
    assert "declared multiple values for '$ref'" in str(excinfo.value)
def test_flattener_double_processed_refs():
    """The flattener uses references to indicate objects, but these
    references are not JSON pointer URI fragments. In some cases, such references
    may be fed back into the flattener, like if object B is nested inside
    object A with a combiner (``oneOf``).

    When the combiner is processed, B is (correctly) flattened into a distinct
    object and placed in the schema map. A reference to B is returned, as a tuple
    (``{'$ref': ('properties', 'A', 'oneOf', 0, 'properties', 'B')}``), from
    ``_flatten_object_type``. So when the combiners are flattened, the result is:
    ``{'properties': {'B': {'$ref': ('properties', 'A', 'oneOf', 0, 'properties',
    'B')}}}``.

    So when `_flatten_object_type` hits the `$ref`, it's important that
    ``_flatten_ref_type`` understands tuples, which is also tested, so this test
    is for showing that such a situation occurs in a normal, well-formed schema.
    """
    test_schema = resource_json(__name__,
                                "data/valid_refs_flattened_twice.json")

    flattener = JsonSchemaFlattener(test_schema)
    flattener.flatten_schema()
Esempio n. 7
0
def test_flatten_combiners_resolve_types(combiner):
    ref = ("definitions", "obj")
    test_schema = {
        "typeName": "AWS::Valid::TypeName",
        "definitions": {
            "obj": {
                "type": "object"
            }
        },
        "properties": {
            "p": {
                combiner: [{
                    "type": "string"
                }, {
                    "$ref": fragment_encode(ref)
                }]
            }
        },
    }

    flattener = JsonSchemaFlattener(test_schema)
    with pytest.raises(ConstraintError) as excinfo:
        flattener.flatten_schema()
    assert "declared multiple values for 'type'" in str(excinfo.value)
Esempio n. 8
0
def test_circular_reference(test_schema):
    flattener = JsonSchemaFlattener(test_schema)
    with pytest.raises(CircularRefError) as excinfo:
        flattener.flatten_schema()
    assert "#/properties/a" in str(excinfo.value)