From 398189ea7af581b6df68d40449d0122d9cf2bfe3 Mon Sep 17 00:00:00 2001 From: Pedro Brochado Date: Thu, 24 Oct 2024 18:10:16 -0300 Subject: [PATCH] Fix Any serializer type regression breaking Ruby bindings See https://github.com/pulp/pulp_rpm/issues/3639 --- CHANGES/+fix-any-type.bugfix | 2 ++ pulp_ansible/app/fields.py | 12 ++++++++++++ pulp_ansible/app/galaxy/v3/serializers.py | 10 ++++++---- pulp_ansible/app/serializers.py | 17 +++++++++-------- 4 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 CHANGES/+fix-any-type.bugfix create mode 100644 pulp_ansible/app/fields.py diff --git a/CHANGES/+fix-any-type.bugfix b/CHANGES/+fix-any-type.bugfix new file mode 100644 index 000000000..89ea25f51 --- /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/pulp_ansible/app/fields.py b/pulp_ansible/app/fields.py new file mode 100644 index 000000000..110d2c55c --- /dev/null +++ b/pulp_ansible/app/fields.py @@ -0,0 +1,12 @@ +from drf_spectacular.utils import extend_schema_field +from drf_spectacular.types import OpenApiTypes +from rest_framework import serializers + + +@extend_schema_field(OpenApiTypes.OBJECT) +class JSONObjectField(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 + """ diff --git a/pulp_ansible/app/galaxy/v3/serializers.py b/pulp_ansible/app/galaxy/v3/serializers.py index 388f9354e..2fb941282 100644 --- a/pulp_ansible/app/galaxy/v3/serializers.py +++ b/pulp_ansible/app/galaxy/v3/serializers.py @@ -5,7 +5,7 @@ from rest_framework.reverse import reverse from rest_framework import serializers, relations -from pulp_ansible.app import models, serializers as ansible_serializers +from pulp_ansible.app import models, fields, serializers as ansible_serializers from pulpcore.plugin.models import RepositoryVersion from pulpcore.plugin import serializers as core_serializers @@ -294,10 +294,12 @@ class CollectionVersionSerializer(UnpaginatedCollectionVersionSerializer): A serializer for a CollectionVersion. """ - manifest = serializers.JSONField( + manifest = fields.JSONObjectField( help_text="A JSON field holding MANIFEST.json data.", read_only=True ) - files = serializers.JSONField(help_text="A JSON field holding FILES.json data.", read_only=True) + files = fields.JSONObjectField( + help_text="A JSON field holding FILES.json data.", read_only=True + ) class Meta: model = models.CollectionVersion @@ -310,7 +312,7 @@ class Meta: class CollectionVersionDocsSerializer(serializers.ModelSerializer): """A serializer to display the docs_blob of a CollectionVersion.""" - docs_blob = serializers.JSONField() + docs_blob = fields.JSONObjectField() class Meta: fields = ("docs_blob",) diff --git a/pulp_ansible/app/serializers.py b/pulp_ansible/app/serializers.py index e42e7421e..efa8dbae6 100644 --- a/pulp_ansible/app/serializers.py +++ b/pulp_ansible/app/serializers.py @@ -46,6 +46,7 @@ Role, Tag, ) +from pulp_ansible.app import fields from pulp_ansible.app.schema import COPY_CONFIG_SCHEMA from pulp_ansible.app.tasks.utils import ( parse_collections_requirements_file, @@ -578,11 +579,11 @@ class CollectionVersionSerializer(ContentChecksumSerializer, CollectionVersionUp read_only=True, ) - contents = serializers.JSONField( + contents = fields.JSONObjectField( help_text=_("A JSON field with data about the contents."), read_only=True ) - dependencies = serializers.JSONField( + dependencies = fields.JSONObjectField( help_text=_( "A dict declaring Collections that this collection requires to be installed for it to " "be usable." @@ -596,16 +597,16 @@ class CollectionVersionSerializer(ContentChecksumSerializer, CollectionVersionUp read_only=True, ) - docs_blob = serializers.JSONField( + docs_blob = fields.JSONObjectField( help_text=_("A JSON field holding the various documentation blobs in the collection."), read_only=True, ) - manifest = serializers.JSONField( + manifest = fields.JSONObjectField( help_text=_("A JSON field holding MANIFEST.json data."), read_only=True ) - files = serializers.JSONField( + files = fields.JSONObjectField( help_text=_("A JSON field holding FILES.json data."), read_only=True ) @@ -1030,8 +1031,8 @@ class CollectionImportDetailSerializer(CollectionImportListSerializer): A serializer for a CollectionImport detail view. """ - error = serializers.JSONField(source="task.error", required=False) - messages = serializers.JSONField() + error = fields.JSONObjectField(source="task.error", required=False) + messages = fields.JSONObjectField() class Meta(CollectionImportListSerializer.Meta): fields = CollectionImportListSerializer.Meta.fields + ("error", "messages") @@ -1076,7 +1077,7 @@ class CopySerializer(serializers.Serializer): A serializer for Content Copy API. """ - config = serializers.JSONField( + config = fields.JSONObjectField( help_text=_("A JSON document describing sources, destinations, and content to be copied"), )