python-openapi/openapi-schema-validator

test_array_prefixitems_invalid[value0] failing on 0.6.2

mweinelt opened this issue · 2 comments

Hi, we're seeing the following test fail. Our environment is

  • Python 3.11.6
  • jsonschema 4.20.0
  • jsonschema-specifications 2023.11.2
  • rfc3339-validator 0.1.4
openapi-schema-validator> ______ TestOAS31ValidatorValidate.test_array_prefixitems_invalid[value0] _______
openapi-schema-validator> 
openapi-schema-validator> self = <test_validators.TestOAS31ValidatorValidate object at 0x7ffff5d0f710>
openapi-schema-validator> validator_class = <class 'jsonschema.validators.create.<locals>.Validator'>
openapi-schema-validator> value = [1600, 'Pennsylvania', 'Avenue', 'NW', 'Washington']
openapi-schema-validator> 
openapi-schema-validator>     @pytest.mark.parametrize(
openapi-schema-validator>         "value",
openapi-schema-validator>         [
openapi-schema-validator>             [1600, "Pennsylvania", "Avenue", "NW", "Washington"],
openapi-schema-validator>         ],
openapi-schema-validator>     )
openapi-schema-validator>     def test_array_prefixitems_invalid(self, validator_class, value):
openapi-schema-validator>         schema = {
openapi-schema-validator>             "type": "array",
openapi-schema-validator>             "prefixItems": [
openapi-schema-validator>                 {"type": "number"},
openapi-schema-validator>                 {"type": "string"},
openapi-schema-validator>                 {"enum": ["Street", "Avenue", "Boulevard"]},
openapi-schema-validator>                 {"enum": ["NW", "NE", "SW", "SE"]},
openapi-schema-validator>             ],
openapi-schema-validator>             "items": False,
openapi-schema-validator>         }
openapi-schema-validator>         validator = validator_class(
openapi-schema-validator>             schema,
openapi-schema-validator>             format_checker=oas31_format_checker,
openapi-schema-validator>         )
openapi-schema-validator>     
openapi-schema-validator>         with pytest.raises(ValidationError) as excinfo:
openapi-schema-validator>             validator.validate(value)
openapi-schema-validator>     
openapi-schema-validator>         error = "Expected at most 4 items, but found 5"
openapi-schema-validator> >       assert error in str(excinfo.value)
openapi-schema-validator> E       assert 'Expected at most 4 items, but found 5' in "Expected at most 4 items but found 1 extra: 'Washington'\n\nFailed validating 'items' in schema:\n    {'items': False,\n     'prefixItems': [{'type': 'number'},\n                     {'type': 'string'},\n                     {'enum': ['Street', 'Avenue', 'Boulevard']},\n                     {'enum': ['NW', 'NE', 'SW', 'SE']}],\n     'type': 'array'}\n\nOn instance:\n    [1600, 'Pennsylvania', 'Avenue', 'NW', 'Washington']"
openapi-schema-validator> E        +  where "Expected at most 4 items but found 1 extra: 'Washington'\n\nFailed validating 'items' in schema:\n    {'items': False,\n     'prefixItems': [{'type': 'number'},\n                     {'type': 'string'},\n                     {'enum': ['Street', 'Avenue', 'Boulevard']},\n                     {'enum': ['NW', 'NE', 'SW', 'SE']}],\n     'type': 'array'}\n\nOn instance:\n    [1600, 'Pennsylvania', 'Avenue', 'NW', 'Washington']" = str(<ValidationError: "Expected at most 4 items but found 1 extra: 'Washington'">)
openapi-schema-validator> E        +    where <ValidationError: "Expected at most 4 items but found 1 extra: 'Washington'"> = <ExceptionInfo <ValidationError: "Expected at most 4 items but found 1 extra: 'Washington'"> tblen=2>.value
openapi-schema-validator> 
openapi-schema-validator> error      = 'Expected at most 4 items, but found 5'
openapi-schema-validator> excinfo    = <ExceptionInfo <ValidationError: "Expected at most 4 items but found 1 extra: 'Washington'"> tblen=2>
openapi-schema-validator> schema     = {'items': False,
openapi-schema-validator>  'prefixItems': [{'type': 'number'},
openapi-schema-validator>                  {'type': 'string'},
openapi-schema-validator>                  {'enum': ['Street', 'Avenue', 'Boulevard']},
openapi-schema-validator>                  {'enum': ['NW', 'NE', 'SW', 'SE']}],
openapi-schema-validator>  'type': 'array'}
openapi-schema-validator> self       = <test_validators.TestOAS31ValidatorValidate object at 0x7ffff5d0f710>
openapi-schema-validator> validator  = Validator(schema={'items': False, 'prefixItems': [{'type': 'number'}, {'type': 'string'}, {'enum': ['Street', 'Avenue', 'Boulevard']}, {'enum': ['NW', 'NE', 'SW', 'SE']}], 'type': 'array'}, format_checker=<FormatChecker checkers=['date', 'date-time', 'double', 'email', 'float', 'idn-email', 'int32', 'int64', 'ipv4', 'ipv6', 'password', 'regex', 'time', 'uuid']>)
openapi-schema-validator> validator_class = <class 'jsonschema.validators.create.<locals>.Validator'>
openapi-schema-validator> value      = [1600, 'Pennsylvania', 'Avenue', 'NW', 'Washington']
openapi-schema-validator> 
openapi-schema-validator> tests/integration/test_validators.py:867: AssertionError

The failure is caused by newer jsonschema. In python-jsonschema/jsonschema@8cff13d, the error message is changed. To get through this test, I just changed the expected error message with:

diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py
index 07bc4df..5663c4c 100644
--- a/tests/integration/test_validators.py
+++ b/tests/integration/test_validators.py
@@ -863,5 +863,5 @@ class TestOAS31ValidatorValidate(BaseTestOASValidatorValidate):
         with pytest.raises(ValidationError) as excinfo:
             validator.validate(value)
 
-        error = "Expected at most 4 items, but found 5"
+        error = "Expected at most 4 items but found 1 extra"
         assert error in str(excinfo.value)

Not sure if the maintainer wants to support both older and newer error messages or not.

Have the same issue.