python-openapi/openapi-core

Spec now validates three times

p1c2u opened this issue · 0 comments

p1c2u commented
          This seems wasteful since `Spec.from_dict` has already validated the spec. This now validates the spec three times, once in `Spec.from_dict`, once in `validate_request`, and once in `validate_response`:
from openapi_core import Spec, validate_request, validate_response
from openapi_core.testing import MockRequest, MockResponse

spec = Spec.from_dict(
    {
        "openapi": "3.1.0",
        "info": {"title": "Test", "version": "1.0.0"},
        "paths": {"/": {"get": {"responses": {"200": {"description": "OK"}}}}},
    },
)
request = MockRequest("http://localhost", "get", "/")
validate_request(request, spec=spec)
response = MockResponse("")
validate_response(request, response, spec=spec)

This adds a very expensive per-request and per-response overhead when the spec is large.

Moreover, the documented workaround doesn’t work; this still validates the spec (as can be seen by misspelling title, say):

from openapi_core import Spec, validate_request, validate_response
from openapi_core.testing import MockRequest, MockResponse

spec = Spec.from_dict(
    {
        "openapi": "3.1.0",
        "info": {"titel": "Test", "version": "1.0.0"},
        "paths": {"/": {"get": {"responses": {"200": {"description": "OK"}}}}},
    },
    validator=None,
)
request = MockRequest("http://localhost", "get", "/")
validate_request(request, spec=spec, spec_validator_cls=None)
response = MockResponse("")
validate_response(request, response, spec=spec, spec_validator_cls=None)

Originally posted by @andersk in #686 (comment)