From d16b3ee3a00d5e7b168df461073a8a381c5939d6 Mon Sep 17 00:00:00 2001 From: Pedro Brochado Date: Thu, 24 Oct 2024 18:23:03 -0300 Subject: [PATCH] Fix Any serializer type regression breaking Ruby bindings See https://github.com/pulp/pulp_rpm/issues/3639 (cherry picked from commit 58425d791296752be427a43cda9df0d2403bc7bc) --- CHANGES/+fix-any-type.bugfix | 2 ++ pulpcore/app/serializers/domain.py | 2 +- pulpcore/app/serializers/exporter.py | 7 ++++--- pulpcore/app/serializers/fields.py | 11 +++++++++++ pulpcore/app/serializers/importer.py | 3 ++- pulpcore/app/serializers/task.py | 3 ++- 6 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 CHANGES/+fix-any-type.bugfix diff --git a/CHANGES/+fix-any-type.bugfix b/CHANGES/+fix-any-type.bugfix new file mode 100644 index 0000000000..89ea25f511 --- /dev/null +++ b/CHANGES/+fix-any-type.bugfix @@ -0,0 +1,2 @@ +Fixed the JSONField specification so it doesn't break ruby bindings. +See context [here](https://github.com/pulp/pulp_rpm/issues/3639). diff --git a/pulpcore/app/serializers/domain.py b/pulpcore/app/serializers/domain.py index 692b9f4799..478e976bdc 100644 --- a/pulpcore/app/serializers/domain.py +++ b/pulpcore/app/serializers/domain.py @@ -238,7 +238,7 @@ class GoogleSettingsSerializer(BaseSettingsClass): bucket_name = serializers.CharField(required=True) project_id = serializers.CharField(required=True) - # credentials = serializers.JSONField(write_only=True) # Need better upstream support + # credentials = fields.JSONDictField(write_only=True) # Need better upstream support custom_endpoint = serializers.CharField(allow_null=True, default=None) location = serializers.CharField(allow_blank=True, default="") default_acl = serializers.CharField(allow_null=True, default=None) diff --git a/pulpcore/app/serializers/exporter.py b/pulpcore/app/serializers/exporter.py index 0030b34e19..aa071a7982 100644 --- a/pulpcore/app/serializers/exporter.py +++ b/pulpcore/app/serializers/exporter.py @@ -15,6 +15,7 @@ RelatedField, RelatedResourceField, RepositoryVersionRelatedField, + fields, ) from pulpcore.constants import FS_EXPORT_CHOICES, FS_EXPORT_METHODS @@ -103,7 +104,7 @@ class ExportSerializer(ModelSerializer): view_name="None", # This is a polymorphic field. The serializer does not need a view name. ) - params = serializers.JSONField( + params = fields.JSONDictField( help_text=_("Any additional parameters that were used to create the export."), read_only=True, ) @@ -118,12 +119,12 @@ class PulpExportSerializer(ExportSerializer): Serializer for PulpExports. """ - output_file_info = serializers.JSONField( + output_file_info = fields.JSONDictField( help_text=_("Dictionary of filename: sha256hash entries for export-output-file(s)"), read_only=True, ) - toc_info = serializers.JSONField( + toc_info = fields.JSONDictField( help_text=_("Filename and sha256-checksum of table-of-contents for this export"), read_only=True, ) diff --git a/pulpcore/app/serializers/fields.py b/pulpcore/app/serializers/fields.py index 4400656ff3..a9e0323b4a 100644 --- a/pulpcore/app/serializers/fields.py +++ b/pulpcore/app/serializers/fields.py @@ -4,6 +4,8 @@ from urllib.parse import urljoin from django.conf import settings +from drf_spectacular.utils import extend_schema_field +from drf_spectacular.types import OpenApiTypes from rest_framework import serializers from rest_framework.fields import empty from rest_framework.reverse import reverse @@ -20,6 +22,15 @@ def relative_path_validator(relative_path): ) +@extend_schema_field(OpenApiTypes.OBJECT) +class JSONDictField(serializers.JSONField): + """A drf JSONField override to force openapi schema to use 'object' type. + + Not strictly correct, but we relied on that for a long time. + See: https://github.com/tfranzel/drf-spectacular/issues/1095 + """ + + class SingleContentArtifactField(RelatedField): """ A serializer field for the '_artifacts' ManyToManyField on the Content model (single-artifact). diff --git a/pulpcore/app/serializers/importer.py b/pulpcore/app/serializers/importer.py index 8b0c66a538..742ad9ab17 100644 --- a/pulpcore/app/serializers/importer.py +++ b/pulpcore/app/serializers/importer.py @@ -12,6 +12,7 @@ ModelSerializer, RelatedField, ValidateFieldsMixin, + fields, ) @@ -40,7 +41,7 @@ class ImportSerializer(ModelSerializer): view_name="tasks-detail", ) - params = serializers.JSONField( + params = fields.JSONDictField( help_text=_("Any parameters that were used to create the import."), ) diff --git a/pulpcore/app/serializers/task.py b/pulpcore/app/serializers/task.py index f9b9c42717..41941dbd04 100755 --- a/pulpcore/app/serializers/task.py +++ b/pulpcore/app/serializers/task.py @@ -13,6 +13,7 @@ RelatedField, RelatedResourceField, TaskGroupStatusCountField, + fields, ) from pulpcore.constants import TASK_STATES from pulpcore.app.util import get_domain @@ -45,7 +46,7 @@ class TaskSerializer(ModelSerializer): help_text=_("Timestamp of the when this task stopped execution."), read_only=True ) error = serializers.DictField( - child=serializers.JSONField(), + child=fields.JSONDictField(), help_text=_( "A JSON Object of a fatal error encountered during the execution of this task." ),