Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Serializer schema generation when used as a ListField child #1175

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix Serializer schema generation when used as a ListField child
arttuperala committed Sep 12, 2023
commit e87dff30c572cd8a215c8565abf221dab2a36a27
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Note that in line with [Django REST framework policy](https://www.django-rest-framework.org/topics/release-notes/),
any parts of the framework not mentioned in the documentation should generally be considered private API, and may be subject to change.

## [Unreleased]

### Fixed

* Fixed OpenAPI schema generation for `Serializer` children of `ListField`.

## [6.1.0] - 2023-08-25

### Added
21 changes: 21 additions & 0 deletions example/factories.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
Company,
Entry,
ProjectType,
Questionnaire,
ResearchProject,
TaggedItem,
)
@@ -140,3 +141,23 @@ def future_projects(self, create, extracted, **kwargs):
if extracted:
for project in extracted:
self.future_projects.add(project)


class QuestionnaireFactory(factory.django.DjangoModelFactory):
class Meta:
model = Questionnaire

name = factory.LazyAttribute(lambda x: faker.text())
questions = [
{
"text": "What is your name?",
"required": True,
},
{
"text": "What is your quest?",
"required": False,
},
{
"text": "What is the air-speed velocity of an unladen swallow?",
},
]
28 changes: 28 additions & 0 deletions example/migrations/0013_questionnaire.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.5 on 2023-09-07 02:35

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("example", "0012_author_full_name"),
]

operations = [
migrations.CreateModel(
name="Questionnaire",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=100)),
("questions", models.JSONField()),
],
),
]
5 changes: 5 additions & 0 deletions example/models.py
Original file line number Diff line number Diff line change
@@ -180,3 +180,8 @@ class Company(models.Model):

def __str__(self):
return self.name


class Questionnaire(models.Model):
name = models.CharField(max_length=100)
questions = models.JSONField()
14 changes: 14 additions & 0 deletions example/serializers.py
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
LabResults,
Project,
ProjectType,
Questionnaire,
ResearchProject,
TaggedItem,
)
@@ -421,3 +422,16 @@ class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = "__all__"


class QuestionSerializer(serializers.Serializer):
text = serializers.CharField()
required = serializers.BooleanField(default=False)


class QuestionnaireSerializer(serializers.ModelSerializer):
questions = serializers.ListField(child=QuestionSerializer())

class Meta:
model = Questionnaire
fields = "__all__"
2 changes: 2 additions & 0 deletions example/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
CommentFactory,
CompanyFactory,
EntryFactory,
QuestionnaireFactory,
ResearchProjectFactory,
TaggedItemFactory,
)
@@ -27,6 +28,7 @@
register(ArtProjectFactory)
register(ResearchProjectFactory)
register(CompanyFactory)
register(QuestionnaireFactory)


@pytest.fixture
32 changes: 32 additions & 0 deletions example/tests/test_openapi.py
Original file line number Diff line number Diff line change
@@ -125,6 +125,38 @@ def test_schema_id_field():
assert "id" not in company_properties["attributes"]["properties"]


def test_schema_list_of_objects():
"""Schema for ListField child Serializer reflects the actual response structure."""
patterns = [
re_path(
"^questionnaires/?$", views.QuestionnaireViewset.as_view({"get": "list"})
),
]
generator = SchemaGenerator(patterns=patterns)

request = create_request("/")
schema = generator.get_schema(request=request)

assert {
"type": "object",
"properties": {
"questions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"text": {"type": "string"},
"required": {"type": "boolean", "default": False},
},
"required": ["text"],
},
},
"name": {"type": "string", "maxLength": 100},
},
"required": ["questions", "name"],
} == schema["components"]["schemas"]["Questionnaire"]["properties"]["attributes"]


def test_schema_parameters_include():
"""Include paramater is only used when serializer defines included_serializers."""
patterns = [
32 changes: 32 additions & 0 deletions example/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -224,6 +224,38 @@ def test_model_serializer_with_implicit_fields(self, comment, client):
assert response.status_code == 200
assert expected == response.json()

def test_model_serializer_with_list_of_objects(self, questionnaire, client):
expected = {
"data": {
"type": "questionnaires",
"id": str(questionnaire.pk),
"attributes": {
"name": questionnaire.name,
"questions": [
{
"text": "What is your name?",
"required": True,
},
{
"text": "What is your quest?",
"required": False,
},
{
"text": "What is the air-speed velocity of an unladen swallow?",
"required": False,
},
],
},
},
}

response = client.get(
reverse("questionnaire-detail", kwargs={"pk": questionnaire.pk})
)

assert response.status_code == 200
assert expected == response.json()


class TestPolymorphicModelSerializer(TestCase):
def setUp(self):
2 changes: 2 additions & 0 deletions example/urls.py
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
NonPaginatedEntryViewSet,
ProjectTypeViewset,
ProjectViewset,
QuestionnaireViewset,
)

router = routers.DefaultRouter(trailing_slash=False)
@@ -32,6 +33,7 @@
router.register(r"projects", ProjectViewset)
router.register(r"project-types", ProjectTypeViewset)
router.register(r"lab-results", LabResultViewSet)
router.register(r"questionnaires", QuestionnaireViewset)

urlpatterns = [
path("", include(router.urls)),
2 changes: 2 additions & 0 deletions example/urls_test.py
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
NonPaginatedEntryViewSet,
ProjectTypeViewset,
ProjectViewset,
QuestionnaireViewset,
)

router = routers.DefaultRouter(trailing_slash=False)
@@ -38,6 +39,7 @@
router.register(r"projects", ProjectViewset)
router.register(r"project-types", ProjectTypeViewset)
router.register(r"lab-results", LabResultViewSet)
router.register(r"questionnaires", QuestionnaireViewset)

# for the old tests
router.register(r"identities", Identity)
7 changes: 7 additions & 0 deletions example/views.py
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
LabResults,
Project,
ProjectType,
Questionnaire,
)
from example.serializers import (
AuthorDetailSerializer,
@@ -43,6 +44,7 @@
LabResultsSerializer,
ProjectSerializer,
ProjectTypeSerializer,
QuestionnaireSerializer,
)

HTTP_422_UNPROCESSABLE_ENTITY = 422
@@ -292,3 +294,8 @@ class LabResultViewSet(ReadOnlyModelViewSet):
"__all__": [],
"author": ["author__bio", "author__entries"],
}


class QuestionnaireViewset(ModelViewSet):
queryset = Questionnaire.objects.all()
serializer_class = QuestionnaireSerializer
5 changes: 5 additions & 0 deletions rest_framework_json_api/schemas/openapi.py
Original file line number Diff line number Diff line change
@@ -681,6 +681,11 @@ def map_serializer(self, serializer):
and 'links'.
"""
# TODO: remove attributes, etc. for relationshipView??
if isinstance(serializer.parent, serializers.ListField):
# Return plain non-JSON:API serializer schema for serializers nested inside
# a ListField, as those don't use the full JSON:API serializer schemas.
return super().map_serializer(serializer)

required = []
attributes = {}
relationships_required = []