diff --git a/CHANGES/3639.bugfix b/CHANGES/3639.bugfix new file mode 100644 index 000000000..8ca6e6431 --- /dev/null +++ b/CHANGES/3639.bugfix @@ -0,0 +1 @@ +Fixed openapi schema generation by monkeypatching that drf JSONField is always an OA 'object'. diff --git a/pulp_rpm/app/__init__.py b/pulp_rpm/app/__init__.py index 3324c37e3..2de286158 100644 --- a/pulp_rpm/app/__init__.py +++ b/pulp_rpm/app/__init__.py @@ -11,3 +11,8 @@ class PulpRpmPluginAppConfig(PulpPluginAppConfig): version = "3.28.0.dev" python_package_name = "pulp-rpm" domain_compatible = True + + def ready(self): + # include drf-spectacular Extension module + import pulp_rpm.app.schema.extensions # noqa: E402, F401 + super().ready() diff --git a/pulp_rpm/app/schema/extensions.py b/pulp_rpm/app/schema/extensions.py new file mode 100644 index 000000000..4c7634133 --- /dev/null +++ b/pulp_rpm/app/schema/extensions.py @@ -0,0 +1,23 @@ +"""drf-spectacular Extensions. + +See: +https://drf-spectacular.readthedocs.io/en/latest/customization.html#step-5-extensions +""" +from drf_spectacular.extensions import OpenApiSerializerFieldExtension +from drf_spectacular.plumbing import build_basic_type +from drf_spectacular.types import OpenApiTypes + + +class RestrictedJsonFieldExtension(OpenApiSerializerFieldExtension): + """ + Workaround to makes drf JSONField to produce only Object Type. + + See: + https://github.com/tfranzel/drf-spectacular/issues/1242 + """ + + target_class = "rest_framework.fields.JSONField" + # match_subclasses = True # not needed here but good to know. + + def map_serializer_field(self, auto_schema, direction): + return build_basic_type(OpenApiTypes.OBJECT)