Open
Description
I have an issue with nested self-referencing schema,
class GroupSchema(HasPermissionMixin, ModelSchema):
content = fields.Nested(GroupParamSchema, required=True)
class GroupParamSchema(Schema):
filters = fields.Nested(FilterSchema, required=True, many=True)
class FilterSchema(Schema):
next_filter = fields.Nested("FilterSchema", required=False, allow_none=True)
The issue is with FilterSchema
I have a helper method that converts Marshmallow Schema to an OpenApiJson object:
def get_openapi_schema(
serializer,
):
spec = APISpec(
title="",
version="",
openapi_version="3.0.2",
plugins=[MarshmallowPlugin(schema_name_resolver=schema_name_resolver)],
)
openapi_schema = OpenAPIConverter(openapi_version="3.0.2",schema_name_resolver=schema_name_resolver,spec=spec)
return {200: openapi_schema.schema2jsonschema(serializer)}
The schema_name_resolver
as described in the docs should not return None
for Circular schemas
def schema_name_resolver(schema):
schema_name = resolve_schema_cls(schema).__name__
circular = False
values = list(schema.fields.values())
for value in values:
if value.__class__.__name__ == "Nested":
if value.nested == schema_name:
circular = True
break
if circular:
return schema_name
return None
But it still complains:
File "/usr/local/lib/python3.9/site-packages/apispec/ext/marshmallow/openapi.py", line 297, in get_ref_dict
ref_schema = self.spec.components.get_ref("schema", self.refs[schema_key])
KeyError: (<class 'veylinx.api.insights.serializers.base.FilterSchema'>, None, frozenset(), frozenset(), frozenset(), False)
And I'm sure that the schema_name_resolver
is returning a string
when the schema is circular
Using a resolver as lambda schema_class: None
will raise the error below which is understandable!
apispec.exceptions.APISpecError: Name resolver returned None for schema <FilterSchema(many=False)> which is part of a chain of circular referencing schemas. Please ensure that the schema_name_resolver passed to MarshmallowPlugin returns a string for all circular referencing schemas.
I'm using:
Django==4.0.8
apispec==6.0.2
marshmallow==3.19.0
Debian GNU/Linux 11 (bullseye)