|
16 | 16 | VALIDATE_MINIMUM_ERROR,
|
17 | 17 | VALIDATE_MINIMUM_NUMBER_OF_PROPERTIES_ERROR,
|
18 | 18 | VALIDATE_MULTIPLE_OF_ERROR,
|
| 19 | + VALIDATE_TYPE_ERROR, |
19 | 20 | )
|
20 | 21 | from openapi_tester.validators import VALIDATOR_MAP
|
21 | 22 | from tests import (
|
@@ -141,12 +142,68 @@ def test_additional_properties_allowed():
|
141 | 142 | tester.test_schema_section(schema, {"oneKey": "test", "twoKey": "test2"})
|
142 | 143 |
|
143 | 144 |
|
| 145 | +def test_additional_properties_specified_as_empty_object_allowed(): |
| 146 | + schema = {"type": "object", "additionalProperties": {}, "properties": {"oneKey": {"type": "string"}}} |
| 147 | + tester.test_schema_section(schema, {"oneKey": "test", "twoKey": "test2"}) |
| 148 | + |
| 149 | + |
144 | 150 | def test_additional_properties_not_allowed_by_default():
|
145 | 151 | schema = {"type": "object", "properties": {"oneKey": {"type": "string"}}}
|
146 | 152 | with pytest.raises(DocumentationError, match=VALIDATE_EXCESS_RESPONSE_KEY_ERROR[:90]):
|
147 | 153 | tester.test_schema_section(schema, {"oneKey": "test", "twoKey": "test2"})
|
148 | 154 |
|
149 | 155 |
|
| 156 | +def test_string_dictionary_specified_as_additional_properties_allowed(): |
| 157 | + schema = {"type": "object", "additionalProperties": {"type": "string"}, "properties": {"key_1": {"type": "string"}}} |
| 158 | + tester.test_schema_section(schema, {"key_1": "value_1", "key_2": "value_2", "key_3": "value_3"}) |
| 159 | + |
| 160 | + |
| 161 | +def test_string_dictionary_with_non_string_value_fails_validation(): |
| 162 | + schema = {"type": "object", "additionalProperties": {"type": "string"}, "properties": {"key_1": {"type": "string"}}} |
| 163 | + expected_error_message = VALIDATE_TYPE_ERROR.format(article="a", type="string", received=123) |
| 164 | + with pytest.raises(DocumentationError, match=expected_error_message): |
| 165 | + tester.test_schema_section(schema, {"key_1": "value_1", "key_2": 123, "key_3": "value_3"}) |
| 166 | + |
| 167 | + |
| 168 | +def test_object_dictionary_specified_as_additional_properties_allowed(): |
| 169 | + schema = { |
| 170 | + "type": "object", |
| 171 | + "properties": {"key_1": {"type": "string"}}, |
| 172 | + "additionalProperties": { |
| 173 | + "type": "object", |
| 174 | + "properties": {"key_2": {"type": "string"}, "key_3": {"type": "number"}}, |
| 175 | + }, |
| 176 | + } |
| 177 | + tester.test_schema_section( |
| 178 | + schema, |
| 179 | + { |
| 180 | + "key_1": "value_1", |
| 181 | + "some_extra_key": {"key_2": "value_2", "key_3": 123}, |
| 182 | + "another_extra_key": {"key_2": "value_4", "key_3": 246}, |
| 183 | + }, |
| 184 | + ) |
| 185 | + |
| 186 | + |
| 187 | +def test_additional_properties_schema_not_validated_in_main_properties(): |
| 188 | + schema = { |
| 189 | + "type": "object", |
| 190 | + "properties": {"key_1": {"type": "string"}}, |
| 191 | + "additionalProperties": { |
| 192 | + "type": "object", |
| 193 | + "properties": {"key_2": {"type": "string"}, "key_3": {"type": "number"}}, |
| 194 | + }, |
| 195 | + } |
| 196 | + expected_error_message = VALIDATE_TYPE_ERROR.format(article="an", type="object", received='"value_2"') |
| 197 | + with pytest.raises(DocumentationError, match=expected_error_message): |
| 198 | + tester.test_schema_section(schema, {"key_1": "value_1", "key_2": "value_2", "key_3": 123}) |
| 199 | + |
| 200 | + |
| 201 | +def test_invalid_additional_properties_raises_schema_error(): |
| 202 | + schema = {"type": "object", "properties": {"key_1": {"type": "string"}}, "additionalProperties": 123} |
| 203 | + with pytest.raises(OpenAPISchemaError, match="Invalid additionalProperties type"): |
| 204 | + tester.test_schema_section(schema, {"key_1": "value_1", "key_2": "value_2"}) |
| 205 | + |
| 206 | + |
150 | 207 | def test_pattern_validation():
|
151 | 208 | """The a regex pattern can be passed to describe how a string should look"""
|
152 | 209 | schema = {"type": "string", "pattern": r"^\d{3}-\d{2}-\d{4}$"}
|
|
0 commit comments