def test_removing_readonly_prop_even_if_keeping(): schema = { "type": 'object', "properties": { "prop1": { "type": 'string', "readOnly": True }, "prop2": { "type": 'string', } } } options = {"removeReadOnly": True, "keepNotSupported": ['readOnly']} result = convert(schema, options) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "properties": { "prop2": { "type": 'string', } } } assert result == expected
def test_handling_additional_properties_with_one_of_patternProperty_types(): schema = { "type": 'object', "additionalProperties": { "type": 'number' }, 'x-patternProperties': { '^[a-z]*$': { "type": 'string' }, '^[A-Z]*$': { "type": 'number' } } } result = convert(schema, {"supportPatternProperties": True}) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "additionalProperties": False, "patternProperties": { '^[a-z]*$': { "type": 'string' }, '^[A-Z]*$': { "type": 'number' } } } assert result == expected
def test_not_removing_writeonly_props_by_default(): schema = { "type": 'object', "required": ['prop1', 'prop2'], "properties": { "prop1": { "type": 'string', "writeOnly": True }, "prop2": { "type": 'string', } } } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "required": ['prop1', 'prop2'], "properties": { "prop1": { "type": 'string', }, "prop2": { "type": 'string', } } } assert result == expected
def test_properties(): schema = { "type": 'object', "required": ['bar'], "properties": { "foo": { "type": 'string', "example": '2017-01-01T12:34:56Z' }, "bar": { "type": 'string', "nullable": True } } } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "required": ['bar'], "properties": { "foo": { "type": 'string', }, "bar": { "type": ['string', 'null'] } } } assert result == expected
def test_removing_readonly_from_required(): schema = { "type": 'object', "required": ['prop1', 'prop2'], "properties": { "prop1": { "type": 'string', "readOnly": True }, "prop2": { "type": 'string', } } } result = convert(schema, {"removeReadOnly": True}) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "required": ['prop2'], "properties": { "prop2": { "type": 'string', } } } assert result == expected
def test_not_supporting_patternProperties_by_default(): schema = { "type": 'object', "additionalProperties": { "type": 'string' }, 'x-patternProperties': { '^[a-z]*$': { "type": 'string' } } } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "additionalProperties": { "type": 'string' }, 'x-patternProperties': { '^[a-z]*$': { "type": 'string' } } } assert result == expected
def test_setting_custom_pattern_properties_handler(): schema = { "type": 'object', "additionalProperties": { "type": 'string' }, 'x-patternProperties': { '^[a-z]*$': { "type": 'string' } } } def handler(sch): sch['patternProperties'] = False return sch options = { "supportPatternProperties": True, "patternPropertiesHandler": handler, } result = convert(schema, options) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "additionalProperties": { "type": 'string' }, "patternProperties": False } assert result == expected
def test_complex_schema_in_place(): schema = get_schema_file('schema-1.json') result = convert(schema, {'cloneSchema': False}) expected = get_schema_file('schema-1-expected.json') assert schema is result assert schema == expected assert result == expected
def test_plain_string_is_untouched(): schema = {"type": 'string'} result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'string' } assert result == expected
def test_remove_example_by_default(): schema = {"type": 'string', "example": 'foo'} result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'string' } assert result == expected
def test_remove_deprecated_by_default(): schema = {"type": 'string', "deprecated": True} result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'string' } assert result == expected
def test_retaining_custom_formats(): schema = {"type": 'string', "format": 'email'} result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'string', "format": 'email' } assert result == expected
def test_handling_date(): schema = {"type": 'string', "format": 'date'} result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'string', "format": 'date' } assert result == expected schema = {"type": 'string', "format": 'date'} result = convert(schema, {'dateToDateTime': True}) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'string', "format": 'date-time' } assert result == expected
def test_valid_types(): types = ['integer', 'number', 'string', 'boolean', 'object', 'array'] for ttype in types: schema = {'type': ttype} result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": ttype, } assert result == expected
def test_converts_types_in_not_2(): schema = {"not": {"type": 'string', "format": 'password', "minLength": 8}} result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "not": { "type": 'string', "format": 'password', "minLength": 8 } } assert result == expected
def test_deep_schema(): schema = { "type": 'object', "required": ['prop1', 'prop2'], "properties": { "prop1": { "type": 'string', "readOnly": True }, "prop2": { "allOf": [ { "type": 'object', "required": ['prop3'], "properties": { "prop3": { "type": 'object', "readOnly": True } } }, { "type": 'object', "properties": { "prop4": { "type": 'object', "readOnly": True } } }, ] } } } result = convert(schema, {"removeReadOnly": True}) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "required": ['prop2'], "properties": { "prop2": { "allOf": [ { "type": 'object' }, { "type": 'object' }, ] } } } assert result == expected
def test_handling_additionalProperties_with_non_matching_objects(): schema = { "type": 'object', "additionalProperties": { "type": 'object', "properties": { "test": { "type": 'string' } } }, 'x-patternProperties': { '^[a-z]*$': { "type": 'string' }, '^[A-Z]*$': { "type": 'object', "properties": { "test": { "type": 'integer' } } } } } result = convert(schema, {"supportPatternProperties": True}) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "additionalProperties": { "type": 'object', "properties": { "test": { "type": 'string' } } }, "patternProperties": { '^[a-z]*$': { "type": 'string' }, '^[A-Z]*$': { "type": 'object', "properties": { "test": { "type": 'integer' } } } } } assert result == expected
def test_clone_schema_true(): """Test that the schema is clone when cloneSchema is True""" schema = { 'type': 'string', 'nullable': True, } result = convert(schema, {"cloneSchema": True}) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': ['string', 'null'], } assert result == expected assert schema is not result
def test_clone_schema_by_default(): """Test the the default behavior is to clone the schema""" schema = { 'type': 'string', 'nullable': True, } result = convert(schema) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': ['string', 'null'], } assert result == expected assert schema is not result
def test_handles_nullable(): schema = { "type": 'string', "nullable": True, } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": ['string', 'null'], } assert result == expected schema = { "type": 'string', "nullable": False, } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'string' } assert result == expected
def test_handles_integer_types(): schema = { "type": 'integer', } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'integer', } assert result == expected schema = { "type": 'integer', "format": 'int32', } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'integer', "format": 'int32', } assert result == expected
def test_clone_schema_with_duplicate_references(): dupe = { "oneOf": [{ "type": "string", "example": "cidr", "nullable": True, }, { "type": "string", "enum": ["alias", "policy"] }] } schema = { "type": "object", "properties": { "first": dupe, "second": dupe, } } result = convert(schema) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', "type": "object", "properties": { "first": { "oneOf": [ { "type": ["string", "null"] }, { "type": "string", "enum": ["alias", "policy"] }, ], }, "second": { "oneOf": [ { "type": ["string", "null"] }, { "type": "string", "enum": ["alias", "policy"] }, ], }, } } assert schema is not result assert result == expected assert result['properties']['first'] is not result['properties']['second']
def test_clone_schema_false(): """Test that the schema is modified in place when cloneSchema is False""" schema = { 'type': 'string', 'nullable': True, } result = convert(schema, {"cloneSchema": False}) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': ['string', 'null'], } assert schema == expected assert result == expected assert schema is result
def test_remove_discriminator_by_default(): schema = { "oneOf": [{ "type": 'object', "required": ['foo'], "properties": { "foo": { "type": 'string' } } }, { "type": 'object', "required": ['foo'], "properties": { "foo": { "type": 'string' } } }], "discriminator": { "propertyName": 'foo' } } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "oneOf": [{ "type": 'object', "required": ['foo'], "properties": { "foo": { "type": 'string' } } }, { "type": 'object', "required": ['foo'], "properties": { "foo": { "type": 'string' } } }], } assert result == expected
def test_deleting_properties_if_empty(): schema = { "type": 'object', "required": ['prop1'], "properties": { "prop1": { "type": 'string', "readOnly": True } } } result = convert(schema, {"removeReadOnly": True}) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object' } assert result == expected
def test_iterate_oneOfs(): schema = { "oneOf": [{ "type": 'object', "required": ['foo'], "properties": { "foo": { "type": 'integer' } } }, { "oneOf": [{ "type": 'object', "properties": { "bar": { "type": 'number' } } }] }] } result = convert(schema) expected = { "$schema": "http://json-schema.org/draft-04/schema#", "oneOf": [{ "type": "object", "required": ["foo"], "properties": { "foo": { "type": "integer", } } }, { "oneOf": [{ "type": "object", "properties": { "bar": { "type": "number", } } }] }] } assert result == expected
def test_retaining_fields(): schema = { "type": 'object', "properties": { "readOnly": { "type": 'string', "readOnly": True, "example": 'foo' }, "anotherProp": { "type": 'object', "properties": { "writeOnly": { "type": 'string', "writeOnly": True } } } }, "discriminator": 'bar' } options = {"keepNotSupported": ['readOnly', 'discriminator']} result = convert(schema, options) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "properties": { "readOnly": { "type": 'string', "readOnly": True, }, "anotherProp": { "type": 'object', "properties": { "writeOnly": { "type": 'string', } } } }, "discriminator": 'bar' } assert result == expected
def test_items(): schema = { "type": 'array', "items": { "type": 'string', "format": 'date-time', "example": '2017-01-01T12:34:56Z' } } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'array', "items": { "type": 'string', "format": 'date-time' } } assert result == expected
def test_invalid_types(): schema = {"type": "dateTime"} with pytest.raises(InvalidTypeError) as e: convert(schema) assert 'Type "dateTime" is not a valid type' in str(e) schema = {"type": "foo"} with pytest.raises(InvalidTypeError) as e: convert(schema) assert 'Type "foo" is not a valid type' in str(e) schema = get_schema_file('schema-2-invalid-type.json') with pytest.raises(InvalidTypeError) as e: convert(schema) assert 'Type "invalidtype" is not a valid type'
def test_remove_readonly_by_default(): schema = { "type": 'object', "properties": { "readOnly": { "type": 'string', "readOnly": True } } } result = convert(schema) expected = { "$schema": 'http://json-schema.org/draft-04/schema#', "type": 'object', "properties": { "readOnly": { "type": 'string' } } } assert result == expected