You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've currently implemented schema validation of API responses in my tests using python-jsonschema's RefResolver. Everything works perfectly!
fromtypingimportAnyimportopenapi_schema_validatorfromdrf_spectacular.settingsimportspectacular_settingsfromjsonschemaimportRefResolverfromopenapi_schema_validatorimportOAS31Validator@cachedef_get_schema(
api_version: str="v4", # See: https://drf-spectacular.readthedocs.io/en/latest/faq.html#i-get-an-empty-schema-or-endpoints-are-missing
) ->dict[str, Any]:
generator_class=spectacular_settings.DEFAULT_GENERATOR_CLASSgenerator=generator_class(api_version=api_version)
returngenerator.get_schema(request=None, public=True)
defvalidate_schema(
instance: dict[str, Any],
*,
version: str,
path: str,
method: str,
response: str,
content: str="application/json",
) ->None:
schema=_get_schema(version)
try:
_methods=schema["paths"][path]
exceptKeyError: # pragma: no cover (should never happen)msg=f"Path {path} not found in schema"raiseValueError(msg) fromNonetry:
_responses=_methods[method]
exceptKeyError: # pragma: no cover (should never happen)msg=f"Method {method} not found in schema"raiseValueError(msg) fromNonetry:
_content=_responses["responses"][response]["content"]
exceptKeyError: # pragma: no cover (should never happen)msg=f"Response {response} not found in schema"raiseValueError(msg) fromNonetry:
_schema=_content[content]["schema"]
exceptKeyError: # pragma: no cover (should never happen)msg=f"Content {content} not found in schema"raiseValueError(msg) fromNone# TODO: migrate to referencing libopenapi_schema_validator.validate(
instance=instance,
schema=_schema,
cls=OAS31Validator,
resolver=RefResolver.from_schema(schema),
)
But I'm getting the following error: jsonschema.exceptions._WrappedReferencingError: PointerToNowhere: '/components/schemas/Foo' does not exist within {'$ref': '#/components/schemas/Foo'}
After asking for help in python-openapi (here) and python-jsonschema (here) it seems to me like it may be an issue with the generated schema and it's structure (which is generated by drf-spectacular)
Unfortunately I'm not an expert on this topic, so could you point me in the right direction and figure out where the issue may be?
Thank you!
The text was updated successfully, but these errors were encountered:
I've currently implemented schema validation of API responses in my tests using python-jsonschema's RefResolver. Everything works perfectly!
I use it as such:
As described here, I'm now looking to migrate to the referencing library with code that looks like this:
But I'm getting the following error:
jsonschema.exceptions._WrappedReferencingError: PointerToNowhere: '/components/schemas/Foo' does not exist within {'$ref': '#/components/schemas/Foo'}
After asking for help in python-openapi (here) and python-jsonschema (here) it seems to me like it may be an issue with the generated schema and it's structure (which is generated by drf-spectacular)
Unfortunately I'm not an expert on this topic, so could you point me in the right direction and figure out where the issue may be?
Thank you!
The text was updated successfully, but these errors were encountered: