python-openapi / openapi-core

Openapi-core is a Python library that adds client-side and server-side support for the OpenAPI v3.0 and OpenAPI v3.1 specification.
BSD 3-Clause "New" or "Revised" License
308 stars 132 forks source link

KeyError: 'items' validating [] against schema {} #380

Closed andersk closed 1 year ago

andersk commented 2 years ago

Validating an array against the empty schema {} gives KeyError: 'items'. A git bisect shows this was introduced by commit 35f8e28157ca5d2f1ddd9f5684c4e6c0ddf75d6c (#318).

from openapi_core import create_spec
from openapi_core.testing import MockRequest, MockResponse
from openapi_core.validation.response.validators import ResponseValidator

spec = create_spec(
    {
        "openapi": "3.0.3",
        "info": {"version": "0", "title": ""},
        "servers": [{"url": "http://localhost"}],
        "paths": {
            "/test": {
                "get": {
                    "responses": {
                        "200": {
                            "description": "",
                            "content": {"application/json": {"schema": {}}},
                        }
                    },
                }
            }
        },
    }
)
response_validator = ResponseValidator(spec)
request = MockRequest("http://localhost", "get", "/test")
response = MockResponse("[]")
response_validator.validate(request, response)
Traceback (most recent call last):
  File "/home/anders/.cache/pypoetry/virtualenvs/openapi-core-XEMBi3Y7-py3.9/lib/python3.9/site-packages/dictpath/paths.py", line 138, in open
    yield self._content_cached
AttributeError: 'SpecPath' object has no attribute '_content_cached'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/anders/python/openapi-core/items-bug.py", line 27, in <module>
    response_validator.validate(request, response)
  File "/home/anders/python/openapi-core/openapi_core/validation/response/validators.py", line 185, in validate
    data, data_errors = self._get_data(response, operation_response)
  File "/home/anders/python/openapi-core/openapi_core/validation/response/validators.py", line 76, in _get_data
    data = self._unmarshal(schema, casted)
  File "/home/anders/python/openapi-core/openapi_core/validation/validators.py", line 74, in _unmarshal
    return unmarshaller(value)
  File "/home/anders/python/openapi-core/openapi_core/unmarshalling/schemas/unmarshallers.py", line 48, in __call__
    return self.unmarshal(value)
  File "/home/anders/python/openapi-core/openapi_core/unmarshalling/schemas/unmarshallers.py", line 295, in unmarshal
    return unmarshaller(value)
  File "/home/anders/python/openapi-core/openapi_core/unmarshalling/schemas/unmarshallers.py", line 168, in __call__
    return list(map(self.items_unmarshaller, value))
  File "/home/anders/python/openapi-core/openapi_core/unmarshalling/schemas/unmarshallers.py", line 162, in items_unmarshaller
    return self.unmarshallers_factory.create(self.schema / "items")
  File "/home/anders/python/openapi-core/openapi_core/unmarshalling/schemas/factories.py", line 60, in create
    if schema.getkey("deprecated", False):
  File "/home/anders/.cache/pypoetry/virtualenvs/openapi-core-XEMBi3Y7-py3.9/lib/python3.9/site-packages/dictpath/paths.py", line 128, in getkey
    with self.open() as d:
  File "/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/contextlib.py", line 119, in __enter__
    return next(self.gen)
  File "/home/anders/.cache/pypoetry/virtualenvs/openapi-core-XEMBi3Y7-py3.9/lib/python3.9/site-packages/dictpath/paths.py", line 140, in open
    with self._open() as content:
  File "/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/contextlib.py", line 119, in __enter__
    return next(self.gen)
  File "/home/anders/python/openapi-core/openapi_core/spec/accessors.py", line 15, in open
    content = content[part]
KeyError: 'items'
p1c2u commented 1 year ago

Works with new version hence closing.